Search code examples
cpipefork

Parallel pipe issue when closing in c


I am currently working on a project where I need to create multiple fork who read from different pipe until the pipe closed. The issue here is that if I create more than one pipe, even if I close both side of all the pipe the child are still stuck on read.

Here is a simplify version of my code with the same issue:

#include <stdlib.h>
#include <unistd.h>

#define NB_PIPES 2

void    read_write_pipe(int pipefd[2]) { //write what goes through is pipe until it closed
    char buf;

    close(pipefd[1]); //close the writing part of the tube
    while (read(pipefd[0], &buf, 1) > 0)
        write(1, &buf, 1);
    close(pipefd[0]);
    _exit(EXIT_SUCCESS);
}

int main(void)
{
    int pipefd[NB_PIPES][2];
    pid_t cpid;

    for (int i = 0; i < NB_PIPES; i++) { //I create my pype
        pipe(pipefd[i]);
    }
    for (int i = 0; i < NB_PIPES; i++) {
        cpid = fork();
        if (cpid == 0)
            read_write_pipe(pipefd[i]); //create fork who will read from pipe i
        else {
            close(pipefd[i][0]); //close the reading part of pipe i
        }
    }
    for (int i = 0; i < NB_PIPES; i++) {
        write(pipefd[i][1], "Salut\n", 6); //write through pipe i
        close(pipefd[i][1]); //close the writing part of pipe i
    }
    for (int i = 0; i < NB_PIPES; i++) {
        wait(NULL);
    }
    return (0);
}

I compile with ggc

Thanks for reading and for the help !


Solution

  • So thanks to Bodo I found the issue Here is a correct version of the code:

    #include <stdlib.h>
    #include <unistd.h>
    
    #define NB_PIPES 2
    
    void    read_write_pipe(int pipefd[NB_PIPES][2], int i) { //write what goes through is pipe until it closed
        char buf;
    
        for (int j = 0; j < NB_PIPES; j++) {
            if (j != i) {
                close(pipefd[j][0]); //close all the other pipe
                close(pipefd[j][1]);
            }
        }
        close(pipefd[i][1]); //close the writing part of the tube
        while (read(pipefd[i][0], &buf, 1) > 0)
            write(1, &buf, 1);
        close(pipefd[i][0]);
        _exit(EXIT_SUCCESS);
    }
    
    int main(void)
    {
        int pipefd[NB_PIPES][2];
        pid_t cpid;
    
        for (int i = 0; i < NB_PIPES; i++) { //I create my pype
            pipe(pipefd[i]);
        }
        for (int i = 0; i < NB_PIPES; i++) {
            cpid = fork();
            if (cpid == 0)
                read_write_pipe(pipefd, i); //create fork who will read from pipe i
            else {
                close(pipefd[i][0]); //close the reading part of pipe i
            }
        }
        sleep(1);
        for (int i = 0; i < NB_PIPES; i++) {
            write(pipefd[i][1], "Salut\n", 6); //write through pipe i
            close(pipefd[i][1]); //close the writing part of pipe i
        }
        for (int i = 0; i < NB_PIPES; i++) {
            wait(NULL);
        }
        return (0);
    }
    

    Thanks for the help !