18 18 votes Consider the following program snippet. Assume that the program compiles and runs successfully. Further, assume that the fork() system call is always successful in creating a process.int main () { int i; for (i = 0; i < 3; i++) { if (fork() == 0) { continue; } break; } printf("Hello!"); return 0; }The total number of times that the printf statement gets executed is $\_\_\_\_$. (answer in integer) Operating System gatecse-2026-set1 numerical-answers operating-system fork-system-call two-marks + – gatecse 3.4k views answer comment Share Follow Print See all 2 Comments 2 2 Comments reply Het2312 commented Jun 25 reply Follow flag Diagram to visualize how each process is created and where it eventually prints. 5 5 replyShare Sarah Sayed commented Aug 13 reply Follow flag if (fork() == 0) -----> here fork function is called and its return values those of parent and child processes are being tested (reminder for me ) 0 0 replyShare Please log in or register to add a comment.
8 8 votes At i=0: The original parent process forks.The parent gets a non-zero return value, fails the "if"condition, run "break", and prints Hello! (1st print).The child gets a 0 return value, executes "continue", and moves to i=1. At i=1: The child process forks again.The parent (child of the first step) hits "break" and prints Hello! (2nd print).The new child executes "continue" and moves to i=2. At i=2: That child forks a last time.The parent hits "break" and prints Hello! (3rd print).The final child executes "continue", which increments i to 3. Since the loop condition i < 3 is now false, it exits the loop naturally and prints Hello! (4th print) Anurag_Patwa answered Jun 18 Anurag_Patwa comment Share Follow See 1 comment 1 1 comment reply Baki Hanma commented Sep 2 reply Follow flag thnx buddy... 1 1 replyShare Please log in or register to add a comment.
1 1 vote Navya Charitha answered Mar 1 Navya Charitha comment Share Follow 0 reply Please log in or register to add a comment.
1 1 vote The key here is how the if-else logic inside the loop interacts with the fork() return values:fork() == 0 (Child): Executes continue, meaning it goes to the next iteration of the loop.fork() > 0 (Parent): Executes break, meaning it exits the loop immediately and goes to the printf.Step-by-Step TraceStart: We begin with 1 Parent Process ($P_0$) at $i=0$.Iteration $i = 0$: * $P_0$ forks.Parent ($P_0$): Receives a positive PID, hits break, exits loop $\rightarrow$ Prints "Hello!" (1).Child ($C_1$): Receives 0, hits continue, goes to $i = 1$.Iteration $i = 1$:$C_1$ forks.Parent ($C_1$): Receives a positive PID, hits break, exits loop $\rightarrow$ Prints "Hello!" (2).Child ($C_2$): Receives 0, hits continue, goes to $i = 2$.Iteration $i = 2$:$C_2$ forks.Parent ($C_2$): Receives a positive PID, hits break, exits loop $\rightarrow$ Prints "Hello!" (3).Child ($C_3$): Receives 0, hits continue, loop ends (since $i < 3$ is now false) $\rightarrow$ Prints "Hello!" (4). NIL DAS answered Apr 3 NIL DAS comment Share Follow 0 reply Please log in or register to add a comment.
1 1 vote go classes Baki Hanma answered Sep 2 Baki Hanma comment Share Follow 0 reply Please log in or register to add a comment.
0 0 votes https://www.youtube.com/live/IUAIytbb2mw?si=grlYPQnCmeUjcNYn&t=8331 sachin sir's explanation... Baki Hanma answered Sep 2 Baki Hanma comment Share Follow 0 reply Please log in or register to add a comment.