A process executes the code for(i = 1; i <= 3; i++) fork(); fork(); The number…
2023
A process executes the code
for(i = 1; i <= 3; i++)
fork();
fork();
The number of new processes created is _____ (No space between the lines in given code)
- A.
64
- B.
63
- C.
16
- D.
15
Attempted by 59 students.
Show answer & explanation
Correct answer: D
Above code will execute a system call fork() four times.
Without curly braces, only the first statement following the loop definition is considered to belong to the loop body. Hence, 1st fork() statement will get executed 3 times.
for(i = 1; i <= 3; i++)
fork();// executes fork() 3 times
fork();// executes fork() 1 time
The number of child processes created is 2n - 1. i.e. 24 - 1 = 15.