Fork (system call)

When system call fork the current process creates a copy of itself, which then runs as a child process of generating program. The child process receives the data, the machine code and the instruction pointer from the parent process and by the operating system (such as the parent process and any other process also ) has its own process number, the PID ( engl. "Process IDentifier "). As a result, the operating system manages the child process as a separate instance of the program and executes it independently from the parent process.

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

At the return value of fork () is detected, in which process you are. Provides fork () return 0, this indicates the child, the parent process is the PID of the child is returned. If an error occurs, fork ( ) returns a value less than 0 and no child process is created.

Example

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

# include # include # include   int main () {     int pid, j, i;       pid = fork ();       if ( pid == 0)     {        / * Child process         * If fork returns a 0, we are in the child process         * /        for (j = 0; j < 10; j )        {   printf (" child process:% d ( PID: % d) \ n", j, getpid ());          sleep (1 );        }        exit ( 0);     }     else if ( pid > 0)     {        / * Parent process         * Indicates fork returns a value greater than 0, we are in the parent process         * Pid is the ID of the child process         * Getpid ( ) returns the own pid         * /        for (i = 0; i < 10; i )        {           printf (" parent: % d (PID: % d) \ n", i, getpid ());           sleep (1 );        }     }     else     {        / * If a negative value is returned, an error occurred * /        fprintf (stderr, "Error" );        exit (1 );     }     return 0; } Possible output from the program

Child process: 0 (PID: 11868 ) Parent: 0 (PID: 11867 ) Child process: 1 ( PID: 11868 ) Parent: 1 (PID: 11867 ) Child process: 2 (PID: 11868 ) Parent: 2 (PID: 11867 ) Child process: 3 (PID: 11868 ) Parent: 3 (PID: 11867 ) Child process: 4 (PID: 11868 ) Parent: 4 (PID: 11867 ) Child process: 5 (PID: 11868 ) Parent: 5 (PID: 11867 ) Child process: 6 (PID: 11868 ) Parent: 6 (PID: 11867 ) Child process: 7 (PID: 11868 ) Parent: 7 (PID: 11867 ) Child process: 8 (PID: 11868 ) Parent: 8 (PID: 11867 ) Child process: 9 (PID: 11868 ) Parent: 9 (PID: 11867 ) The order of the outputs can vary, because the operating system decides on the basis of different criteria at run time which process is when and for how long executed on the processor (scheduling). Among other things, play the current utilization of computing resources, competing processes (system such as application programs) that already consumed CPU time or wait time endured a role. From this information, the priority of a process is repeatedly re-evaluated. The output process IDs are reassigned Forkaufruf at each program and and are therefore only indicative.

Terms of Fork to launch other programs

Fork is also used to (so no copies of the calling program ) to start other programs. To this end, calls for the Fork one of the processes (usually the child process ) an appropriate command (eg execve ), whereby the calling process is replaced by the desired program. An example: the user has a shell open and want to display using the command "ls" the current directory content. So he typed

Ls one. What will occur ( in simplified form ) the following:

  • The shell calls fork () and thereby produces as described above a new child process ( a copy of itself )
  • The child process newly created now calls the execve command ( "ls "). This causes the child process will be replaced by ls program
  • So the newly created ls- process (which is still a child of the shell) is executed

The use of fork () allows the child process to adjust its file descriptors before the call to execve (), thus for example, the input-output (stdin, stdout, stderr) redirect.

342396
de