Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I don't understand this diagram of fork()

Tags:

c

fork

unix

enter image description here

How we can get this process with this condition??schema of process?

    int main (int argc, char **argv) {
    int i;
    int pid;
       for (i= 0; i < 3; i++) {
            pid = fork();

            if (pid < 0) break;// with this condition i dont understand??

       }
           while (wait(NULL) != -1);
like image 296
bengika Avatar asked Feb 13 '12 23:02

bengika


People also ask

What does fork () means?

System call fork() is used to create processes. It takes no arguments and returns a process ID. The purpose of fork() is to create a new process, which becomes the child process of the caller. After a new child process is created, both processes will execute the next instruction following the fork() system call.

How does fork process work?

In the computing field, fork() is the primary method of process creation on Unix-like operating systems. This function creates a new copy called the child out of the original process, that is called the parent. When the parent process closes or crashes for some reason, it also kills the child process.

What is fork () in C?

This fork system call is used to create a new process. This newly created process is known as child process. The current process which is creating another child process is called the parent process. A child process uses the same program counter, CPU register, same files that are used by the parent process.

What happens when fork is called 3 times?

If the parent and child keep executing the same code (i.e. they don't check the return value of fork() , or their own process ID, and branch to different code paths based on it), then each subsequent fork will double the number of processes. So, yes, after three forks, you will end up with 2³ = 8 processes in total.


2 Answers

fork() splits a process in two, and returns 0 (if this process is the child), or the PID of the child (if this process is the parent), or -1 if the fork failed. So, this line:

if (pid < 0) break;

Says "exit the loop if we failed to create a child process".

The diagram is a little confusing because of the way the processes (circles) correspond to the fork() calls in the loop. The three child processes of the main process are created when i is 0, 1, and 2 respectively (see the diagram at the bottom of this post).

Since the loop continues in both the parent and the child process from the point fork was called, this is how the forks happen:

  • i == 0: fork is called in the original parent. There are now two processes (the top one, and the left one).
  • i == 1: fork is called in the two existing processes. New children are the leftmost child on the second layer from the bottom, and the middle child on the third layer from the bottom. There are now four processes
  • i == 2: fork is called in all existing processes. New children are all remaining nodes (the bottom node, the two rightmost nodes in the second layer from the borrom, and the rightmost node in the third layer from the bottom)
  • i == 3: All 8 processes exit the loop

Here is the diagram again, with numbers indicating what the value of i was in the loop when the process was created:

                 -1  <--- this is the  parent that starts the loop
              /   |  \
             0    1   2
           /  \   |
          1    2  2
          |
          2
like image 127
Timothy Jones Avatar answered Sep 27 '22 21:09

Timothy Jones


To understand your diagram you must rely on the behavior of fork: it splits the process in two, creating another process identical to the first (except for the PID) in a new memory location.

If you call it in a loop that's what happen:

When i=0 the first process will be split, creating another process that will start running from exactly this point on (so will skip the first loop). Focusing on the first process, it will continue the loop, generating another process when i=1. The second process, thus, will start from i=1, so will skip the first two loops. The first process will be split last time for i=2. The last copy created, however, will start running from i=2, so it will exit the loop and will not generate anything.

The first copy created will start the loop from i=1, generating two process, while the second copy will start from i=2, generating only one copy.

You can continue this reasoning and understand the rest of the diagram.

As others pointed out, if (pid < 0) is just a check to see if there are errors and does not modify the logic of the code.

like image 37
Manlio Avatar answered Sep 27 '22 21:09

Manlio