I have two pipes that both get different data at random points. What I want is to print the content from both pipes to stdout.
__________________
pipe1 | |
]==============> -----------.
| | \
| process1 | -----> stdout
pipe2 | | /
]==============> -----------´
|__________________|
My code looks about this in process1:
while (1) {
read(pipe1[0], &buff, sizeof(char));
write(1, &buff, sizeof(char));
read(pipe2[0], &buff2, sizeof(char));
write(1, &buff2, sizeof(char));
}
However that doesn’t work as one read() can be blocked(if no data is coming) while data comes from the other pipe.
How can I print simultaneously from both pipes without being blocked in one pipe? Or any other suggestion on how to solve this is welcome.
Use
selectto wait on both sockets. When data is ready, it will tell you which pipes have data available.Note the above runs forever unless there’s an error. You will likely want your program to stop at some point.