919 views
2 votes
2 votes
Consider the following pseudo code:

for(i=1;i<=4;i++)
{
fork();
printf("Gate");
}

How many times “Gate” is printed?

2 Answers

2 votes
2 votes

30 times.

The answer given in various sources is wrong for this question. I executed the programm in Ubuntu.

For every fork call there will be to print statements here.

first fork call 2process = 2 prints

second fork call 4 process = 4 prints

third fork call 8 process = 8 prints

fourth fork call 16 process = 16 prints

total = 2+4+8+16 = 30 (except the first parent) every process in the derivation of process and child tree will execute print statements.

NOTE:question  is not about how many processes are created. 

1 votes
1 votes
There will be created 15 new child processes and one parent process, hence Gate should be printed 16 times.

Related questions

2 votes
2 votes
3 answers
3
Philosophical_Virus asked Dec 10, 2023
910 views
Int main (){fork();printf("a");fork();printf("b");return 0;}How many distinct outputs are possible of above code? And also give outputs
3 votes
3 votes
1 answer
4
24aaaa23 asked Oct 1, 2023
794 views
Consider the following code segment :int main (void) { for (int i=2; i<=5; i++) { fork( ); }printf (“GOCLASSES”);How many times “GOCLASSES” is printed by the ab...