Home c How the Fork () function works in C. I can not understand...

How the Fork () function works in C. I can not understand the result of the program of the program

Author

Date

Category

I have such a short example code, we were given it to demonstrate the work of the Fork () function:

# include & lt; stdio.h & gt;
#Include & lt; stdlib.h & gt;
#Include & lt; unistd.h & gt;
#Include & lt; sys / Types.h & gt;
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 in the terminal writes fork-test
I do not understand, firstly, why it happens more than once, because I call the printf method ("Fork-Test \ N"); only once, secondly, once already several times, then why exactly 4? With that also 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.

Meaning does not understand me.
I will be grateful for any explanations about fork ()


Answer 1, Authority 100%

process after system call FORK , splits , the source process creates an identical descendant-double in an identical condition (not almost). The created process will be engaged in performing the same code exactly from the same point as the source process.

Discern who created, and who was created, can be on the return value of fork , so its result is usually transmitted to if so that these processes perform some different things, one I went to the branch if , the other in the branch ELSE .

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

p # before the first PID = FORK ();
 [FORK 1]
   P # before the second pid = fork ();
   \
    P (1) # in front of the second PID = FORK ();
 [FORK 2]
   P # in front of Printf ("Fork-test \ n");
  / \
P (2) P (1) # before printf ("Fork-test \ n"); # Before printf ("Fork-Test \ N");
      \
      P (1) (2) # in front of PrintF ("Fork-test \ n");

After the second fork of the processes already 4. And after these Forks each process comes to call PRINTF and displays the specified string. each process does it yourself. Therefore, the output takes as many times as the processes.

You can make sure that by placing the output to the second fork (the output will occur twice) or before the first (once).

they are completed independently of each other.
You have it it turned out that the process of P ended the second. And something that you started this process, followed only behind the p process, but not by his “clones” (since they own PID , Process id), Therefore, there is no such output for the rest.


Answer 2, Authority 110%

Difficulty of Understanding Fork It is that all the launched processes deal with the same written code.

When you called Fork First time

# include & lt; stdio.h & gt;
#Include & lt; stdlib.h & gt;
#Include & lt; unistd.h & gt;
#Include & lt; sys / Types.h & gt;
INT MAIN () {
PID_T PID;
PID = FORK ();
// after the first call fork
PID = FORK ();
PrintF ("Fork-Test \ N");
Return EXIT_SUCCESS;
}

The one part of your program, which is located after the comment, is performed by two processes. True, each of the processes has its address space. You can submit this as follows

parent process
==================== ================
PID = FORK (); PID = FORK (); 
PrintF ("Fork-Test \ N"); PrintF ("Fork-Test \ N");
Return EXIT_SUCCESS; Return EXIT_SUCCESS;
}}

Now each of the processes meets on its path the following call Fork . Therefore, two more processes appear. And all four processes perform each in its address space offer

printf ("fork-test \ n");
Return EXIT_SUCCESS;
}

in the documentation in the description of fork written

fork () Creates a New Process by Duplicating the Calling Process. Their
    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 offer from the quote. This means that each process has, roughly speaking, the same source code of your program, the execution of which for the new process begins after the offer with the call Fork .

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions