Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Program doesn't stop after reading from pipe

Tags:

c

unix

pipe

I am trying to understand pipes. I have this small program which uses a pipe to send a message from the parent process to its child. The child recieves all the 3 messages, but instead of exiting, after reading the last message it hangs. What am I doing wrong? Thanks.

PS: I noticed that if I would sleep for 2 seconds in the while loop from the parent, it would work.

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

int main(){

    int desc[2];
    pipe(desc);

    int pid = fork();

    if(pid == 0){
        while(1){
            sleep(1);
            char buffer[16];
            if(read(desc[0], buffer, 16) != 16){
                printf("Error or finished");
                exit(0);
            };
            printf("Child: message recieved - '%s'\n", buffer);
        }
        close(desc[1]);
    }
    if(pid > 0){
        int i=0;
        while(i <= 2){
            char buffer[100];
            i++; char x[10];
            strcpy(buffer, "Hello, child!");
            sprintf(x, " %d", i);
            strcat(buffer, x);
            if(write(desc[1], buffer, 16) != 16){
                printf("Error");
                exit(0);
            };
        }
        close(desc[0]);
    }
    return 0;
}
like image 632
Toma Radu-Petrescu Avatar asked Oct 16 '25 17:10

Toma Radu-Petrescu


1 Answers

You forgot to close the unused ends of the pipes in both the parent and child processes. Actually your child possess reading and writing parts of the pipe, so it cannot detect the end-of-file as there exists a writer (itself!), so it is blocked in the read. Change your code to:

if(pid == 0){
    close(desc[1]); // Child is not a writer, so close the write part immediately!
    while(1){
      ...
    }
}
if(pid > 0){
    close(desc[0]); // Parent is not a reader, so close the read part immediately!
    int i=0;
    while(i <= 2){
      ...
    }
}

Remember that on a pipe, end-of-file is "nothing more to read in the pipe" and "no more writer".

like image 167
Jean-Baptiste Yunès Avatar answered Oct 18 '25 06:10

Jean-Baptiste Yunès



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!