529 views

1 Answer

Best answer
7 votes
7 votes

There are two concepts here, short circuit rule in C and fork in OS.

By short circuit rule in C, if the first operand of logical AND $(\&\&)$ is $0,$ the second operand won't be evaluated. Similarly, if the first operand of logical OR $(||)$ is $1.$

Now, fork(), copies the current process image and creates a child process which starts executing from the next instruction onward. fork() returns the child process id to the parent and 0 to the child process.

Now, let's trace the given code.
 

if((fork1() && fork2()) || fork3()){
    printf("GATE2021");
}


After fork1, we have $2$ processes and only one has return value non zero and only that will execute fork2 creating one new process.

Thus after fork1 and fork2 we will have $3$ processes and two of them will have $0$ as value for the first operand of || and only those $2$ will execute fork3 creating $2$ more processes which will have their return value as $0.$ Thus in total we will have $5$ processes and $3$ of them will have TRUE (non zero) value for "if" and executes the printf.

So, correct answer $3.$

selected by
Answer:

Related questions

4 votes
4 votes
1 answer
3
2 votes
2 votes
1 answer
4
gatecse asked Nov 15, 2020
105 views
Processes in an operating system can be in any of the following states. Choose the proper matching for the given table.$\begin{array}{|l|l|l|}\hline& \textbf{Process Stat...