父进程,创建2个子进程并使用管道发送数据

时间:2018-09-09 18:30:58

标签: c linux pipe

我必须创建2个子进程并将数据从父进程发送到这两个进程,所以我使用了pipe

如果我仅使用1个子进程和1个管道,则所有这些都可以与fdopenfscanffprintf完美配合。 另外,如果我创建2个管道并将数据发送到单个进程,则仍然可以完美运行。

但是,如果我创建第二个进程并尝试从第二个pipe中读取,则什么也没发生。

例如:

int main() {
    pid_t pid1, pid2;

    int a[2];
    pipe(a);


    pid1 = fork();

    if(pid1 == 0) {
        char x,y;
        FILE *stream;
        stream = fdopen(a[0],"r");

        fscanf(stream,"%c",&x);
        printf("%c\n", x);

        close(a[1]);
        close(a[0]);
    } else {
        int b[2];
        pipe(b);
        pid2 = fork();
        FILE *stream1, *stream2;
        close(a[0]);
        close(b[0]);
        stream1 = fdopen(a[1],"w");
        stream2 = fdopen(b[1],"w");

        fprintf(stream1, "yo bella zio\n");
        fprintf(stream2, "como estas\n");

        fflush(stream1);
        fflush(stream2);
        close(a[1]);
        close(b[1]);

        waitpid (pid1, NULL, 0);
        waitpid (pid2, NULL, 0);

        if (pid2 == 0) {     
            FILE *stream;
            close(b[1]);
            close(a[1]);
            close(a[0]);
            stream = fdopen(b[0],"r");

            fscanf(stream,"%c",&x);

            printf("%c\n", x);
        } else {

        }
    }
}

我真的尝试了所有组合。一起声明所有管道,关闭或不关闭管道。一切,但一无所有。

1 个答案:

答案 0 :(得分:1)

此代码解决了my comment中发现的问题和一些零散的问题。

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(void)
{
    pid_t pid1, pid2;
    int a[2];
    int b[2];
    pipe(a);

    pid1 = fork();

    if (pid1 < 0)
    {
        fprintf(stderr, "failed to fork child 1 (%d: %s)\n", errno, strerror(errno));
        exit(EXIT_FAILURE);
    }
    else if (pid1 == 0)
    {
        close(a[1]);    // Must be closed before the loop
        FILE *stream = fdopen(a[0], "r");
        if (stream == NULL)
        {
            fprintf(stderr, "failed to create stream for reading (%d: %s)\n", errno, strerror(errno));
            exit(EXIT_FAILURE);
        }

        int c;
        while ((c = getc(stream)) != EOF)
            putchar(c);

        //char x;
        //fscanf(stream, "%c", &x);
        //printf("%c\n", x);

        //close(a[0]);  -- Bad idea once you've used fdopen() on the descriptor 
        printf("Child 1 done\n");
        exit(0);
    }
    else
    {
        pipe(b);
        pid2 = fork();
        if (pid2 < 0)
        {
            fprintf(stderr, "failed to fork child 2 (%d: %s)\n", errno, strerror(errno));
            exit(EXIT_FAILURE);
        }
        else if (pid2 == 0)
        {
            close(b[1]);
            close(a[1]);
            close(a[0]);
            FILE *stream = fdopen(b[0], "r");
            if (stream == NULL)
            {
                fprintf(stderr, "failed to create stream for reading (%d: %s)\n", errno, strerror(errno));
                exit(EXIT_FAILURE);
            }

            int c;
            while ((c = getc(stream)) != EOF)
                putchar(c);

            //char x;
            //fscanf(stream, "%c", &x);
            //printf("%c\n", x);

            printf("Child 2 done\n");
            exit(0);
        }
    }

    close(a[0]);
    close(b[0]);

    FILE *stream1 = fdopen(a[1], "w");
    if (stream1 == NULL)
    {
        fprintf(stderr, "failed to create stream for writing (%d: %s)\n", errno, strerror(errno));
        exit(EXIT_FAILURE);
    }
    FILE *stream2 = fdopen(b[1], "w");
    if (stream2 == NULL)
    {
        fprintf(stderr, "failed to create stream for writing (%d: %s)\n", errno, strerror(errno));
        exit(EXIT_FAILURE);
    }

    fprintf(stream1, "yo bella zio\n");
    fprintf(stream2, "como estas\n");

    fflush(stream1);    // Not necessary because fclose flushes the stream
    fflush(stream2);    // Not necessary because fclose flushes the stream
    fclose(stream1);    // Necessary because child won't get EOF until this is closed
    fclose(stream2);    // Necessary because child won't get EOF until this is closed
    //close(a[1]);      -- bad idea once you've used fdopen() on the descriptor
    //close(b[1]);      -- bad idea once you've used fdopen() on the descriptor

    waitpid(pid1, NULL, 0);
    waitpid(pid2, NULL, 0);
    printf("All done!\n");
    return 0;
}

请注意,我更改了子进程,以便(a)它们在代码块中显式退出,并且(b)使它们的主体变成循环,以便打印所有发送的数据。这要求我将close(a[1])移到第一个孩子中。否则,循环不会终止,因为操作系统会看到孩子1打开了用于写入的描述符。

在运行macOS 10.13.6 High Sierra(作为编译器的GCC 8.2.0)的Mac上执行时,得到以下输出:

yo bella zio
Child 1 done
como estas
Child 2 done
All done!