Search code examples
cnamed-pipes

Open() with O_WRONLY blocks even though I open() with O_RDONLY on the other end


I am facing the exact problem that the title says.

CODE

pid_t childpid;
int childfdRead, childfdWrite; // file descriptors for childs
int parentfdsRead[numWorker], parentfdsWrite[numWorker]; // file descriptors for parent

// store the fifo filenames
char *childPipeNameRead[numWorker];
char *childPipeNameWrite[numWorker];

// helper string to construct fifo filenames
char *suffix = (char*)malloc(20*sizeof(char));
char *fifoname = (char*)malloc(20*sizeof(char));

for(i = 0; i < numWorker; i++){
    // Fifo filename structure read
    sprintf(fifoname, "%s", PATH);
    sprintf(suffix, "_childRead%d", i);
    childPipeNameRead[i] = strdup(strcat(fifoname, suffix));
    mkfifo(childPipeNameRead[i], 0666);

    // Fifo filename structure write
    sprintf(fifoname, "%s", PATH);
    sprintf(suffix, "_childWrite%d", i);
    childPipeNameWrite[i] = strdup(strcat(fifoname, suffix));
    mkfifo(childPipeNameWrite[i], 0666);

    childpid = fork();

    if(childpid < 0){
        perror("fork\n");
    }

    else if(childpid == 0){
        // Open read and write pipes on childs.
        if((childfdRead = open(childPipeNameRead[i], O_RDONLY | O_NONBLOCK)) < 0)
            perror("child pipe:");

        printf("Child with id %d opened pipe with name %s\n", getpid(), childPipeNameRead[i]);

        if((childfdWrite = open(childPipeNameWrite[i], O_WRONLY)) < 0)
            perror("child pipe:");

        printf("Child with id %d opened pipe with name %s\n", getpid(), childPipeNameWrite[i]);
        break;
    }
    else{
        // Open read and write pipes for each child in the parent process.
        if((parentfdsRead[i] = open(childPipeNameRead[i], O_RDONLY | O_NONBLOCK)) < 0)
            perror("parent pipe:");
        printf("Parent with id %d opened pipe with name %s\n", getpid(), childPipeNameRead[i]);

        if((parentfdsWrite[i] = open(childPipeNameWrite[i], O_WRONLY)) < 0)
            perror("parent pipe:");
        printf("Parent with id %d opened pipe with name %s\n", getpid(), childPipeNameWrite[i]);
    }
}

The program hangs after the parent and one child open their read pipe (parent opens only one of them). Is this normal behaviour? I was expecting it to open all pipes since I am using O_RDONLY | O_NONBLOCK for both the parent and the child.


Solution

  • The problem is that you open childPipeNameRead[i] for reading in both the child and parent process. And similarly you open childPipeNameWrite[i] for writing in both processes.

    You need to do the opposite in one of the processes.