Assuming that the system call fork() never fails, consider the following C programs P1 and P2 executed on a UNIX/Linux system.
| P1 | P2 |
|---|---|
| int main() { fork(); fork(); fork(); printf("Happy\n"); } | int main() { fork(); printf("Happy\n"); fork(); printf("Happy\n"); fork(); printf("Happy\n"); } |
Statement II: P2 displays "Happy" 12 times.
In light of the above statements, choose the most appropriate answer.
Pick an option to check yourself, or open the answer below.
Show answer & explanation
Correct answer: C. Statement I is correct but Statement II is false
Explanation
In P1, three fork() calls run back-to-back with no branching, so the process count doubles at each call: 1→2→4→8; all 8 processes then execute the single printf, so "Happy" is printed 8 times — Statement I is correct. In P2, the printf calls are interleaved between the fork() calls, so the process count reaching each printf is itself doubling: 2 processes execute the first printf, 4 execute the second, and 8 execute the third, giving a total of 2+4+8 = 14 prints, not 12 — Statement II is false. Hence option (iii).More Discrete Structures and Optimization questions
- Consider the following four processes with the arrival time and length of CPU burst given in milliseconds: Process Arrival Time Burst Time…
- Which module gives control of the CPU to the process selected by the short-term scheduler?
- A fair share scheduler computes the priority of a process based on which of the following? (1) Processor utilization by the individual proc…
- Which of the following CPU scheduling algorithms may cause starvation of a process? (1) First-Come-First-Served (2) Round Robin (3) Priorit…
- A scheduling algorithm assigns a process's priority proportional to how long it has been waiting; every process starts at priority zero (th…
- Match List I with List II List I List II