为什么我的子进程分叉的程序需要用户在退出前输入 'Enter'?

Why my program in which a child process is forked needs user to type 'Enter' before exiting?

我有以下程序:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <sys/wait.h>

int main()
{
    int p[2];
    char *argv[2];
    argv[0] = "wc";
    argv[1] = "-w";
    argv[2] = NULL;

    pipe(p);
    if (fork() == 0)
    {
        close(0);
        dup(p[0]);
        close(p[0]);
        close(p[1]);
        execvp(argv[0], argv);
    }
    else
    {
        close(p[0]);
        write(p[1], "hello world\n", 12);
    }

    fprintf(stdout, "hello world\n");
}

当我 运行 它时:

$ gcc a.c
$ ./a.out

我得到以下信息:

hello world
$ 2
_    // the cursor is flickering here

输入Enter后程序退出。这是什么原因?另外,如果我这样交换父进程和子进程中的内容:


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <sys/wait.h>

int main()
{
    int p[2];
    char *argv[2];
    argv[0] = "wc";
    argv[1] = "-w";
    argv[2] = NULL;

    pipe(p);
    if (fork() == 0)
    {
        close(p[0]);
        write(p[1], "hello world\n", 12);
    }
    else
    {
        close(0);
        dup(p[0]);
        close(p[0]);
        close(p[1]);
        execvp(argv[0], argv);
    }

    fprintf(stdout, "hello world\n");
}

我得到了预期的输出:

hello world
2
$

程序已退出并准备好获取下一个命令。第一个程序有什么问题?

如果您仔细查看输出,您会发现您的程序退出:

hello world
$ 2
_    // the cursor is flickering here

看看 $ 是如何打印出来的?这是您 shell 像往常一样等待输入。通过按 enter,您刚刚向 shell 输入了一个空白命令,并获得了第二个 $ 提示。

2 在那里做什么?那是 wc 的输出。您的程序实际上并没有在等待 wc 退出。这意味着您的程序在 之前 wc 退出,因此 shell 恢复,打印其提示,并且仅 然后 wc 退出并打印 2.

要解决此问题,您可能需要添加某种 wait 调用以等待父进程中的子进程。