c - init 收割孤儿进程后,shell 等待输入

标签 c linux bash unix ubuntu

以下程序调用 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。

最佳答案

当 shell 显示提示时,它不再像平常那样等待。问题(如果你可以这样调用它的话)是子进程打印一些输出后跟一个换行符。此换行符已打印,但 shell 不会打印新提示。就这样。只需像往常一样编写命令即可。

关于c - init 收割孤儿进程后,shell 等待输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19115336/

相关文章:

android - cstdlib 的 NDK 错误

c - 如何使用 CMake 将我的库与 czmq 链接

c - Select() 阻塞标准输入,不会超时

linux - 如何用以特定数字开头的数字替换文件名?

linux - 如何知道哪个文件保存 grep 结果?

windows - 如何在 bash/ubuntu 下通过 SSH 在 Windows 上使用 hg?

bash - 为什么函数中的return语句会导致bash脚本结束

c - 为什么 int 在 c 或任何其他语言中占用 4 个字节?

bash - 将 GNU Parallel 与 Split 结合使用

linux - 如何在搜索(查找)结果中列出所有目录中的文件?