bengika
bengika

Reputation: 63

I don't understand this diagram of fork()

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);

Upvotes: 5

Views: 5772

Answers (4)

Manlio
Manlio

Reputation: 10865

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.

Upvotes: 3

asaelr
asaelr

Reputation: 5456

fork return -1 on error, and 0 or positive else, so the line if (pid < 0) break; says "if there was error, exit from the loop".

Assuming that there is not error, it's something like:

At the beginning, i=0, and you have one process. let's call it p0.

In the line fork();, p0 creates another process. let's call it p1.

In everyone of them, we have i++ (so now i is 1), and we are iterating the loop again.

p0 and p1, separately, have a fork(); command, so everyone of them creates another process. let's call the new processes p2 and p3.

Now, in every process, we have i++, that set i to be 2, and we run the loop again.

Everyone of the 4 processes we have, run the line fork();, and creates a new process. so now we have also p4,p5,p6,p7.

Every process increase its i to 3, and then, since the loop condition is now false, the loop finally ends.

Now, the 8 process arrive (separately) to the next line.

(In fact, every iteration double the number of processes, so if you change the 3 to, for example, 15, you will have 2^15 processes at the end.)

Upvotes: 2

Timothy Jones
Timothy Jones

Reputation: 22125

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

Upvotes: 8

Kevin
Kevin

Reputation: 25259

fork returns -1 if the fork call failed. it returns the pid in parent and 0 in the child. The condition you're looking at doesn't really matter to the functioning of the code; it's just saying if there's an error with fork then exit the loop. If there's no error in the fork call then the process tree in your diagram will be built.

The reason why is that the same loop will continue running in the child processes. So the children will also continue to fork based on the value of i at the time fork was called.

Upvotes: 2

Related Questions