Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
    fork();
    fork();
    fork();

    puts("hi");

    return 0;
}

This program prints 8 times hi and exits, Why? Is not each fork calls main recursively as in: f(): "hi"; f()

share|improve this question
add comment

2 Answers

                           main()
                             |
                             |
                      fork()  ------------------
                              |                 |
                     fork()   -------        -------
                              |     |        |      |
                   fork()   ----   ----     ---    ---
                            |  |   |   |    |  |   |  |

This is the process tree of above code that's how the fork() works.and why its printing 8 means its always 2 power n ( where n is number of call i.e fork().

share|improve this answer
add comment

No, fork is not "recursive" in the traditional meaning of recursion. A call to fork() duplicates the current process so it "returns twice". For the child process, the return value is 0, and for the parent the return value is the child PID. fork() does not restart main - that would be more like fork followed by exec.

Your program works like this. First let's insert some line numbers:

 1: #include <stdio.h>
 2: #include <stdlib.h>
 3: #include <string.h>
 4:
 5: int main(int argc, char *argv[])
 6: {
 7:    fork();
 8:    fork();
 9:    fork();
10:
11:    puts("hi");
12:
13:    return 0;
14: }

Let's say you start your program and it gets a PID of P.

  • After line 7, the call to fork results in two separate processes, both at line 8, one with PID P (the original) and one with a pid of C1 (the new child).

  • P runs line 8 and spawns a new child, with PID C2. Both P and C2 are at line 9 now. Meanwhile, C1 runs line 8 and spawns a new child, CC1. Both CC1 and C1 are at line 9 as well. (Not entirely relevant, but the order in which the above two sentences occur is indeterminate. They could happen simultaneously on multi-processor systems.)

There are now a total of 4 processes: P, C1, C2, and CC1. As you can see, each successive fork doubles the amount of processes. Since there are 3 fork calls, you end up with 23 or 8 processes. The genealogy looks something like:

P (initial process, started by you)
+-- C1 (created on line 7)
|   +-- CC1 (created on line 8)
|   |   +-- CCC1 (created on line 9)
|   +-- CC2 (created on line 9)
+-- C2 (created on line 8)
|   +-- CC3 (created on line 9)
+-- C3 (created on line 9)

Each process is created by its parent in the tree.

share|improve this answer
    
I think the misconception that the OP had was the fork started at the beginning of main again. This answer clearly illustrates that fork creates two processes at the same location. (If you were trying to create a fork bomb you need something like this while (1) fork();). –  Kevin Cox Nov 30 '12 at 17:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.