Q32: fork && fork

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.

Q31: fork system call

Question #31: The system call fork() returns PID for the parent but returns 0 for the child. How many total processes will be spawned after executing following program?

int main()

{

fork();

fork();

}

A)     2

B)     3

C)     4

D)     1

 

Solution: After first fork(), we have two processes, let’s say A(parent) and B(child). Now, both A and B will fork one child each, hence totaling 4 processes. Therefore, the correct answer is C.