Guest User

Untitled

a guest
Aug 22nd, 2019
66
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h> /* Definition of AT_* constants */
  8. #include <errno.h>
  9. #include <sys/epoll.h>
  10.  
  11. int main(void) {
  12. int fds[2];
  13.  
  14. if (mkfifo("./fifo1", 0644) < 0) {
  15. perror("mkfifo()");
  16. };
  17.  
  18. if (mkfifo("./fifo2", 0644) < 0) {
  19. perror("mkfifo()");
  20. };
  21.  
  22. if ((fds[0] = open("./fifo1", O_RDONLY|O_NONBLOCK, 0644)) < 0)
  23. perror("open()");
  24.  
  25. if ((fds[1] = open("./fifo2", O_RDONLY|O_NONBLOCK, 0644)) < 0)
  26. perror("open()");
  27.  
  28. int epfd;
  29. struct epoll_event ev[2] = {0};
  30. struct epoll_event evlist[2];
  31.  
  32. epfd = epoll_create(2);
  33. if (epfd == -1) {
  34. perror("epoll_create");
  35. exit(EXIT_FAILURE);
  36. }
  37. ev[0].data.fd = fds[0];
  38. ev[0].events = EPOLLIN;
  39. if (epoll_ctl(epfd, EPOLL_CTL_ADD, fds[0], &ev[0]) == -1) {
  40. perror("epoll_ctl");
  41. exit(EXIT_FAILURE);
  42. }
  43.  
  44. ev[1].data.fd = fds[1];
  45. ev[1].events = EPOLLIN;
  46. if (epoll_ctl(epfd, EPOLL_CTL_ADD, fds[1], &ev[1]) == -1) {
  47. perror("epoll_ctl");
  48. exit(EXIT_FAILURE);
  49. }
  50.  
  51. while (1) {
  52. int ready = epoll_wait(epfd, evlist, 2, -1);
  53. if (ready == -1) {
  54. if (errno == EINTR)
  55. continue;
  56. else {
  57. perror("epoll_wait()");
  58. exit(EXIT_FAILURE);
  59. }
  60. }
  61. printf("ready: %d\n", ready);
  62. }
  63. // char buf[4096] = {0};
  64. // ssize_t n;
  65. // ssize_t result;
  66. // size_t remains;
  67. // for (;;) {
  68. // char *p = buf;
  69. // n = 0;
  70. // remains = 4096;
  71. // while (remains > 0) {
  72. // if ((result = read(fds[0], p, remains)) == -1) {
  73. // if (errno == EAGAIN || errno == ETIMEDOUT) {
  74. // sleep(1);
  75. // continue;
  76. // } else {
  77. // perror("read()");
  78. // exit(EXIT_FAILURE);
  79. // }
  80. // }
  81. // n += result;
  82. // remains -= result;
  83. // printf("%s", p);
  84. // p += n;
  85. // }
  86. // }
  87. }
RAW Paste Data