Inter-process communication # Linux # # fork

Engraved (English: fork, also translated as derived , branch ) is a UNIX or UNIX-like bifurcation function, fork function running program is divided into two (almost) exactly the same process, each process starts from a Code thread begins execution of the same position. These two threads in the process to continue, as if two users simultaneously launched two copies of the application.

fork system call for creating a new process, called the child process , it is with the process (called process fork system call) running at the same time, this process is called the parent process . After creating a new child process, the two processes will perform the fork (the next instruction following the system call). Processes using the same pc (program counter), the same CPU registers, the same used in the parent process to open the file.

It takes no arguments and returns an integer value. The following values ​​are different return fork ().

  • Negative : create a child process to fail.
  • Zero : Return to the newly created child process.
  • Value : Returns the parent or the caller. This value includes child process of the newly created ID.

A process, including code, data, and resources allocated to the process. fork () system call function by creating a process nearly identical to the original process, that is, the two processes can do the exact same thing, but different if the initial parameters or variables passed, the two processes can do different things .

  A process calls fork () after the function, the system assigns resources to give new process, such as space to store data and code. Then all the value of the original are copied to the new process of the new process, different values ​​only a few values ​​with the original process. The equivalent of a clone of himself.

We may be familiar with the fork mechanism through a classic case. The following code uses gcc compiler

#include "stdio.h"
#include "sys/types.h"
#include "unistd.h"

int main()
{
    pid_t pid1;
    pid_t pid2;

    pid1 = fork();
    pid2 = fork();

    printf("pid1:%d, pid2:%d", pid1, pid2);
}

For example: after generating run fork_test gcc -o fork_test fork_test.c.

The results assume that the output of the parent process is pid1: 9268, pid2: 9269. Then the execution will run a total of several processes, outputs the results of several other processes and how

According to fork principle (fork function running program is divided into two (almost) exactly the same process, each process started a thread begins execution of code from the same location. These two threads in the process to continue, just as two users simultaneously launched two copies of the application) fork conference program is divided into two parts AB cut two copies, with exactly the same resources.

1) When the process moves to A pid1 = fork (); when creating a copy of B, case A process to obtain pid1 = 9268, B Process A process synchronization resources. pid1 = 0, pid2 = 0

  • A:pid1=9268, pid2=0
  • B:pid1=0, pid2=0

2) When the process moves to A pid2 = fork (); when creating a copy of the C, this time to give A process pid2 = 9269, C A process synchronization process resources. pid1 = 9268, pid2 = 0

  • A:pid1=9268, pid2=9269
  • B:pid1=0, pid2=0
  • C:pid1=9268, pid2=0

3) When the process moves to B pid2 = fork (); when creating a copy of the B1, Process B at this time to give pid2 = 9270, B1 B process synchronization process resources. pid1 = 0, pid2 = 0

  • A:pid1=9268, pid2=9269
  • B:pid1=0, pid2=9270
  • C:pid1=9268, pid2=0
  • B1:pid1=9268, pid2=0

Program results are as follows:

 

Published 170 original articles · won praise 207 · Views 4.59 million +

Guess you like

Origin blog.csdn.net/xiaoting451292510/article/details/103766712