Abstract
Process Creation
- A 2 steps process - fork(), exec()
- This two-step process gives the child process the flexibility to manipulate its file descriptors (stdin in the example below) after
fork()
but beforeexecve()
, in order to accomplish redirection of standard input, standard output, and standard error - The code below creates a child process, pipes the input to the child process, then loads and executes the child process
Process Termination
2 Voluntary Ways
- Process (进程) ends its job
- Process hits an error during execution and exits gracefully
2 Involuntary Ways
- Fatal error - which couldn’t be handled by the Process (进程) itself (eg. Segmentation Fault)
- Termination by other process (killem all!)
Zombie Process
- A Child Process that has completed execution but still has an entry in the process table because its parent has not yet called wait() to retrieve its exit status
A zombie process lives forever
In the code example above, the parent process keeps running and never calls
wait()
.
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
fork()
- Creates an exact duplicate of the original Process (进程)
fork()
returns-1
if the fork fails
wait()
- Used by a parent process to wait for one of its child processes to terminate