Advertisement
Guest User

Untitled

a guest
Feb 25th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. #include <sys/select.h>
  2. #include <sys/time.h>
  3. #include <sys/stat.h>
  4. #include <stddef.h>
  5. #include <fcntl.h>
  6. #include <stdio.h>
  7.  
  8. #define nfds 2
  9.  
  10. int
  11. main() {
  12.  
  13. fd_set read, write;
  14.  
  15. struct timeval timeout;
  16. int fd1 = open("fds/1", O_RDWR),
  17. fd2 = open("fds/2", O_RDWR);
  18.  
  19. printf("fd: %i\n", fd1);
  20. printf("fd: %i\n", fd2);
  21.  
  22. int ready;
  23.  
  24. FD_ZERO(&read);
  25. FD_ZERO(&write);
  26.  
  27. FD_SET(fd1, &read);
  28. FD_SET(fd2, &read);
  29.  
  30. for(;;){
  31. timeout.tv_sec = 5;
  32. timeout.tv_usec = 0;
  33.  
  34. ready = select(nfds + 1, &read, NULL, NULL, &timeout);
  35. printf("ready = %i\t", ready);
  36.  
  37. if (ready < 0) {
  38. perror("fail\n");
  39. return 1;
  40. } else if (ready > 0) {
  41. printf("reading\n");
  42. } else {
  43. printf("no data within time interval\n");
  44. }
  45. }
  46.  
  47. return 0;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement