457 views
5 5 votes

Consider the following C program. Assume that every $\texttt{fork()}$ call succeeds.

int main(void) {
    unsigned long int i;

    for (i = 0; i < 6; i++)
        if (fork() >= 0)
            continue;
        else
            break;
}

What is the total number of processes present after the loop terminates, including the original process?

  1. $7$
     
  2. $8$
     
  3. $32$
     
  4. $64$

2 Answers

0 0 votes

Asuccessful $\texttt{fork()}$ returns:

  • A positive value in the parent process
     
  • $0$ in the child process
     

Therefore, the condition $\texttt{fork() >= 0}$ is true in both the parent and the child. 

Every process continues to the next iteration.

The number of processes doubles in each iteration:

  • After iteration $1:~2$
  • After iteration $2: ~4$
  • After iteration $3: ~8$
  • After iteration $4: ~16$
  • After iteration $5: ~32$
  • After iteration $6: ~64$

Thus, the total number of processes is $2^6=64$.

Answer:
Position:
Show:

Related questions

3 3 votes
1 1 answer
313
313 views
GO Classes asked Jul 15
313 views
Assume that every $\texttt{fork()}$ call succeeds.int main() { int a = 0; int rc = fork(); a++; if (rc == 0) { rc = fork(); a++; } else { a++; } printf("a is %d\n", a); }...
4 4 votes
1 1 answer
310
310 views
GO Classes asked Jul 15
310 views
Assume that every $\texttt{fork()}$ call succeeds.int main() { int pid; pid = fork() && fork(); if (!pid) printf("A\n"); else printf("B\n"); return 0; }How many times are...
5 5 votes
1 1 answer
290
290 views
GO Classes asked Jul 15
290 views
Consider the following program. Assume that every $\texttt{fork()}$ call succeeds.int main() { int p1 = 1, p2 = 2, p3 = 3; p1 = fork(); if (p2 0) p2 = fork(); if (p1 0)...
5 5 votes
2 2 answers
269
269 views
GO Classes asked Jul 15
269 views
Consider the following C program. When the execution reaches line $5$, how many processes and threads, respectively, exist in the process hierarchy?int main() { int i; fo...