Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is causing my program to hang and not exit properly? (pipe, read system call, while loop)

I have a program where I write from several child processes to a pipe, and then attempt to read all the messages written to each process, from each pipe, and print them to the screen. With the following code (specifically, the while loop using the read system call to store the messages into buffer buf), my program will hang and not exit, nor print all of the messages sent to the different processes.

for (i = 0; i < MAXP; i++) {
    if(id == i) {
        while(read(pfds[i][0], buf, sizeof(buf)) > 0)
             printf("process%d has received a message from %s\n",i,buf);    
    }
}

However, with the following code, the program exits correctly, but does not print all the messages (as they aren't all read):

for (i = 0; i < MAXP; i++) {
    if(id == i) {
        nbytes = read(pfds[i][0], buf, sizeof(buf));
        printf("process%d has received a message from %s\n",i,buf); 
    }
}

This is my code writing to the pipes:

write(pfds[j][1],msg,9);  // write the message to j pipe

and the message is:

sprintf(msg, "process%d", i); // create the message - 9 bytes (inc. null term)
// the message is "process0" or "process1" ... through "process8"

which is a 9 byte char array:

char msg[9];

Solution

for (j = 0; j < MAXP; j++) {
    close(pfds[j][1]); // close write to j from i
}
like image 452
Søren Avatar asked Sep 05 '25 09:09

Søren


1 Answers

Maybe b/c you didn't close the pipe? if write end is not closed, read will keep on hanging

Trying adding

close(pfds[j][1]);

after you write your msgs to the pipe

like image 132
JoeC Avatar answered Sep 09 '25 02:09

JoeC