epoll.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include <fcntl.h>
  2. #include <stdio.h>
  3. #include <sys/epoll.h>
  4. #include <sys/wait.h>
  5. #include <unistd.h>
  6. int reader(int fd) {
  7. // Create an epoll file
  8. int epollfd = epoll_create1(EPOLL_CLOEXEC);
  9. if (epollfd < 0) {
  10. perror("epoll_create1");
  11. return 1;
  12. }
  13. // Register for events from the reader file
  14. struct epoll_event ev;
  15. ev.events = EPOLLIN;
  16. ev.data.fd = fd;
  17. if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) < 0) {
  18. perror("epoll_ctl");
  19. return 1;
  20. }
  21. // Process exactly 1024 events
  22. struct epoll_event events[8];
  23. for (int i = 0; i < 1024; i++) {
  24. // Wait for the next event
  25. int nfds = epoll_wait(epollfd, events, sizeof(events)/sizeof(struct epoll_event), -1);
  26. if (nfds < 0) {
  27. perror("epoll_wait");
  28. return 1;
  29. }
  30. // For each event received
  31. for (int n = 0; n < nfds; n++) {
  32. // If the event is the reader file
  33. if (events[n].data.fd == fd) {
  34. // Read the current event count
  35. int writer_i;
  36. int count = read(fd, &writer_i, sizeof(writer_i));
  37. if (count < 0) {
  38. perror("read");
  39. return 1;
  40. } else if (count < sizeof(writer_i)) {
  41. fprintf(stderr, "read %d instead of %d\n", count, sizeof(writer_i));
  42. return 1;
  43. }
  44. // Make sure the writer's event count matches our own
  45. if (i != writer_i) {
  46. fprintf(stderr, "received event count %d instead of %d\n", writer_i, i);
  47. return 1;
  48. }
  49. printf("%d == %d\n", i, writer_i);
  50. } else {
  51. // Otherwise, return an error
  52. fprintf(stderr, "unknown fd %d\n", events[n].data.fd);
  53. return 1;
  54. }
  55. }
  56. }
  57. return 0;
  58. }
  59. int writer(int fd) {
  60. // Create an epoll file
  61. int epollfd = epoll_create1(EPOLL_CLOEXEC);
  62. if (epollfd < 0) {
  63. perror("epoll_create1");
  64. return 1;
  65. }
  66. // Register for events from the writer file
  67. struct epoll_event ev;
  68. ev.events = EPOLLOUT;
  69. ev.data.fd = fd;
  70. if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) < 0) {
  71. perror("epoll_ctl");
  72. return 1;
  73. }
  74. // Process exactly 1024 events
  75. struct epoll_event events[8];
  76. for (int i = 0; i < 1024; i++) {
  77. // Wait for the next event
  78. int nfds = epoll_wait(epollfd, events, sizeof(events)/sizeof(struct epoll_event), -1);
  79. if (nfds < 0) {
  80. perror("epoll_wait");
  81. return 1;
  82. }
  83. // For each event received
  84. for (int n = 0; n < nfds; n++) {
  85. // If the event is the writer file
  86. if (events[n].data.fd == fd) {
  87. // Write the current event count
  88. int count = write(fd, &i, sizeof(i));
  89. if (count < 0) {
  90. perror("write");
  91. return 1;
  92. } else if (count < sizeof(i)) {
  93. fprintf(stderr, "wrote %d instead of %d\n", count, sizeof(i));
  94. return 1;
  95. }
  96. } else {
  97. // Otherwise, return an error
  98. fprintf(stderr, "unknown fd %d\n", events[n].data.fd);
  99. return 1;
  100. }
  101. }
  102. }
  103. return 0;
  104. }
  105. int main(int argc, char **argv) {
  106. // Create a non-blocking pipe to use for epoll testing
  107. int pipefd[2];
  108. if (pipe2(pipefd, O_CLOEXEC | O_NONBLOCK) < 0) {
  109. perror("pipe2");
  110. return 1;
  111. }
  112. pid_t pid = fork();
  113. if (pid < 0) {
  114. perror("fork");
  115. return 1;
  116. } else if (pid == 0) {
  117. // Child process will read events
  118. close(pipefd[1]);
  119. return reader(pipefd[0]);
  120. } else {
  121. // Parent process will write events
  122. close(pipefd[0]);
  123. int ret = writer(pipefd[1]);
  124. // Wait for child process
  125. int status = 0;
  126. if (waitpid(pid, &status, 0) != pid) {
  127. perror("waitpid");
  128. return 1;
  129. }
  130. // If writer failed, return exit status
  131. if (ret != 0) {
  132. return ret;
  133. }
  134. // If child exited with exit status
  135. if (WIFEXITED(status)) {
  136. // Return the child's exit status
  137. return WEXITSTATUS(status);
  138. } else {
  139. // Otherwise, return 1
  140. return 1;
  141. }
  142. }
  143. }