【发布时间】:2013-10-01 11:45:56
【问题描述】:
以下程序调用fork() 并打算生成一个孤立进程。它工作正常,孤儿进程由进程init 获得(由命令ps 检查)。但是为什么 bash 似乎在等待输入呢?
...
if ((pid = fork() < 0) {
err_sys("fork error"); /* err_sys is declared in apue.h */
} else if (pid == 0) {
/* sleep to ensure that parent process terminates first */
printf("child process: %d\n", getpid());
sleep(2);
exit(0);
}
printf("parent process\n");
exit(0);
}
执行程序
$ ./a.out
parent process
$ child process: 4787
_ <= shell stops here until I hit Ctrl-C
(the underscore is just for demostration)
注意:我使用的是 Ubuntu 11.10。
【问题讨论】:
-
@JoachimPileborg 是的。它也有效。但我不明白为什么会这样。
-
打赌,如果你只是点击回车而不是
ctrl-c,那也可以——这是约阿希姆的观点。