2

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!

7
  • It's called both times. Commented Apr 30, 2013 at 12:35
  • fork() returns child's PID in the calling process and 0 in the child process. so it'll keep spawning child processes, which will spawn a child process themselves. right? :) Commented Apr 30, 2013 at 12:38
  • Shark - so does it keep adding children recursively or are they all children of the first process and its children? Commented Apr 30, 2013 at 12:42
  • the master will keep on spawning children, and master->children will spawn one child each. did you try running the code and stepping with a debugger? That's kinda the point of this homework. Commented Apr 30, 2013 at 12:46
  • I did not, too lazy to boot up Linux! Thanks for helping mate :) Commented Apr 30, 2013 at 12:51

2 Answers 2

9

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Quick last question if you guys are still there, do most OSs handle this or will it result in a full process table? Say a user had no cap on processes and called this code, what would happen?
@user2253489 - if there is no limit on the number of processes per user, then AFAIK, you can run this forever. In this example no processes lives for long, they all die shortly after creation so there shouldn't be a problem. But if they all did a while(1) after creation you would eat up all the physical+swap memory on the system, and the out-of-memory (OOM) killer will start killing individual processes. However new ones will be spawned just as fast. It's called a "fork bomb" you can check your fav distro to see if there's anything built in to protect against it.
6
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.