Question #32: How many total processes will be spawned after executing following program?
int main()
{
fork() && fork();
}
A) 2
B) 3
C) 4
D) 1
Solution: The system call fork() returns PID for the parent but returns 0 for the child. So after the first fork(), the child process returns 0 and will not execute the next fork since there is a logical AND operator after that. 0 AND with anything is 0, hence the next fork() won’t be executed, there by totaling 3 processes. Hence, the correct answer is B.