retagged by
876 views
3 votes
3 votes
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 above code?
retagged by

1 Answer

3 votes
3 votes

We can simplify the code by opening the loop:-


int main (void) {

fork( );

fork( );

fork( );

fork( );

printf (“GOCLASSES”);

 

Now,we no total no of processes= 2 ^ n where n = no of forks

Therefore total processes= 2 ^ 4 = 16 thus GoClasses will be printed 16 times

 

Related questions

2 votes
2 votes
3 answers
3
Philosophical_Virus asked Dec 10, 2023
994 views
Int main (){fork();printf("a");fork();printf("b");return 0;}How many distinct outputs are possible of above code? And also give outputs
0 votes
0 votes
0 answers
4