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 calling main recursively as in: f(): "hi"; f()?

share|improve this question
    
This question appears to be off-topic because it is purely a C issue and has nothing to do with *nix. –  terdon Aug 7 at 13:27
    
@terdon You could argue programming questions are off-topic, but you can't really say fork is "purely C" since it is a system call primarily associated with *nix and POSIX operating systems, with behavior that happens to "break" the normal rules of "pure" C. You won't find any mention of "fork" in the C standard. –  jw013 Aug 7 at 13:37
    
@jw013 I am about as far from a C expert as it is possible to be so I may very well be wrong. Are you saying that any code that contains fork is by definition one that will only run on *nix? So questions about fork specifically should be on topic? –  terdon Aug 7 at 13:38
    
@terdon "any code that contains fork is by definition one that will only run on *nix" - There may be rare exceptions or corner cases I am unaware of, but this is the general rule. –  jw013 Aug 7 at 13:41
1  
@jw013 fair enough, question reopened. Thanks for the pointer (no pun intended). –  terdon Aug 7 at 13:43

2 Answers 2

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
1  
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
                           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

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.