Process Creation
Creating a new process: fork()
- The fork() system call creates a new process, the child, which is an almost exact duplicate of the calling process, the parent.
- The key point to understanding fork() is to realize that after it has completed its work, two processes exist, and, in each process, execution continues from the point where fork() returns.
- The two processes are executing the same program text, but they have separate copies of the stack, data, and heap segments. The child’s stack, data, and heap segments are initially exact duplicates of the corresponding parts the parent’s memory. After the fork(), each process can modify the variables in its stack, data, and heap segments without affecting the other process
Explain
- For the parent,
fork()
returns the process ID of the newly created child (to track viawait()
system calls) - For the child,
fork()
returns 0. The child can obtain its own process ID usinggetpid()
, and the process ID of its parent usinggetppid()
. - When a fork() is performed, the child receives duplicates of all of the parent’s file descriptors. These duplicates are made in the manner of dup(), which means that corresponding descriptors in the parent and the child refer to the same open file description