retagged by
3,705 views
4 votes
4 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 x=3;

while (x>0){

fork ();

printf("hello");

wait (NULL) ;

X-- ;

}

The total number of times the printf statement is executed is __________.

retagged by

2 Answers

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

Answer:

Related questions

2.8k
views
2 answers
2 votes
Arjun asked Feb 16
2,752 views
Consider a $512$ GB hard disk with $32$ storage surfaces. There are $4096$ sectors per track and each sector holds $1024$ bytes of data. The number of cylinders in the hard disk is _________.
3.5k
views
3 answers
2 votes
Arjun asked Feb 16
3,537 views
Consider a memory management system that uses a page size of $2 \mathrm{~KB}$. Assume that both the physical and virtual addresses start from $0$. ... format) corresponding to the virtual address $2500$ (in decimal format) is ___________.
2.6k
views
3 answers
3 votes
Arjun asked Feb 16
2,558 views
Let $A$ and $B$ be non-empty finite sets such that there exist one-to-one and onto functions $\text{(i)}$ from $A$ to $B$ and $\text{(ii)}$ from $A \times A$ to $A \cup B$. The number of possible values of $\text{|A|}$ is ___________.
3.6k
views
1 answers
4 votes
Arjun asked Feb 16
3,592 views
Consider the operator precedence and associativity rules for the integer arithmetic operators given in the table below.Operator PrecedenceAssociativity+Highest Left-HighRight*MediumRight/ ... as per the above rules is ________.