【问题标题】:shell waits for input after orphan process was reaped by init孤儿进程被init收割后shell等待输入
【发布时间】: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,那也可以——这是约阿希姆的观点。

标签: c linux bash unix ubuntu


【解决方案1】:

shell 在显示提示时不再等待。问题(如果你可以这样称呼它)是子进程打印一些输出,然后是换行符。此换行符已打印,但 shell 不打印新提示。就这样。像往常一样写命令就行了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-08
    • 2014-12-06
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 2020-09-09
    • 1970-01-01
    相关资源
    最近更新 更多