Fork (operating system)
From Wikipedia, the free encyclopedia
- This article is about "forking" a process in a multitasking or multithreading operating system.
- For other uses, see Fork (disambiguation).
| It has been suggested that Fork Paging be merged into this article or section. (Discuss) |
A fork, when applied to computing occurs when a process creates a copy of itself, which is called a "child." The original process is now called the "parent". More generally, a fork in a multithreading environment means that a thread of execution is duplicated.
Under Unix and Unix-like operating systems, the parent and the child operations are selected by examining the return value of the fork() system call. In the child process, the return value of fork() is 0, whereas the return value in the parent process is the PID of the newly-created child process.
As soon as fork is called, there will be a separate address space created for the child. The child process will have an exact copy of all the segments of the parent process, though if copy-on-write semantics are implemented actual physical memory may not be assigned. Both the parent and child processes may execute independently of each other.
Contents |
Forking is an important part of Unix, critical to the support of its design philosophy, which encourages the development of filters. In Unix, a filter is a small program that reads its input from stdin, and writes its output to stdout. A pipeline of these commands can be strung together by a shell to create new commands. For example, one can string together the output of the find(1) command and the input of the wc(1) command to create a new command that will print a count of files ending in ".cpp" found in the current directory, as follows:
$ find . -name "*.cpp" -print | wc -l
In order to accomplish this, the shell forks itself, and uses pipes, a form of interprocess communication, to tie the output of the find command to the input of the wc command. Two child processes are created, one for each command (find and wc). These child processes are overlaid with the code associated with the programs they are intended to execute, using the exec(3) family of system calls (in the above example, find will overlay the first child process, and wc will overlay the second child process, and the shell will use pipes to tie the output of find with the input of wc).
More generally, forking is also performed by the shell each time a user issues a command. A child process is created by forking the shell, and the child process is overlaid, once again by exec, with the code associated with the program to be executed.
Below is some sample C programming language code to illustrate the idea of forking. The code that is in the "Child process" and "Parent process" sections is executed simultaneously in two different processes.
#include/* printf, stderr, fprintf */ #include /* _exit */ #include /* exit */ #include /* so we can handle the errors */ int main(void) { pid_t pid = fork(); if (pid == 0) { /* Child process: * When fork() returns 0, we are in * the child process. * Here we count up to ten, one each second. */ int j; for (j = 0; j < 10; j++) { printf("child: %d\n", j); sleep(1); } _exit(0); /* Note that we do not use exit() */ } else if (pid > 0) { /* Parent process: * When fork() returns a positive number, we are in the parent process * (whose process id is the one returned by the fork). * Again we count up to ten. */ int i; for (i = 0; i < 10; i++) { printf("parent: %d\n", i); sleep(1); } exit(0); } else { /* Error: When fork() returns a negative number, an error happened (for example, number of processes reached the limit). */ fprintf(stderr, "can't fork, error %d\n", errno); exit(1); } }
This code will print out the following:
parent: 0 child: 0 child: 1 parent: 1 parent: 2 child: 2 child: 3 parent: 3 parent: 4 child: 4 child: 5 parent: 5 parent: 6 child: 6 child: 7 parent: 7 parent: 8 child: 8 child: 9 parent: 9
The order of each output is determined by the kernel.