Q35: fork() || fork() && fork()

Question #35: How many total processes will be spawned after executing following program?

int main()

{

fork() || fork() && fork() ;

}

 

Options:

A)     4

B)      8

C)      6

D)     7

 

Solution:

As we have OR after the first fork(), hence both child and parent process will be executed further. After second fork(), we have 4 processes. In one of the case, where in we have child process in each of the fork calls, we have 0 || 0 = 0 till now, hence the next fork() won’t be executed and will return from here. For all other cases, another child will be forked, hence 4+3 = 7 processes. Hence, the correct answer is option D.