Thread: fork ()

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    15

    fork ()

    Consider the following C program.

    Code:
    #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

  2. #2
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    I would say 3. One for the parent and one each for the children processes

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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

  4. #4
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    Whoops, didn't see the loop, my mistake.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Syscal View Post
    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.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    15
    thank u
    anduril462 http://im.cprogramming.com/images/st...ser-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

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    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.
    Code:
    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:
    Code:
    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().

  8. #8
    Registered User
    Join Date
    Apr 2012
    Posts
    15
    Woow ......anduril462 Thank u very much ....very good explaination .. I think in unix we use sleep() insted of wait () yah ????!!!

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Read the man pages (section 2) for the sleep and wait functions. Type "man 2 wait" and "man 2 sleep".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with fork
    By jmike72 in forum C Programming
    Replies: 11
    Last Post: 03-25-2012, 12:36 PM
  2. Fork()
    By Drainy in forum C Programming
    Replies: 1
    Last Post: 11-13-2009, 10:08 AM
  3. How to use fork()!
    By ioat in forum C Programming
    Replies: 10
    Last Post: 06-27-2009, 07:07 AM
  4. y does it run fork twice
    By raja9911 in forum C Programming
    Replies: 3
    Last Post: 02-03-2006, 03:08 AM
  5. fork() ???
    By Devil Panther in forum Linux Programming
    Replies: 7
    Last Post: 09-20-2003, 08:16 AM