Why does this program print “forked!” 4 times?
问题 Why does this program print “forked!” 4 times? #include <stdio.h> #include <unistd.h> int main(void) { fork() && (fork() || fork()); printf(\"forked!\\n\"); return 0; } 回答1: The first fork() returns a non-zero value in the calling process (call it p0) and 0 in the child (call it p1). In p1 the shortcircuit for && is taken and the process calls printf and terminates. In p0 the process must evaluate the remainder of the expression. Then it calls fork() again, thus creating a new child process