It is allowable (and expected) that read can return 0. This means the pipe is returning EOF. You are not handling this condition. The contents of errno
are irrelevant unless the call fails and returns -1.
for (;;)
{
bytes = read(fd, buffer, sizeof(buffer));
if (bytes > 0)
{
total_bytes += (size_t)bytes;
printf("%s", buffer);
}
if (bytes == 0)
return //something appropriate
if (bytes == -1)
{
if (errno == EWOULDBLOCK)
break; // recieve buffer is empty so return to main loop
else
{
perror("read error");
return EXIT_FAILURE;
}
}
}
You are going through the effort to return different codes but you don't pay any attention to them in main
.
Also, what's with the 3 select
statements again? I thought that was cleared up in a previous question.
Edit
for (;;)
{
// clear fds read flags
FD_ZERO(&read_fds);
FD_SET(fd_a, &read_fds);
FD_SET(fd_b, &read_fds);
FD_SET(fd_c, &read_fds);
tv.tv_sec = 0;
tv.tv_usec = 10000;
nfd = select(fd_c + 1, &read_fds, NULL, NULL, &tv);
if (nfd == 0) //timeout - continue or do something else for a bit
continue;
if (nfd == -1)
{
perror("select error");
return EXIT_FAILURE;
}
if (FD_ISSET(fd_a, &read_fds))
readPipe(fd_a);
if (FD_ISSET(fd_b, &read_fds))
readPipe(fd_b);
if (FD_ISSET(fd_c, &read_fds))
readPipe(fd_c);
}