In computing, fork is an operation whereby a process creates a copy of itself. It is usually implemented as a C standard library wrapper to the fork, clone, or other system calls of the kernel. For many years, fork was the primary method of process creation on Unix and Unix-like operating systems, and it remains a required interface for compliance with POSIX. Despite this, it has fallen out of favor in recent years as a result of flaws including poor performance, a lack of thread safety, and its status as a common source of security vulnerabilities.
Overview In multitasking operating systems, processes (running programs) need a way to create new processes, e.g. to run other programs. Fork and its variants are typically the only way of doing so in Unix-like systems. For a process to start the execution of a different program, it first forks to create a copy of itself. Then, the copy, called the "child process", makes any environment changes the child will need and then calls the exec system call to overlay itself with the new program: it ceases execution of its former program in favor of the new. (Or, in rarer cases, the child forgoes the exec and continues executing, as a separate process, some other functionality of the original program.) The fork operation creates a separate address space for the child. The child process has an exact copy of all the memory segments of the parent process. In modern UNIX variants that follow the virtual memory model from SunOS-4.0, copy-on-write semantics are implemented and the physical memory need not be actually copied. Instead, virtual memory pages in both processes may refer to the same pages of physical memory until one of them writes to such a page: then it is copied. This optimization is important in the common case where fork is used in conjunction with exec to execute a new program: typically, the child process performs only a small set of actions before it ceases execution of its program in favour of the program to be started, and it requires very few, if any, of its parent's data structures. When a process calls fork, it is deemed the parent process and the newly created process is its child. After the fork, both processes not only run the same program, but they resume execution as though both had called the system call. They can then inspect the call's return value to determine their status, child or parent, and act accordingly. The history of fork dates back to the 1960s as one of the earliest references to a fork concept appeared in A Multiprocessor System Design by Melvin Conway, published in 1962. Conway's paper motivated the implementation by L. Peter Deutsch of fork in the GENIE time-sharing system, where the concept was borrowed by Ken Thompson for its earliest appearance in Research Unix. Fork later became a standard interface in POSIX.
Technical
Example The following variant of the "Hello, World!" program demonstrates the mechanics of the fork system call in the C programming language. The program forks into two processes, each deciding what functionality they perform based on the return value of the fork system call. What follows is a dissection of this program.
The first statement in main calls the fork system call to split execution into two processes. The return value of fork is recorded in a variable of type pid_t, which is the POSIX type for process identifiers (PIDs), defined to be a signed integer type.
A negative result indicates an error in fork: no new process was created, so an error message is printed. If fork was successful, then there are now two processes, both executing the main function from the point where fork has returned. To make the processes perform different tasks, the program must branch on the return value of fork to determine whether it is executing as the child process or the parent process.
In the child process, the return value appears as zero (which is an invalid process identifier; the child can use getpid() to obtain its own process identifier if needed). The child process prints the desired greeting message, then exits.
The other process, the parent, receives from fork the process identifier of the child, which is always a positive number. The parent process passes this identifier to the waitpid system call to suspend execution until the child has exited. When this has happened, the parent resumes execution and exits by means of the return statement.
Communication The child process starts off with a copy of its parent's file descriptors. For inter-process communication with data exchange and synchronization between parent and child processes, pipes can be used. The parent process will often create one or several pipes, and then after forking the processes will close the ends of the pipes that they do not need. To share the same variable between the parent and child processes, memory mapping or shared memory must be used.
Variants
… excerpt ends here. Continue reading the full article.