0

这是我的名为 server.c 的示例代码(已删除包含以保持简单)。

int main()
{
    for(int i = 0; i < 10; i++) {
        fork();
        execl("./client", "./client", NULL);
    }
    if (wait(NULL) == -1)
        perror("error with wait");
    return 0;
}

这是从上述程序中执行的客户端代码。

int main()
{
    printf("This is the child with pid = %d from parent %d", getpid(), getppid());
    return 0;
}

现在让我解释一下我认为会发生什么以及我实际得到的输出。

在服务器中,我们进入 for 循环。在第一次迭代中,我们点击了 fork()。此时有两个进程,即父进程和第一个子进程。现在我们从这两个进程中执行一个名为“client”的程序。此客户端代码仅打印一些信息。所以当我运行这个服务器程序时,我应该得到两行,对吧?一条来自父母,另一条来自孩子?但是我只打印了一行,使用 strace 后我发现只有父母在打印东西,而不是孩子。那么这是为什么呢?

是因为孩子已经死了(这是正确的术语吗?),因为它被执行了,就不能再收集孩子了吗?如果是这样,孩子会怎么样?变成丧尸了吧?它会被init收集吗?即便如此,为什么它不会在像僵尸一样结束之前打印出那条线?

4

2 回答 2

0

如果标准输出连接到终端,它应该是行缓冲的,所以不需要刷新。并且所有流在关闭时都会自动刷新,这会在程序完成时发生。

也许你看到两个打印在同一行?试试这个代码:

#include <stdio.h>
int main(int argc, char **argv) {
  printf("This is the child with pid = %d from parent %d\n", getpid(), getppid());
  return 0;
}

顺便说一句,您缺少一个%din printf

于 2017-04-12T22:33:11.180 回答
0

你绝对应该得到两条线。

如果你不是,那可能是由于你运行程序的方式。父母可以打印并退出,如果您在该点停止查看或收听,您将丢失来自孩子的信息。

因此,请确保您直接从 shell 终端运行脚本,而不是脚本的一部分,从 IDE、编辑器或任何其他工具中运行。

例如,这里的输出./server; echo "Done"

user ~ $ ./server; echo "Done."
This is the child with pid = 27904 from parent
Done.

这使它看起来只有一行输出。但是,在这之后的行中,提示已经返回,并且子进程在其后写入了一些信息:

user ~ $ This is the child with pid = 27905 from parent

如果您以 shell 等待的方式运行脚本,例如添加| catwhich will wait until the pipe is fully closed,您可以更清楚地看到两者:

user ~ $ ./server | cat; echo "Done."
This is the child with pid = 27953 from parent
This is the child with pid = 27955 from parent
Done.
于 2017-04-12T22:37:19.900 回答