编辑:不要尝试将管道的末端连接到stdout。将管道的输入连接到stdout,将管道的输出连接到stdin。

我想使用pipe()和dup2()将子进程的stdout传送到其母亲的stdout。
在我的示例中,我尝试在子项中打印一个字符串,该子句的标准输出重定向到管道。然后将字符串显示在“母亲”的标准输出中。但是,输出永远不会出现在母进程的标准输出中。到底是怎么回事?



#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char *argv[])
{
        // This program should print 1, 2 and 4; but only prints 1 and 4.

        int stdout_cpy = dup(1);

        printf("1. stdout working\n");

        int pipe1[2];
        pipe(pipe1);

        int pid = fork();

        if (pid == 0) {
                // child
                dup2(pipe1[1], 1); // stdout out to pipe in
                close(pipe1[0]);
                fprintf(stdout, "2. This should print in the mother's stdout\n");
                exit(0);
        } else {
                // mother
                close(pipe1[1]);
                dup2(pipe1[0], 1); // stdout from pipe out
        }

        /* 2. should print in parent's stdout... */
        int status;
        while (wait(&status) > 0);

        printf("3. This should not print\n");

        dup2(stdout_cpy, 1);
        close(pipe1[0]);

        printf("4. stdout redirected, done\n");

        return 0;
}


输出量

1. stdout working
4. stdout redirected, done

最佳答案

这是您的代码的有效版本。为了显示子级的输出,父级必须从管道读取它,然后将其写入其自己的标准输出,这是此代码的作用。如果管道没有读取任何内容,则写入管道的输出将不会出现在任何地方。此更改使重复的标准输出基本上不相关。

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

int main(void)
{
    int stdout_cpy = dup(1);            // Mostly irrelevant

    printf("1. stdout working\n");

    int pipe1[2];
    pipe(pipe1);

    int pid = fork();

    if (pid == 0)
    {
        // child at work
        dup2(pipe1[1], 1);              // stdout out to write end of pipe
        close(pipe1[0]);                // Close both ends of the pipe!
        close(pipe1[1]);                // Close both ends of the pipe!
        printf("2. This should be read by parent from stdin and be printed to the parent's stdout\n");
        exit(0);
    }

    /* Parent at work */
    dup2(pipe1[0], 0);                  // stdin from from read end of pipe
    close(pipe1[0]);                    // Close both ends of the pipe!
    close(pipe1[1]);                    // Close both ends of the pipe!

    // Read data written on pipe by child, and write to stdout
    char buffer[512];
    int nbytes = read(0, buffer, sizeof(buffer));
    if (nbytes > 0)
        write(1, buffer, nbytes);

    /* 2. should print in parent's stdout... */
    int status;
    int corpse;
    while ((corpse = wait(&status)) > 0)
        printf("%d: child %d exited with status 0x%.4X\n", (int)getpid(), corpse, status);

    printf("3. This should print too\n");

    dup2(stdout_cpy, 1);                // Mostly irrelevant
    //close(pipe1[0]);                  // Long since closed
    close(stdout_cpy);                  // No longer needed (closed on exit anyway)

    printf("4. stdout redirected, done\n");

    return 0;
}


样本输出:

1. stdout working
2. This should be read by parent from stdin and be printed to the parent's stdout
8008: child 8009 exited with status 0x0000
3. This should print too
4. stdout redirected, done


另外,如果您只希望孩子的输出与父母的输出显示在同一设备上,则根本不用管道。子级将继承父级的标准输出,并且无需任何额外帮助即可写入。

关于c - 管道到标准输出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50669417/

10-13 08:02
查看更多