Several concepts are used here, first of all, knowing what the fork does and what it returns in certain circumstances. Soon, when it is called, it creates a duplicate process of the caller and returns 0 ( false for logical expressions) in the child process and nonzero ( true for logical expressions) for the parent process. In fact, it can return a negative (non-zero) value in case of an error, but here we assume that it always succeeds.
The second concept is a short calculation of logical expressions such as && and || , in particular 0 && fork() will not call fork() , because if the first operand is false (zero) then there is no need to calculate the second. Similarly, 1 || fork() 1 || fork() will not call fork() .
Also note that in child processes, expression evaluation continues at the same point as in the parent process.
Also note that the expression is evaluated in the following order due to priority:
(fork() && fork()) || (fork() && fork())
These observations should lead you to the correct answer.
Consider a simplified example fork() && fork()
fork() / \ false true && fork() / \ false true
So, here we have created three processes, two of which return false as the result, and one returns true . Then for || we have all processes returning false , trying to run the same operator again, so as an answer we have 2 * 3 + 1 = 7 .
source share