Aspire's Library

A Place for Latest Exam wise Questions, Videos, Previous Year Papers,
Study Stuff for MCA Examinations - NIMCET

Previous Year Question (PYQs)



Assuming that the system call $fork()$ never fails, consider the following C programs $P1$ and $P2$ executed on a UNIX or Linux system:
Program $P1$ Program $P2$
/* P1 */int main() {    fork();    fork();    fork();    print("Happy\n");}
/* P2 */int main() {    fork();    print("Happy\n");    fork();    print("Happy\n");    fork();    print("Happy\n");}
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:





Solution

For program $P1$:

There are $3$ fork calls before the print statement.

Each $fork()$ doubles the number of processes.

So, total number of processes after $3$ fork calls is:

$2^3=8$

Each process executes the print statement.

Therefore, $P1$ displays "Happy" $8$ times.

So, Statement I is true.

For program $P2$:

Initially, there is $1$ process.

After first $fork()$:

Number of processes $=2$

So, first print statement executes $2$ times.

After second $fork()$:

Number of processes $=4$

So, second print statement executes $4$ times.

After third $fork()$:

Number of processes $=8$

So, third print statement executes $8$ times.

Total number of times "Happy" is printed:

$2+4+8=14$

So, $P2$ displays "Happy" $14$ times, not $12$ times.

Therefore, Statement II is false.


Online Test Series,
Information About Examination,
Syllabus, Notification
and More.

Click Here to
View More


Online Test Series,
Information About Examination,
Syllabus, Notification
and More.

Click Here to
View More

Ask Your Question or Put Your Review.

loading...