Fork (Unix)

from Wikipedia, the free encyclopedia

In Unix-like operating systems , fork is the name of a system call which the calling process (parent process) uses to create a copy of itself, a so-called child process. The child process takes over the data , the code , the command counter and the file descriptors from the parent process and receives its own process number, the PID (“ P rocess ID entifier”) from the kernel (like the parent process and every other process ). As a result, the operating system manages the child process as an independent instance of the program and executes it independently of the parent process.

A child process usually does not continue to work exactly like the parent process, but chooses different code paths (different instructions).

The return value of fork () recognizes which process you are currently in. If fork () returns a 0, this marks the child process; the child's PID is returned in the parent process. In the event of an error, fork () returns a value less than 0 and no child process was created.

example

The following program is written in the C programming language and is intended to show how a fork works. The sample program counts from 0 to 9 and outputs the value of the respective process and its individual process ID.

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>

int main ()
{
   int i, j;
   pid_t pid;

   pid = fork();

   if (pid == 0)
   {
      /* Kindprozess
       * wenn fork() eine 0 zurückgibt, befinden wir uns im Kindprozess
       */
      for (j=0; j < 10; j++)
      {
        printf ("Kindprozess:  %d (PID: %d)\n", j, getpid());
        sleep (1);
      }
      exit (0);
   }
   else if (pid > 0)
   {
      /* Elternprozess
       * Gibt fork() einen Wert größer 0 zurück, befinden wir uns im Elternprozess
       * in pid steht die ID des Kindprozesses
       * getpid() gibt die eigene PID zurück
       */
      for (i=0; i < 10; i++)
      {
         printf ("Elternprozess: %d (PID: %d)\n", i, getpid());
         sleep (1);
      }
   }
   else
   {
      /* Wird ein negativer Wert zurückgegeben, ist ein Fehler aufgetreten */
      fprintf (stderr, "Fehler");
      exit (1);
   }
   return 0;
}

Possible output of the program:

Kindprozess:  0 (PID: 11868)
Elternprozess: 0 (PID: 11867)
Kindprozess:  1 (PID: 11868)
Elternprozess: 1 (PID: 11867)
Kindprozess:  2 (PID: 11868)
Elternprozess: 2 (PID: 11867)
Kindprozess:  3 (PID: 11868)
Elternprozess: 3 (PID: 11867)
Kindprozess:  4 (PID: 11868)
Elternprozess: 4 (PID: 11867)
Kindprozess:  5 (PID: 11868)
Elternprozess: 5 (PID: 11867)
Kindprozess:  6 (PID: 11868)
Elternprozess: 6 (PID: 11867)
Kindprozess:  7 (PID: 11868)
Elternprozess: 7 (PID: 11867)
Kindprozess:  8 (PID: 11868)
Elternprozess: 8 (PID: 11867)
Kindprozess:  9 (PID: 11868)
Elternprozess: 9 (PID: 11867)

The order of the outputs can vary, as the operating system decides on the basis of various criteria at runtime which process is to be executed on the processor when and for how long ( scheduling ). Among other things, the current utilization of computer resources, competing processes (system and application programs), the computing time that has already been used or the waiting time that has been endured play a role. The priority of each process is re-evaluated again and again from this information. The process IDs issued are reassigned each time the program and fork are called and are therefore only examples.

Using fork to start other programs

Fork is also used to start other programs (i.e. not copies of the calling program). For this purpose, after the fork, one of the processes (usually the child process) calls a corresponding command (e.g. execve ), which replaces the calling process with the desired program. An example: the user has opened a shell and would like to use the command lsto display the current directory content. So he's typing

 ls

a. The following then happens (simplified):

  1. The shell calls fork () and creates a new child process (a copy of itself) as described above.
  2. The newly created child process now calls the execve ("/ bin / ls") command . This replaces the child process with the program ls .
  3. The newly created ls process (which is still a child of the shell) is executed.

The use of fork () allows the child process to adapt its file descriptors before calling execve () , in order to redirect the input / output ( stdin, stdout, stderr ) , for example .

See also

Web links

Individual evidence

  1. ^ Andrew S. Tanenbaum: Modern Operating Systems . Second edition. ISBN 0-13-092641-8 , p. 75