complement 9.md 10.md 11.md
This commit is contained in:
59
notes/9.md
59
notes/9.md
@@ -84,7 +84,7 @@ Single processor executes multiple processes concurrently. process execution int
|
||||
|
||||
Multicore processor share main memory each can execute a separate process. scheduling of processors onto cores done by kernel.
|
||||
|
||||
### Concurrent Processes
|
||||
#### Concurrent Processes
|
||||
|
||||
Concurrency is **not at the exact same time**.
|
||||
|
||||
@@ -94,7 +94,7 @@ Control flows for concurrent processes are pysically disjoint in time. But user
|
||||
|
||||
* Execution time of instruction may vary because of the Nondeterminism of the System: OS scheduling, Interrupts, Cache miss or Page fault, I/O device delays.
|
||||
|
||||
### Context Switching
|
||||
#### Context Switching
|
||||
|
||||
Prcess are managed by a shared chunk of memory-resident OS code called the **kernel**.
|
||||
|
||||
@@ -180,7 +180,7 @@ int main() {
|
||||
}
|
||||
```
|
||||
|
||||
```sh {cmd}
|
||||
```sh {cmd hide}
|
||||
while ! [ -r 9_1.out ]; do sleep .1; done; ./9_1.out
|
||||
```
|
||||
|
||||
@@ -197,7 +197,58 @@ Any topological sort of the graph corresponds to a feasible total ordering.
|
||||
|
||||
### Reaping Child Processes
|
||||
|
||||
When a child process terminates, it becomes a **zombie** until its parent calls `wait` to read its exit status.
|
||||
When process terminates, it still consumes system resources. It is called a "zombie".
|
||||
|
||||
**"Reaping"** is performed by the parent by using `wait` or `waitpid`. And then parent is given exit status information. Finally kernel then deletes zombie child process.
|
||||
|
||||
If any parent terminates without reaping a child, then the orphaned child will be reaped by the `init` process (pid 1).
|
||||
However, long-running processes should reap their children to avoid accumulating zombies.
|
||||
|
||||
```c {cmd=gcc, args=[-O2 -x c $input_file -o 9_2.out]}
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main() {
|
||||
if(fork() == 0) {
|
||||
printf("Terminating child process\n");
|
||||
exit(0);
|
||||
} else {
|
||||
printf("Running Parent\n");
|
||||
sleep(1);
|
||||
printf("Parent exiting\n");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```sh {cmd hide}
|
||||
while ! [ -r 9_2.out ]; do sleep .1; done; ./9_2.out & ps -ef | grep "9_2.out" | grep -v grep;
|
||||
```
|
||||
|
||||
|
||||
```c
|
||||
pid_t wait(int * child_status);
|
||||
```
|
||||
|
||||
`wait` suspends current process until **one of its children** terminates.
|
||||
|
||||
* **return value**: pid of terminated child
|
||||
* **child_status**: if not `NULL`, the integer points will be set to the termination status(reason and exit status) of the child.
|
||||
* It can be checked by using macros defined in `wait.h`
|
||||
|
||||
## execve
|
||||
|
||||
```c
|
||||
int execve(const char *filename, char *const argv[], char *envp[]);
|
||||
```
|
||||
|
||||
Load and runs in the current process.
|
||||
|
||||
* `filename`: path of the executable file
|
||||
* `argv`: argument list
|
||||
* `envp`: environment variables "name=value"
|
||||
* `getenv`, `putenv`, `printenv`
|
||||
|
||||
It does **overwrite code, data, stack**. retain only **pid, file descriptors, signal context**.
|
||||
|
||||
Called **once and never returns on success**.
|
||||
|
||||
Reference in New Issue
Block a user