in Operating System
470 views
0 votes
0 votes
main(){

int i,n;

 for(int i=0;i<n;i++){

fork();

printf("*");

}

}

How many times ‘*’ will be printed? The answer is not 2^n ? why?
in Operating System
470 views

3 Comments

It will vary. Check for n=1,2,3. If you checked this please give what expression you are getting. I am not getting any fixed expression
0
0
I am getting this expression.

2^(n+1)-2
0
0
getting correct for n=4?? too. if yes then I think this correct
0
0

1 Answer

0 votes
0 votes
each time the for loop gets executed it prints star(*) two times

=> cuz fork() creates one child and it prints one star and when the control comes back to parent it prints * again

so for n loop executions the answer should be => 2n

1 comment

No this is not the right answer. Let say n=3 ,then I will be getting 14 stars. I think the general expression is 2^(n+1)-2. I don’t know why.
–1
–1

Related questions