0

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?

5
  • It's not 4 groups of 2x, it's 2 groups of 4x. First, the process splits into 4 processes and one of them prints "Hello". Then, each one splits into 4 processes and one of them prints "Hello" Commented Jul 2, 2020 at 11:49
  • @Gerhardh No? First, the process splits into 4 processes and one of them prints "Hello". Commented Jul 2, 2020 at 12:08
  • @user253751 Maybe I misinterpret your comment. Not 4*4 but 4+4. Commented Jul 2, 2020 at 12:15
  • 4
    You should never write code such as fork() == fork(), so it isn't meaningful to ponder about what it will do. It's the same category of questions as "what happens if I toss a handful of nails into the microwave oven" or "what happens if I put my cat in the washing machine"... please just never do that. Just pondering about it is disturbing in itself. Commented Jul 2, 2020 at 12:51
  • @Gerhardh The first if(fork()==fork()) splits into 4 and one of the prints "Hello". The second if(fork()==fork()) splits into 4 and one of them prints "Hello". If you are just looking at the number splits, you might think that the second and third calls to fork() have some relation to each other, and therefore end up with the number 7. They do not. Commented Jul 2, 2020 at 13:01

2 Answers 2

2

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"); 
Sign up to request clarification or add additional context in comments.

2 Comments

ok, i understand what you are saying, but i am having troubles understanding the splits. so for: ``` if (fork()==fork()) printf("Hello\n");
@uValerion if(fork() == fork()) calls fork(), then it calls fork(), then it checks whether the two calls returned the same value
1

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.