Processes & the Process Abstraction

the parent-child relationship

Families have a natural structure: every person has a parent, and the relationship comes with duties (a parent looks after a child). Processes work the same way. Every process except the very first is created by another process, and the relationship is called parent and child: the process that did the creating is the parent, and the one created is the child. This relationship carries real responsibilities, especially cleanup.

When a process calls fork, it becomes the parent and the new process is its child. The child inherits much from the parent at the moment of creation, copies of its memory, its open files, and so on, but afterward they run as separate processes with their own state. The OS records each child's parent (each process knows its parent's PID), which lets a parent keep track of and manage its children. The most important duty is that when a child finishes, the parent is expected to wait for it and collect its exit status, an act called reaping; until the parent does so, the finished child lingers as a zombie occupying a process-table slot.

This parent-child link is the thread from which the whole process tree is woven: every process points back to a parent, all the way up to the first process. It also explains two oddities you may meet. A zombie is a child that has exited but whose parent has not yet reaped it. An orphan is a child whose parent died first; orphans are adopted by the first process (init), which then takes over the duty of reaping them. So the parent-child relationship is not just genealogy, it is the mechanism by which terminated processes get cleaned up.

A shell (parent) forks a child to run a command. While the command runs, the shell is its parent; when the command exits, the shell waits for it, reaps it, and reads its exit status to know whether the command succeeded.

The parent creates the child, then later reaps it and reads its status.

Reaping is the parent's job. If a parent never waits for a finished child, the child becomes a zombie; if the parent dies first, the child becomes an orphan and is adopted by init.

Also called
parent and childprocess parentage父行程與子行程親子關係