Трубопровод к стандартному выводу - PullRequest
0 голосов
/ 03 июня 2018

Редактировать: не пытайтесь подключить конец трубы к стандартному выводу.Соедините вход канала с stdout, а выход канала с stdin.

Я хочу передать поток stdout дочернего процесса на stdout его матери, используя 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

1 Ответ

0 голосов
/ 03 июня 2018

Вот рабочая версия вашего кода.Чтобы вывод дочернего элемента появился, родитель должен прочитать его из канала и затем записать его в свой собственный стандартный вывод, что и делает этот код.Если из канала ничего не читается, вывод, записанный в канал, нигде не появится.Это изменение делает дублированный стандартный вывод в основном неактуальным.

#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

В качестве альтернативы, если вы просто хотите, чтобы выход дочернего элемента отображался на том же устройстве, что и выход родительского элементапросто не используйте трубу вообще;ребенок наследует стандартный вывод родителя и может писать в него без какой-либо дополнительной помощи.

...