以下函数将始终具有四行输出吗?如果没有,原因是什么?喜欢在什么情况下?
int main(int argc, char** argv) {
printf("hello1\n");
if(!fork()) {
printf("helloAnother from child\n");
}
printf("helloh\n");
printf("helloqq\n");
return 0;
}
您正在使用syscall fork()。
http://man7.org/linux/man-pages/man2/fork.2.html
从文档中您可以看到fork确实向该系统调用创建的子进程返回0。在父进程和子进程中都不会有4行输出,因为两者都会写3行。结合两者,您将在终端中获得6行输出。