recategorized by
2,531 views
3 votes
3 votes

 

Assuming that the system call $\text{fork}()$ never fails, consider the following C program $P1$ and $P2$ executed on a UNIX/Linux system:

$\begin{array}{|ll|ll|} \hline & /*P1*/  & &/*P2*/ \\ \text{int} & \text{main}() \{ & \text{int} & \text{main} () \{ \\ & \text{fork} (); & & \text{fork();} \\ & \text{fork}(); &&  \text{printf(“Happy \n”)}; \\ & \text{fork}(); &  & \text{fork()}; \\ & \text{printf(“Happy \n”);}  & &\text{printf(“Happy \n”);} \\ \} &  & & \text{fork()};\\ && & \text{printf(“Happy \n”);} \\ && \} \\ \hline \end{array}$

Statement $I$: $P1$ displays “Happy” $8$ times

Statement $II$: $P2$ displays “Happy” $12$ times

In the light of the above statements, choose the correct answer from the options given below

  1. Both Statement $I$ and Statement $II$ are true
  2. Both Statement $I$ and Statement $II$ are false
  3. Statement $I$ is correct but Statement $II$ is false
  4. Statement $I$ is incorrect but Statement $II$ is true
recategorized by

2 Answers

1 votes
1 votes
First program P1 :-  since 3 fork call are there hence 2^3-2 =7 child processes will be created total process 7+1=8

so total 8 times Happy will be printed

 hence Statement I: P1 displays “Happy” 8 times is correct

 Second  program P2 :-  First fork () call creates total 2 processes [happy printed 2 times]

                                       Second fork() will cause happy prints 4 times  (2x2=4 )

                                       Third fork()  will cause happy printed 8 times  (2x4 )

  so total 14 times Happy will be printed

 hence Statement II: P2 displays “Happy” 12 times is not correct

Ans
Answer:

Related questions