Guest User

Untitled

a guest
Nov 21st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <errno.h>
  5. #include <pthread.h>
  6. #include <sys/epoll.h>
  7.  
  8. #define MAX_EVENTS 10
  9.  
  10. void* thr_epoll_wait(void* arg)
  11. {
  12. int fd = (int) arg;
  13. struct epoll_event ev, events[MAX_EVENTS];
  14. int nfds, epollfd;
  15. int i;
  16.  
  17. fprintf(stderr, "sleep\n");
  18. sleep(1);
  19.  
  20. epollfd = epoll_create(10);
  21. if (epollfd == -1) {
  22. perror("epoll_create");
  23. exit(EXIT_FAILURE);
  24. }
  25.  
  26. ev.events = EPOLLIN | EPOLLET;
  27. ev.data.fd = fd;
  28. if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev) == -1) {
  29. perror("epoll_ctl ");
  30. exit(EXIT_FAILURE);
  31. }
  32.  
  33. nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
  34. if (nfds == -1) {
  35. perror("epoll_pwait");
  36. exit(EXIT_FAILURE);
  37. }
  38.  
  39. fprintf(stderr, "%lx: get events\n", (unsigned long)pthread_self());
  40. for (i=0; i < nfds; i++) {
  41. fprintf(stderr, "%lx: fd=%d\n", (unsigned long)pthread_self(), events[i].data.fd);
  42. }
  43.  
  44. return NULL;
  45. }
  46.  
  47. int main(void)
  48. {
  49. int pipefds[2];
  50. int err;
  51. pthread_t thr;
  52.  
  53. err = pipe(pipefds);
  54. if (err < 0) {
  55. perror("pipe ");
  56. exit(1);
  57. }
  58. fprintf(stderr, "pipe fds=%d %d\n", pipefds[0], pipefds[1]);
  59.  
  60. err = pthread_create(&thr, NULL, thr_epoll_wait, (void*)pipefds[0]);
  61. if (err != 0) {
  62. errno = err;
  63. perror("pthread_create ");
  64. exit(1);
  65. }
  66. err = pthread_create(&thr, NULL, thr_epoll_wait, (void*)pipefds[0]);
  67. if (err != 0) {
  68. errno = err;
  69. perror("pthread_create ");
  70. exit(1);
  71. }
  72.  
  73. fprintf(stderr, "write\n");
  74. write(pipefds[1], "a", 1);
  75.  
  76. fprintf(stderr, "pause\n");
  77. pause();
  78.  
  79. return 0;
  80. }
Add Comment
Please, Sign In to add comment