• edited by
3,434 views
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)

5 Answers

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)

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 Trace

  1. Start: We begin with 1 Parent Process ($P_0$) at $i=0$.

  2. 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$.

  3. 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$.

  4. 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).

Answer:
Position:
Show:

Related questions

20 20 votes
5 5 answers
2.8k
2.8k views
gatecse asked Feb 23
2,846 views
Consider a CPU that has to execute two types of processes. The first type, Actuators (A), requires a CPU burst of $6$ seconds. The second type, Controllers (C), requires ...
13 13 votes
2 2 answers
1.8k
1.8k views
gatecse asked Feb 23
1,771 views
Consider a hard disk with a rotational speed of $15000$ rpm. The time to move the read/write head from a track to its adjacent track is $1$ millisecond. Initially, the he...
10 10 votes
6 6 answers
2.5k
2.5k views
gatecse asked Feb 23
2,450 views
Consider a system consisting of $k$ instances of a resource $R$, being shared by $5$ processes. Assume that each process requires a maximum of two instances of resource $...
10 10 votes
2 2 answers
2.7k
2.7k views
gatecse asked Feb 23
2,678 views
Let $G$ be an undirected graph, which is a path on $8$ vertices. The number of matchings in $G$ is $\_\_\_\_$. (answer in integer)