Opentopia Directory Encyclopedia Tools

Fork (operating system)

Encyclopedia : F : FO : FOR : Fork (operating system)


This article is about "forking" a process in a multitasking or multithreading operating system.
For other uses, see Fork (disambiguation).
A fork, when applied to computing is when a process creates a copy of itself, which then acts as a "child" of the original process, 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 process may execute independent of each other.

Example

Here 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 are executed simultaneously.

int pid;
pid = fork();
if(pid == 0)

_exit(0); /* Note that we do not use exit() */
}
else if(pid > 0)

}
else

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.

See also

 


From Wikipedia, the Free Encyclopedia. Original article here. Support Wikipedia by contributing or donating.
All text is available under the terms of the GNU Free Documentation License See Wikipedia Copyrights for details.

Search Titles
0123456789
ABCDEFGHIJ
KLMNOPQRST
UVWXYZ?

E-mail this article to:

Personal Message: