in Operating System edited by
1,920 views
0 votes
0 votes
Consider the following code snippet using the fork () and wait () system calls. Assume that the code compiles and runs correctly, and that the system calls run successfully without any errors.

int $\mathrm{x}=3$;

while $(\mathrm{x}>0)$ {

fork ();

printf("hello");

wait (NULL) ;

X-- ;

}

The total number of times the printf statement is executed is __________.
in Operating System edited by
by
1.9k views

2 Answers

2 votes
2 votes

In first iteration, parent will spawn a child c1, both will have x=3 and execute printf statement 3 times.

In second iteration, parent and child c1 will spawn 2 children c2 and c3, both children will have x=2 and execute printf statement 2 times.

In third iteration, parent and children c1, c2, c3 will spawn 4 children c4, c5, c6 and c7, all 4 children will have x=1 and execute printf statement only once.

Loop terminates in all processes.

Number of times print statement is executed is $3*2 + 2*2 + 4*1 = 14$

Answer - 14

0 votes
0 votes

Now counting the no. of printfs ..we get 14 as ans.

Answer:

Related questions