Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6319358
  • 博文数量: 2759
  • 博客积分: 1021
  • 博客等级: 中士
  • 技术积分: 4091
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-11 14:14
文章分类

全部博文(2759)

文章存档

2019年(1)

2017年(84)

2016年(196)

2015年(204)

2014年(636)

2013年(1176)

2012年(463)

分类:

2012-08-29 00:13:01

原文地址:eventfd以及epoll原理分析 作者:buaa_zhaoc

这两天公司代码中用到了epoll。然后在跟同事闲扯的过程中发现了Linux中有eventfd。两者虽然名字看起来差不多,但是相关性倒是不多。
为了弄明白这两个东西到底在内核上是怎么实现的,这两天将内核这两个部分的相关代码看了下,也终于明白了这两个东西的实现机制。
后续几篇博客我尽量将这两个东西的工作原理阐述清楚,但是自己的语言表达能力比较差,也只能是尽量了。

今天这篇博客首先是介绍两者的使用方式,恰巧两者能够在一个程序中搞定,我就写了下面的小程序来展示两者的功能。
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/time.h>
  4. #include <stdint.h>
  5. #include <pthread.h>
  6. #include <sys/eventfd.h>
  7. #include <sys/epoll.h>

  8. int efd = -1;

  9. void *read_thread(void *dummy)
  10. {
  11.     int ret = 0;
  12.     uint64_t count = 0;
  13.     int ep_fd = -1;
  14.     struct epoll_event events[10];

  15.     if (efd < 0)
  16.     {
  17.         printf("efd not inited.\n");
  18.         goto fail;
  19.     }

  20.     ep_fd = epoll_create(1024);
  21.     if (ep_fd < 0)
  22.     {
  23.         perror("epoll_create fail: ");
  24.         goto fail;
  25.     }

  26.     {
  27.         struct epoll_event read_event;

  28.         read_event.events = EPOLLHUP | EPOLLERR | EPOLLIN;
  29.         read_event.data.fd = efd;

  30.         ret = epoll_ctl(ep_fd, EPOLL_CTL_ADD, efd, &read_event);
  31.         if (ret < 0)
  32.         {
  33.             perror("epoll ctl failed:");
  34.             goto fail;
  35.         }
  36.     }

  37.     while (1)
  38.     {
  39.         ret = epoll_wait(ep_fd, &events[0], 10, 5000);
  40.         if (ret > 0)
  41.         {
  42.             int i = 0;
  43.             for (; i < ret; i++)
  44.             {
  45.                 if (events[i].events & EPOLLHUP)
  46.                 {
  47.                     printf("epoll eventfd has epoll hup.\n");
  48.                     goto fail;
  49.                 }
  50.                 else if (events[i].events & EPOLLERR)
  51.                 {
  52.                     printf("epoll eventfd has epoll error.\n");
  53.                     goto fail;
  54.                 }
  55.                 else if (events[i].events & EPOLLIN)
  56.                 {
  57.                     int event_fd = events[i].data.fd;
  58.                     ret = read(event_fd, &count, sizeof(count));
  59.                     if (ret < 0)
  60.                     {
  61.                         perror("read fail:");
  62.                         goto fail;
  63.                     }
  64.                     else
  65.                     {
  66.                         struct timeval tv;

  67.                         gettimeofday(&tv, NULL);
  68.                         printf("success read from efd, read %d bytes(%llu) at %lds %ldus\n",
  69.                                ret, count, tv.tv_sec, tv.tv_usec);
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.         else if (ret == 0)
  75.         {
  76.             /* time out */
  77.             printf("epoll wait timed out.\n");
  78.             break;
  79.         }
  80.         else
  81.         {
  82.             perror("epoll wait error:");
  83.             goto fail;
  84.         }
  85.     }

  86. fail:
  87.     if (ep_fd >= 0)
  88.     {
  89.         close(ep_fd);
  90.         ep_fd = -1;
  91.     }

  92.     return NULL;
  93. }

  94. int main(int argc, char *argv[])
  95. {
  96.     pthread_t pid = 0;
  97.     uint64_t count = 0;
  98.     int ret = 0;
  99.     int i = 0;

  100.     efd = eventfd(0, 0);
  101.     if (efd < 0)
  102.     {
  103.         perror("eventfd failed.");
  104.         goto fail;
  105.     }

  106.     ret = pthread_create(&pid, NULL, read_thread, NULL);
  107.     if (ret < 0)
  108.     {
  109.         perror("pthread create:");
  110.         goto fail;
  111.     }

  112.     for (i = 0; i < 5; i++)
  113.     {
  114.         count = 4;
  115.         ret = write(efd, &count, sizeof(count));
  116.         if (ret < 0)
  117.         {
  118.             perror("write event fd fail:");
  119.             goto fail;
  120.         }
  121.         else
  122.         {
  123.             struct timeval tv;

  124.             gettimeofday(&tv, NULL);
  125.             printf("success write to efd, write %d bytes(%llu) at %lds %ldus\n",
  126.                    ret, count, tv.tv_sec, tv.tv_usec);
  127.         }

  128.         sleep(1);
  129.     }

  130. fail:
  131.     if (0 != pid)
  132.     {
  133.         pthread_join(pid, NULL);
  134.         pid = 0;
  135.     }

  136.     if (efd >= 0)
  137.     {
  138.         close(efd);
  139.         efd = -1;
  140.     }
  141.     return ret;
  142. }

  1. gcc main.c -Werror -Wall -lpthread
最后执行效果为
  1. success write to efd, write 8 bytes(4) at 1328805612s 21939us
  2. success read from efd, read 8 bytes(4) at 1328805612s 21997us
  3. success write to efd, write 8 bytes(4) at 1328805613s 22247us
  4. success read from efd, read 8 bytes(4) at 1328805613s 22287us
  5. success write to efd, write 8 bytes(4) at 1328805614s 22462us
  6. success read from efd, read 8 bytes(4) at 1328805614s 22503us
  7. success write to efd, write 8 bytes(4) at 1328805615s 22688us
  8. success read from efd, read 8 bytes(4) at 1328805615s 22726us
  9. success write to efd, write 8 bytes(4) at 1328805616s 22973us
  10. success read from efd, read 8 bytes(4) at 1328805616s 23007us
  11. epoll wait timed out.
eventfd具体与pipe有点像,用来完成两个线程之间事件触发,但是同事说现在已经支持到进程级别,现在我还没有验证过。能够用来作为线程之间简单通讯,类似于pthread_cond_t。
epoll则是linux提供的一种多路复用技术,完成与select,poll等一样的功能,完成对多个文件描述符进行等待。本文上述代码仅仅用到了一个文件描述符。
epoll比select的优势网络随处可见,这就不多说了。

应用程序就已经写到这了,对于内核里面具体是怎么搞的,将会在以后的博客中尽量解释清楚。






阅读(760) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~