485 views
2 2 votes

Consider the following C code segment:
 

#include <unistd.h>
#include <stdio.h>
int main() {
    int i;
    for (i = 0; i < 2; i++) {
        pid_t pid = fork();
        
        if (pid == 0) {
            // Child branch
            printf("Child %d created in iteration %d\n", getpid(), i);
            break; // Exit the loop only for the child process
        } else {
            // Parent branch
            printf("Parent %d created child %d in iteration %d\n", getpid(), pid, i);
        }
    }
    
    sleep(1);
    return 0;
}


Assuming all $\verb|fork()|$ calls succeed, what is the total number of processes (including the original parent) that will be running immediately before the program terminates?

2 Answers

0 0 votes
  1. Original Parent (P): Completes both loop iterations.
     
  2. Child 1 (C1): Created during $i=0$, then breaks out of the loop immediately.
     
  3. Child 2 (C2): Created during $i=1$, then breaks out of the loop immediately.
     

The total number of processes running before the program terminates is THREE.

Answer:
Position:
Show:

Related questions

2 2 votes
1 1 answer
284
284 views
GO Classes asked Dec 13, 2025
284 views
A legacy Unix-like file system uses an index node (i-node) structure that contains $\mathbf8$ direct, zero single-indirect, and one double-indirect pointer. The disk bloc...
1 1 vote
1 1 answer
261
261 views
GO Classes asked Dec 13, 2025
261 views
The index node (i-node) of a Linux-like file system is configured with $\mathbf{10}$ direct, two singleindirect, and one triple-indirect pointers. The disk block size is ...
3 3 votes
1 1 answer
259
259 views
GO Classes asked Dec 13, 2025
259 views
Consider a file system utilizing a FAT (File Allocation Table) where the overhead associated with each entry in the FAT is $\mathbf{4}$ bytes. If the file system is deplo...
2 2 votes
1 1 answer
321
321 views
GO Classes asked Dec 13, 2025
321 views
A FAT (File Allocation Table) based file system is being used. The total overhead of each entry in the FAT is $\mathbf{8 ~bytes}$ in size. Given a $50 \times 10^9 ~\textb...