以下程序调用fork()
并打算产生一个孤立进程。它可以正常工作,并且孤儿进程被进程收割init
(由 command 检查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。