uValerion
uValerion

Reputation: 11

When does fork() == fork()?

I have read online that in the code below a child process will create another child process and Hello will be printed once.

if (fork()==fork())
    printf("Hello\n");

So the father (0) will create 2 child processes (1),(2) and child (1) will create another child process (3). So only child 1 has a child and in the output Hello will indeed be printed once although there will be 4 processes in total.

But what about the next code:

int i;
for (i=0; i<2; i++)
    if (fork()==fork())
        printf("Hello\n");

I know there are 16 running processes in total. If i count using the same method as before then there are 7 child processes that each have a child process, but the output will be only 5 times "Hello". can anyone please explain this?

Upvotes: 0

Views: 770

Answers (2)

Ian Abbott
Ian Abbott

Reputation: 17403

Label the 16 processes A_ through P_. Without loss of generality, assume that the left hand side of the equality operator is evaluated before the right hand side.

The 16 processes and loop iterations can be summarized by the following table:

PID     PPID    i       Left    Right   L==R
----    ----    -       ----    -----   ----

A_      n/a     0       B_      C_      0
A_      n/a     1       D_      E_      0

B_      A_      0       0       F_      0
B_      A_      1       G_      H_      0

C_      A_      0       B_      0       0
C_      A_      1       I_      J_      0

D_      A_      1       0       K_      0

E_      A_      1       D_      0       0

F_      B_      0       0       0       1
F_      B_      1       L_      M_      0

G_      B_      1       0       N_      0

H_      B_      1       G_      0       0

I_      C_      1       0       O_      0

J_      C_      1       I_      0       0

K_      D_      1       0       0       1

L_      F_      1       0       P_      0

M_      F_      1       L_      0       0

N_      G_      1       0       0       1

O_      I_      1       0       0       1

P_      F_      1       0       0       1

As can be seen, exactly 5 of the processes have the equality evaluating to 1, when both sides are equal to the return value 0 from fork().

There is a very unlikely possibility that some of the processes may end up with the same process number due to process numbers being recycled after death of a process.

Upvotes: 1

So you have determined that this code:

if (fork()==fork())
    printf("Hello\n");

splits 1 process into 4, and one of them prints "Hello".

If we do it twice:

if (fork()==fork())
    printf("Hello\n");

if (fork()==fork())
    printf("Hello\n");

then first 1 process splits into 4 and one of them prints "Hello". Then each of those 4 processes splits into 4 (so there are 16 in total), and 1 from each split prints "Hello". 5 "hello"s in total. Do you understand so far?

Now, putting it in a loop makes no difference:

int i;
for (i=0; i<2; i++)
    if (fork()==fork())
        printf("Hello\n");

is exactly the same as

if (fork()==fork())
    printf("Hello\n");

if (fork()==fork())
    printf("Hello\n");

Upvotes: 2

Related Questions