1,371 views
2 votes
2 votes

1)Consider the following pseudo code:

for(i=1;i<=4;i++)
{
fork();
printf("X");
}
How many times “X” is printed?
2) Consider the following pseudo code:
void main()
{
fork();
fork();
fork();
fork();
printf("X");
}

How many times “X” is printed?
 

4 Answers

Best answer
3 votes
3 votes
for 2 code no of child process=2^n -1=2^4 -1=15 process  +1 parent process  so total 16 times X will print

for 1st code

call will be like   

fork();  
printf("X");  16

fork();  
printf("X"); 8

fork();  
printf("X"); 4

fork();  
printf("X"); 2    print =30 times
selected by
0 votes
0 votes

The first code can be simplified as

fork();

fork();

fork();

fork();

which is exactly same as the second code. Since there are 4 fork() operation the total process will be

2^n = 16 process

Related questions

2 votes
2 votes
3 answers
2
Philosophical_Virus asked Dec 10, 2023
768 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
3
24aaaa23 asked Oct 1, 2023
719 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...
0 votes
0 votes
0 answers
4