1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| #include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <poll.h>
#define FD_COUNT 2
#define TUBE_COUNT 2
int main(int argc, char *argv[])
{
struct pollfd fds[FD_COUNT];
int timeout = -1;
char buffer[6];
for (int i = 0; i < FD_COUNT; ++i)
{
fds[i].events = POLLIN;
}
int tube[TUBE_COUNT][2];
for (int i = 0; i < TUBE_COUNT; ++i)
{
if( pipe(tube[i]) < 0 )
{
printf("pipe : errno = %i\n", errno);
}
close(tube[i][1]);
}
if (mkfifo("pipeNamed1", 0666) < 0)
{
printf("pipeNamed : errno = %i\n", errno);
}
if (mkfifo("pipeNamed2", 0666) < 0)
{
printf("pipeNamed : errno = %i\n", errno);
}
int fd_open1 = open("pipeNamed1", O_RDONLY | O_NONBLOCK);
if ( fd_open1 < 0 )
{
printf("fd_open1 : errno = %i\n", errno);
}
int fd_open2 = open("pipeNamed2", O_RDONLY | O_NONBLOCK);
if ( fd_open1 < 0 )
{
printf("fd_open2 : errno = %i\n", errno);
}
fds[0].fd = fd_open1;
fds[1].fd = fd_open2;
printf("boucle :\n");
while(1)
{
int fd_ready = poll( fds, 2, timeout );
printf("fd_ready = %i\trevents = %i\n", fd_ready,
fds[fd_ready -1].revents);
read(fds[fd_ready - 1].fd, buffer, 6 );
printf("buffer = %s\n", buffer);
}
} |
Partager