GATE CS 2024 Set 1 — Question 57
NAT+2 / -0HardProcess Creation (fork, exec)Processes & ThreadsOperating System
Operating System → Processes & Threads → Process Creation (fork, exec)
Last updated
Question
Consider the following code snippet using the The total number of times the
fork() and wait() system calls. Assume that the code compiles and runs correctly, and that the system calls run successfully without any errors.int x = 3;
while(x > 0) {
fork();
printf("hello");
wait(NULL);
x--;
}
printf statement is executed is _______Correct answer
14 to 14
Solution
In each iteration of the
while loop, the fork() system call is executed, which doubles the number of processes. After the fork, both the parent and the child process execute the printf("hello") statement.Let be the number of processes at the start of iteration . - Iteration 1 (): . After
fork(), there are processes. Both print "hello". Total prints in this iteration = . - Iteration 2 (): . After
fork(), there are processes. All print "hello". Total prints in this iteration = . - Iteration 3 (): . After
fork(), there are processes. All print "hello". Total prints in this iteration = .
printf is executed = .Note: The wait(NULL) call in the child process returns immediately (as it has no children), while in the parent process, it waits for the child to finish. This affects the order of execution but does not change the total count of printf calls.More questions on Processes & Threads
2024 Set 1 Q24Which of the following statements about threads is/are TRUE?2024 Set 2 Q24Which of the following tasks is/are the responsibility/responsibilities of the memory management…2024 Set 2 Q25Consider a process P running on a CPU. Which one or more of the following events will always…2024 Set 1 Q25Which of the following process state transitions is/are NOT possible?2024 Set 2 Q37Consider a single processor system with four processes A, B, C, and D, represented as given below,…
Practice GATE CS PYQs with adaptive difficulty
Timed practice, skill tracking, and AI explanations — free to start.
Start practicing free