dahui
dahui

Reputation: 2166

Please help explain this C/linux code using fork()

this is a past paper exam question which I am having trouble solving. For four marks:

Consider the following C fragment which employs the fork() system call found in Unix systems:

while (fork()==0) {
  if (fork()==0) break;
}

Draw a diagram (in the form of a tree) that clearly illustrates the parent-child structure that would be generated by executing this code. What would be the ultimate effect of running this code in practice?

I think that it continually creates child processes, but I can't explain it in the context of the code. Does the while continually call fork each time or is it just called in the if statement each time?

Thanks!

Upvotes: 2

Views: 1730

Answers (2)

Mike
Mike

Reputation: 49363

A while loop will run until the condition in the parentheses is false. In this case that would be: until the return value from fork() is not equal to 0.

fork()'s return is:

On success, the PID of the child process is returned in the parent, and 0 is returned in the child. On failure, -1 is returned in the parent, no child process is created, and errno is set appropriately.

So once fork executes successfully there are 2 processes (the parent and one new child). One of them will return 0 (the child process), the other (the parent process) will return some other value (the PID of the child).

So this tells you that the code runs forever as each child process will continue to execute the while() loop.

Say your parent is PID=0, first child is PID=1, etc. The first run of the code would be something like:

while(fork()==0)  // now there are two processes, the child which returns 0 and the
{                 // parent which returns 1 (the new child's pid)

    if(fork() == 0) break;  // this code is now only executed by the child (1) as the 
                            // parent already left the while loop since it's return was
                            // not == 0
                            // once this code executes there are 2 processes running
                            // child(1) will return child(2)'s PID, child(2) will
                            // return 0 and will enter the if and break from the loop 
    // now here it's only the original child(1) running again
}

So you'll end up with something like:

0 is the original parent, it makes a child and leaves
1 is the original child, it creates a child(2) and then goes to the while loop again
     where it generates 3 before leaving
2 is the child of 1 where it leaves because of the break in the if
3 now hits the if and generates 4 (which leaves because of the break)
3 now goes back to while and generates 5 then leaves, 
etc
etc
              +--> 4
              |
              | (if)
              |
0 --> 1 -+--> 3 ---> 5
         |
         |(if)
         |
         +--> 2

† - These are not realistic PIDs to get while running your user space code on any modern Linux distro, however they're easier to read then a real random string so I'm using them.

Upvotes: 9

Barath Ravikumar
Barath Ravikumar

Reputation: 5836

while (fork()==0) {
  if (fork()==0) break;
}

Before the first line, say there is process with id 6282.

After executing

while (fork()==0)   

Now, there are two processes, the original one with id 6282, and a new process with another id 6283 (say), now the second process is said to be the child of the original process. Now only the child process of id 6283 enters the loop, since in the child context the return value of fork() is zero , whereas in the parent context, the return value is the process id of the child created.

The second statement below is executed by the process 6283

if (fork()==0) break;

Now child process of of 6283 will execute the break statement, due to the above said reason that the return value of fork() is zero in the child context, and hence the child process of 6283 breaks from the loop.

Now the next iteration of the while() loop is executed by only by process 6283.

Upvotes: 6

Related Questions