Skip to content

Instantly share code, notes, and snippets.

@nnathan
Last active May 27, 2019 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nnathan/cd255fb3085dd39628b4a9904c63f8a7 to your computer and use it in GitHub Desktop.
Save nnathan/cd255fb3085dd39628b4a9904c63f8a7 to your computer and use it in GitHub Desktop.
example poll program
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/poll.h>
#define TIMEOUT 5
int main (void)
{
struct pollfd fds[1];
int ret;
int fd = open("/dev/urandom", O_RDWR);
if (fd == -1) {
perror ("open");
return 1;
}
fds[0].fd = fd;
fds[0].events = POLLIN|POLLOUT;
ret = poll(fds, 1, TIMEOUT * 1000);
if (ret == -1) {
perror ("poll");
return 1;
}
if (!ret) {
printf ("%d seconds elapsed.\n", TIMEOUT);
return 0;
}
if (fds[0].revents & POLLIN)
printf ("urandom is readable\n");
if (fds[0].revents & POLLOUT)
printf ("urandom is writable\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment