管道到标准输出

Piping to stdout

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

我想使用 pipe() 和 dup2() 将 child 进程的标准输出通过管道传输到其母进程的标准输出。 在我的示例中,我尝试在 child 中打印一个字符串,它的标准输出重定向到管道;然后让字符串出现在母亲的标准输出中。但是,输出从未出现在母进程的标准输出中。这是怎么回事?

代码

#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

或者,如果您只是希望子项的输出与父项的输出出现在同一台设备上,根本就不要使用管道;子级将继承父级的标准输出并且可以在没有任何额外帮助的情况下写入它。