Processes
A process holds the environment in which threads can run: it holds the memory mappings, the file descriptors, the user and group IDs, and more. The first process is the init
process, which is created by the kernel during boot and has a PID of one. Thereafter, processes are created by duplication in an operation known as forking.
Creating a new process
The POSIX function to create a process is fork(2)
. It is an odd function because, for each successful call, there are two returns: one in the process that made the call, known as the Parent, and one in the newly created process, known as the Child, as shown in the following diagram:
Immediately after the call, the child is an exact copy of the parent: it has the same stack, the same heap, the same file descriptors, and it executes the same line of code – the one following fork
. The only way the programmer can tell them apart is by looking at the return value...