Используйте select
для ожидания на обоих сокетах. Когда данные будут готовы, вам сообщат, по каким каналам есть данные.
void setnonblocking(int fd) {
int opts;
opts = fcntl(fd,F_GETFL);
if (opts < 0) {
perror("Couldn't get file descriptor flags");
exit(EXIT_FAILURE);
}
opts = (opts | O_NONBLOCK);
if (fcntl(fd,F_SETFL,opts) < 0) {
perror("Couldn't set file descriptor to non-blocking");
exit(EXIT_FAILURE);
}
return;
}
#ifndef BUFSIZE
# define BUFSIZE 1024
#endif
void cat(fd_set* waiting, int fd) {
static char buf[BUFSIZE];
int readCnt;
if (FD_ISSET(fd, waiting)) {
while ((readCnt = read(fd, buf, BUFSIZE)) > 0) {
write(stdout, buf, readCnt);
}
if (readCnt < 0) {
perror("Error reading from pipe");
}
}
}
...
{
fd_set pipes, readable;
setnonblocking(pipes1[0]);
setnonblocking(pipes2[0]);
FD_ZERO(&pipes);
FD_SET(pipe1[0],&pipes);
FD_SET(pipe2[0],&pipes);
int ready;
while (1) {
if ((ready = select(2, &pipes, NULL, NULL, NULL)) > 0) {
cat(&pipes, pipe1[0]);
cat(&pipes, pipe2[0]);
} else {
// no time limit, so there was an error
perror("Error waiting for input");
exit(EXIT_FAILURE);
}
FD_SET(pipe1[0],&pipes);
FD_SET(pipe2[0],&pipes);
}
}
Обратите внимание, что вышеописанное работает вечно, если только нет ошибки. Вы, вероятно, захотите, чтобы ваша программа остановилась в какой-то момент.