I’m working on some code involving pipes. The idea is that I’m supposed to have a process looping indefinitely and add data to the pipe as it comes (I’m testing this by reading a file, and going line by line in a while loop).
If I set the other process (the one that reads the pipe) to sleep so the entire file is read I have no problems and get all the file in the output. As soon as I remove the sleep (so now the 2 processes start simultaneously with the 2nd process reading the information off the pipe as it comes), my code goes straight to the else block of my code below and I never see any actual output. What am I doing wrong?
close(pipe[1]);
sleep(5);
while (1) {
nbytes = read(pipe[0], buffer, 200);
if(errno != EWOULDBLOCK) {
printf("%s", buffer);
}
else {
printf("I am not blocked here\n");
sleep(1);
}
}
Thanks
Two things:
int nbio=1; ioctl(pipe[0], FIONBIO, &nbio);probably errno is EWOULDBLOCK the first time through, and then doesn’t get updated on successful read, so it looks like EWOULDBLOCK again.