PDA

View Full Version : fork ()



zooro
04-17-2012, 09:11 AM
Consider the following C program.


#include <stdio.h> int main() { int i; for (i=0;i<3;++i) { fork();fork(); } }


How many processes are created when running this program (including the initial one)? Explain

Syscal
04-17-2012, 09:32 AM
I would say 3. One for the parent and one each for the children processes

anduril462
04-17-2012, 09:58 AM
It is not 3. Each child processes picks up exactly where the parent left off in exactly the same state, with the same variables*. Since you have a loop, the parent creates one child with the first fork() call when i == 0, then creates a second one with the second fork() call when i == 0. That first child picks up right after the first fork call, and inherits the parent's value of i, which is, so it also makes the second fork() call when i == 0. The loop executes 3 times with 2 fork() calls in each iteration, for a total of 6 calls to fork(). The parent makes all 6 calls, creating 6 direct children. The first child of the original process doesn't execute the first fork(), but executes the next 5, creating 5 children of it's own. The second child of the original process only makes 4 fork() calls, etc. Each of those children creates their own children (i.e. grandchildren of the first process), etc, until all processes have finished the loop.

You can see that this rapidly creates a large number of processes, growing exponentially. Try drawing the process tree out on paper zooro, at least for a few levels/iterations, and keep track of the value of i for each process. Look for a pattern in how many children each process has.

There's also a way to get the answer with printf, but I wont go into more detail, because you wouldn't learn as much from it.

* There are some differences in the state, such as memory locks, timers and signals, but they don't apply to your simple example. You can read about them in the man page

Syscal
04-17-2012, 10:02 AM
Whoops, didn't see the loop, my mistake.

anduril462
04-17-2012, 10:15 AM
Whoops, didn't see the loop, my mistake.
Yeah, it's hard to read with it all on one line.

@zooro:
Fix your indendtation. You should always preview or review the post you made to make sure it's clear and readable for everybody else. Edit it to fix any problems right away.

zooro
04-17-2012, 10:29 AM
thank u
anduril462 (http://cboard.cprogramming.com/member.php?u=47609) http://im.cprogramming.com/images/statusicon/user-online.png

..... i try to draw .. i am not sure is it 64 ???

ech fork has 2 process

each loop 2 fork ...

loop1 :
2*2=4
4*2=8
loop2:
4*2=8
8*2=16
loop 3:
16*2=32
32*2= 64

anduril462
04-17-2012, 11:26 AM
Yes, 64 sounds right. Here's part of what the process tree would look like (not all branches are fully filled in). The numbers are just to count, not PIDs or anything.


1
+--2
| +--8
| | +--23
| | | +--
| | | | +--
| | | | | +--
| | | | +--
| | | +--
| | | +--
| | +--
| | +--
| | +--
| +--9
| +--10
| +--11
| +--12
+--3
| +--13
| +--14
| +--15
| +--16
+--4
| +--17
| +--18
| +--19
+--5
| +--20
| +--21
+--6
| +--22
+--7


This code is missing a few other potentially critical things:
1. You need to #include <unistd.h> to use the fork() funciton.
1. main should return 0. Not absolutely necessary per the C99 standard, but still a good idea.
2. Each process should wait for it's children to finish. Since processes are executed in an arbitrary order, it's possible a parent terminates before all it's children are done, leaving the child processes as orphans.

Since you seem to have a grasp of it now, the printf trick is just to put a printf after the loop and count how many lines are printed. Add this after the loop:


printf("PID = %d\n", getpid());
while (wait(NULL) >= 0)
;
return 0;

wait() waits for any child to finish, then returns. The loop is because a process may have more than one child, so it says "wait for all my children to finish" before it gets to the return 0 and ends itself. Read the man page (section 2) for details on wait().

zooro
04-17-2012, 11:45 AM
Woow ......anduril462 (http://cboard.cprogramming.com/member.php?u=47609) Thank u very much ....very good explaination .. I think in unix we use sleep() insted of wait () yah ????!!!

anduril462
04-17-2012, 12:15 PM
Read the man pages (section 2) for the sleep and wait functions. Type "man 2 wait" and "man 2 sleep".