• edited by
7,621 views

4 Answers

Best answer
11 11 votes

The above program is same as 

  fork();

  printf("*");

  fork();

  printf("*");

  fork();

printf("*");

; 

 1st fork at root, 2nd fork at level 1,3rd fork at level2

 14 is the ans.

But answer can be more than 14, due to printf buffer also being copied as a result of fork.

• selected by
5 5 votes

Here fork() is called 3 times through the loop from i=1 to 3 . each time * gets printed .

So, total number of process created :

(21 - 1 ) + 1 =1 + 1 = 2  .. at first level 

( 22 - 1) + 1 = 3+1 = 4 ... at 2nd level

(23-1)+1 =7 + 1 = 8 ...  at 3rd level

so total process created 2+4+8 = 14 

1 1 vote

By using the formula Total processesd will be created will be 2n

first fork will create 2 processes so 2-"*"

By the second fork there will be 4 processes so 4-"*"

So for the three forks total processes will be created will be 8 so 8 "*"

Total 8+4+2=14 is the answer

Answer:
Position:
Show:

Related questions

5 5 votes
2 2 answers
469
469 views
GO Classes asked Jul 15
469 views
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) continu...
3 3 votes
1 1 answer
321
321 views
GO Classes asked Jul 15
321 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
316
316 views
GO Classes asked Jul 15
316 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
296
296 views
GO Classes asked Jul 15
296 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)...