I have such a short code example, it was given to us to demonstrate the operation of the fork () function:

#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> int main () { pid_t pid; pid = fork(); pid = fork(); printf("Fork-Test\n"); return EXIT_SUCCESS; } 

I do not quite understand the result. As a result, I have 4 times wrote Fork-Test in the terminal. I don’t understand, first, why it happens more than once, because I call the printf("Fork-Test\n"); method printf("Fork-Test\n"); only once, secondly, once already several times, why exactly 4? Moreover, in the following form:

 Fork-Test Fork-Test Process returned 0 (0x0) execution time : 0.007 s Press ENTER to continue Fork-Test Fork-Test 

I do not understand the meaning. I will be grateful for any explanations about fork()

    2 answers 2

    The process after the fork system call , forks , an identical twin child in the identical state (well, almost) is created at the initial process. The created process will be busy executing the same code from exactly the same point as the original process.

    It is possible to distinguish who created and who created, by returning the value of fork , so its result is usually passed to if , so that these processes do some different things, one goes to the if branch, the other to the else branch.

    You ignore the return value (save, but do not use it), and therefore both processes continue along the same path. And stumble upon another fork call. And each of them forks again . It turns out the following picture:

      P # перед первым pid = fork(); [fork 1] P # перед вторым pid = fork(); \ P(1) # перед вторым pid = fork(); [fork 2] P # перед printf("Fork-Test\n"); / \ P(2) P(1) # перед printf("Fork-Test\n"); # перед printf("Fork-Test\n"); \ P(1)(2) # перед printf("Fork-Test\n"); 

    After the second fork of the processes already 4. And after these forks, each process comes to a call to printf and prints the specified string. Each process does it on its own. Therefore, the output occurs as many times as there were processes.

    You can verify this by placing the output before the second fork (the output will occur twice) or before the first (once).

    They are completed independently of each other.
    You got that process P finished second. And something through which you started this process, monitored only the process P , but not its “clones” (since they have their own pid , process ID), therefore there is no such conclusion for the rest.

    • some kind of completely incomprehensible diagram turned out - Grundy
    • @Grundy The main thing to understand is that they breed by the method of vegetative propagation. :) - Vlad from Moscow

    The difficulty in understanding fork is that all the processes being run deal with the same written code.

    When you called fork first time

     #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h> int main () { pid_t pid; pid = fork(); // после первого вызова fork pid = fork(); printf("Fork-Test\n"); return EXIT_SUCCESS; } 

    That part of your program that is located after the comment is already being executed by two processes. True, each of the processes has its own address space. You can present it as follows.

     Родительский процесс Дочерний процесс ==================== ================ pid = fork(); pid = fork(); printf("Fork-Test\n"); printf("Fork-Test\n"); return EXIT_SUCCESS; return EXIT_SUCCESS; } } 

    Now each of the processes encounters the next fork call in its path. Therefore, there are two more processes. And all four processes are carried out by everyone in their address space.

     printf("Fork-Test\n"); return EXIT_SUCCESS; } 

    The documentation in the description of the fork is written

     fork() creates a new process by duplicating the calling process. The new process is referred to as the child process. The calling process is referred to as the parent process. The child process and the parent process run in separate memory spaces. **At the time of fork() both memory spaces have the same content.** 

    Pay attention to the last sentence of the quote. This means that each process has, roughly speaking, the same source code of your program, the execution of which for a new process begins after the fork call clause.