Skip to content

Commit

Permalink
os
Browse files Browse the repository at this point in the history
- process operation (zombie process)
- init system (orphan process)
- process (note formatting)
  • Loading branch information
xy-241 committed Dec 23, 2024
1 parent dbad530 commit 60d1fa0
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 26 deletions.
33 changes: 16 additions & 17 deletions content/OS/Process/Process (进程).md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ tags:
- OS
- linux
Creation Date: 2023-10-19T17:12:00
Last Date: 2024-05-20T21:54:38+08:00
Last Date: 2024-12-23T10:34:37+08:00
References:
description: Dive into the world of processes in operating systems!
---
Expand All @@ -33,21 +33,6 @@ description: Dive into the world of processes in operating systems!
>
> `VmRSS` represents the size of [[Main Memory#Memory Frames]] a process is currently using.
## Useful CLI tool
---
## procs
- A `ps` replacement written in [[Rust]]

```bash
brew install procs # Installation

# Alias to ps, and add in config file, add the following line to .zshrc
alias ps='sudo procs --load-config <path_to_your_config.toml>' # Refer to https://github.com/dalance/procs?tab=readme-ov-file#configuration for more info on the config.toml
```

## Terminologies
---

### Core Image

- A **suspended** [[Process (进程)]] consists of its [[Address Space]]
Expand All @@ -65,4 +50,18 @@ alias ps='sudo procs --load-config <path_to_your_config.toml>' # Refer to https:
- A list of open [[File]]
- Outstanding [[Interrupts (中断)#Software Interrupt]]
- List of related processes
- [[Process Management]] information etc
- [[Process Management]] information etc



## procs
---
- A `ps` replacement written in [[Rust]]

```bash
brew install procs # Installation

# Alias to ps, and add in config file, add the following line to .zshrc
alias ps='sudo procs --load-config <path_to_your_config.toml>' # Refer to https://github.com/dalance/procs?tab=readme-ov-file#configuration for more info on the config.toml
```

47 changes: 39 additions & 8 deletions content/OS/Process/Process Operations.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Author Profile:
tags:
- OS
Creation Date: 2023-08-09T22:50:00
Last Date: 2024-12-22T19:09:46+08:00
Last Date: 2024-12-23T10:55:10+08:00
References:
description: Process creation uses fork() and exec() to create a child process, with termination occurring voluntarily or involuntarily. In POSIX, fork() creates a child, and wait() allows the parent to wait for its termination while preventing zombie processes.
title: Process Creation and Termination in POSIX Systems
Expand Down Expand Up @@ -38,12 +38,43 @@ if (pid == 0)
---
### 2 Voluntary Ways
1. [[Process (进程)]] ends its job
2. [[Process (进程)]] hits an error during execution and exits gracefully
2. Process hits an error during execution and exits gracefully

### 2 Involuntary Ways
1. Fatal error - which couldn't be handled by the [[Process (进程)]] itself (eg. [[Memory Failure]])
1. Fatal error - which couldn't be handled by the [[Process (进程)]] itself (eg. [[Segmentation Fault]])
2. Termination by other process (killem all!)

### Zombie Process

```c
// In parent process
wait(&status); // Waits for only one child process to terminate
// Other child processes continue running

while (1) { // Infinite loop
/* Empty loop body */
}

exit(0); // Unreachable code - program never exits from while loop
// WARNING: Any remaining child processes become zombies
// since parent never waits for them to terminate
```
- A [[#Child Process]] that has [[#Process Termination|completed execution]] but still has an entry in the [[Process Control Block (PCB)#Process Table|process table]] because its parent has not yet called [[Process Operations#wait()]] to retrieve its exit status
>[!code] A zombie process lives forever
> In the code example above, the parent process keeps running and never calls `wait()`.
>[!question] Why isn’t the child process removed automatically after completing its job?
> The [[OS]] allows the parent process to check on its child using `wait()` to retrieve the completion status.
>[!important]
> Zombie processes can be removed by terminating their parent process. When the parent process terminates, the OS knows there’s no way for the child processes to be checked, so it cleans them up.
>[!tool]
> You can check for zombie processes with the command: `ps aux | grep 'Z'`.
## POSIX
---
Expand Down Expand Up @@ -72,10 +103,10 @@ while (wait(NULL) > 0) {
// Each iteration handles one child process
// Loop continues until all children are done
}

// WRONG - may leave zombie processes
wait(&status); // Only waits for one child
exit(0); // Other children become zombies
```
- Used by a parent [[Process (进程)|process]] to wait for one of its child processes to terminate
- Used by a parent [[Process (进程)|process]] to wait for one of its child processes to terminate
## References
---
- [Understanding Zombie Processes! - YouTube](https://youtu.be/xJ8KenZw2ag?si=uVqic4a76GV-rlFX)
10 changes: 9 additions & 1 deletion content/OS/Tools/Init System.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ Author Profile:
tags:
- OS
Creation Date: 2023-10-23T15:26:00
Last Date: 2024-11-11T10:41:43+08:00
Last Date: 2024-12-23T10:41:07+08:00
References:
description: The init process (PID 1) manages the lifecycle of all system services, adopting orphaned processes whose parents terminate and potentially leaving them as zombies if they exit without being reaped.
---
## Abstract
---
- A [[Process (进程)]] that runs in the background
- Most important [[Process (进程)]] with [[Process IDentifier (PID)]] **1**
- Manages the **lifecycle** of **all services** that run in the background
- Below shows a list of common init systems. Basically [[Systemd]] for [[Linux Kernel]], [[Launchd]] for [[MacOS]] and **busybox** for for [[OS#Embedded OS]] like [[OpenWRT]]

<div class="onecompilerCode-wrapper">
<iframe
class="onecompilerCode"
Expand All @@ -28,3 +30,9 @@ References:
> If you just want to run a task in the background after the user logs out, you can just run `nohup <command> &`, the **standard output** and **standard error** streams are redirected to a file named `nohup.out` in the **current directory by default**. But this doesn't restart the service on failure or after a reboot etc.

## Orphan Process
---
- A [[Process (进程)#Child Process]] becomes an orphan if its **parent process terminates before it completes**, and it is typically adopted by the [[Init System]] which immediately calls [[Process Operations#wait()]] on the child process to prevent it from becoming a [[Process Operations#Zombie Process|zombie]]

>[!important]
> If an orphan process exits and its **adopted parent hasn't reaped it yet**, it can simultaneously be both an orphan and a zombie.

0 comments on commit 60d1fa0

Please sign in to comment.