in Operating System retagged by
736 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?
in Operating System retagged by
736 views

4 Comments

since printf (“GOCLASSES”); is after for loop then I guess it will print only 1 time
0
0
nope there is fork() so new processes will be created
0
0
16 times printed  GO classes
1
1
Total Process = 2^n ( n= no of fork) = 2^4 = 16 .

hence, GOCLASSES will be print 16 times.
1
1

1 Answer

2 votes
2 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