edited by
5,780 views
6 votes
6 votes
#include <stdio.h>
#include <unistd.h>
int main() {
    int i;
    for(i=1;i<=3;i++) {
        fork();
        printf("*");
    }
    return 0;
}



how many times * will be printed ?...is it 11?

edited by

4 Answers

Best answer
12 votes
12 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 votes
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 votes
1 votes

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:

Related questions

46
views
1 answers
0 votes
vivek10010 asked 2 days ago
46 views
How many child processes will be created in the following code: main(){if(!fork()){ if(!fork()) fork();}fork();}
429
views
1 answers
2 votes
Sparkboy asked Apr 4
429 views
Consider the following pieces of codes for fork( ) system call. Solve and explain how many child processes are created upon execution of this program? Snippet 1: ... 0; }Learning Outcomes. Understand the working of fork ( ) system call.
619
views
2 answers
2 votes
BitMask asked Mar 3
619 views
Q23. The following C program is executed on a Unix/Linux system: main(){ int i=0; while ( ... many number of processes will be created after executing the above program.Options:1.10232.Infinite3.10244.2048
1.2k
views
4 answers
2 votes
Philosophical_Virus asked Dec 10, 2023
1,172 views
Int main (){fork();printf("a");fork();printf("b");return 0;}How many distinct outputs are possible of above code? And also give outputs