Fork (operating system)

Learn more about Fork (operating system)

(Redirected from Fork (computing))
Jump to: navigation, search
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.


[edit] Importance of Fork In Unix

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.

[edit] 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)
      /* 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);
      _exit(0); /* Note that we do not use exit() */
   else if(pid > 0)
      /* Parent process:
       * Otherwise, we are in the parent process.
       * Again we count up to ten.
      int i;
      for(i=0; i < 10; i++)
         printf("parent: %d\n", i);
      /* Error handling. */
      fprintf(stderr, "couldn't fork");

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.

[edit] See also

[edit] External links

eu:Fork (sistema eragileak) fr:Fork ja:Fork

Fork (operating system)

Personal tools
what is world wizzy?
  • World Wizzy is a static snapshot taken of Wikipedia in early 2007. It cannot be edited and is online for historic & educational purposes only.