最高效的进(线)程间通信机制--eventfd

我们常用的进程(线程)间通信机制有管道,信号,消息队列,信号量,共享内存,socket等等,其中主要作为进程(线程)间通知/等待的有管道pipe和socketpair。线程还有特别的condition。

今天来看一个liunx较新的系统调用,它是从LINUX 2.6.27版本开始增加的,主要用于进程或者线程间的通信(如通知/等待机制的实现)。


首先来看一下函数原型:

[cpp]  view plain  copy
  1. #include <sys/eventfd.h>  
  2.   
  3.        int eventfd(unsigned int initval, int flags);  

下面是它man手册中的描述,我照着翻译了一遍(我英语四级434,你们要是怀疑下文的话,可以直接去man eventfd ^^):


eventfd()创建了一个"eventfd object",能在用户态用做事件wait/notify机制,通过内核取唤醒用户态的事件。这个对象保存了一个内核维护的uint64_t类型的整型counter。这个counter初始值被参数initval指定,一般初值设置为0。


它的标记可以有以下属性:

EFD_CLOECEX,EFD_NONBLOCK,EFD_SEMAPHORE。

在linux直到版本2.6.26,这个flags参数是没用的,必须指定为0。


它返回了一个引用eventfd object的描述符。这个描述符可以支持以下操作:

read:如果计数值counter的值不为0,读取成功,获得到该值。如果counter的值为0,非阻塞模式,会直接返回失败,并把errno的值指纹EINVAL。如果为阻塞模式,一直会阻塞到counter为非0位置。

write:会增加8字节的整数在计数器counter上,如果counter的值达到0xfffffffffffffffe时,就会阻塞。直到counter的值被read。阻塞和非阻塞情况同上面read一样。

close:这个操作不用说了。


重点是支持这个:

 poll(2), select(2) (and similar)
              The returned file descriptor supports poll(2) (and analogously epoll(7)) and select(2), as follows:
              *  The file descriptor is readable (the select(2) readfds argument; the poll(2) POLLIN flag) if the  counter  has  a
                 value greater than 0.
              *  The  file descriptor is writable (the select(2) writefds argument; the poll(2) POLLOUT flag) if it is possible to
                 write a value of at least "1" without blocking.
              *  If an overflow of the counter value was detected, then select(2) indicates the  file  descriptor  as  being  both
                 readable  and  writable,  and  poll(2)  returns a POLLERR event.  As noted above, write(2) can never overflow the
                 counter.  However an overflow can occur if 2^64 eventfd "signal posts" were performed by the KAIO subsystem (the‐
                 oretically possible, but practically unlikely).  If an overflow has occurred, then read(2) will return that maxi‐
                 mum uint64_t value (i.e., 0xffffffffffffffff).
              The eventfd file descriptor also supports the other file-descriptor multiplexing APIs: pselect(2) and ppoll(2)
.


它的内核代码实现是这样子的:

[cpp]  view plain  copy
  1. int eventfd_signal(struct eventfd_ctx *ctx, int n)  
  2. {  
  3.     unsigned long flags;  
  4.   
  5.     if (n < 0)  
  6.         return -EINVAL;  
  7.     spin_lock_irqsave(&ctx->wqh.lock, flags);  
  8.     if (ULLONG_MAX - ctx->count < n)  
  9.         n = (int) (ULLONG_MAX - ctx->count);  
  10.     ctx->count += n;  
  11.     if (waitqueue_active(&ctx->wqh))  
  12.         wake_up_locked_poll(&ctx->wqh, POLLIN);  
  13.     spin_unlock_irqrestore(&ctx->wqh.lock, flags);  
  14.   
  15.     return n;  
  16. }  

本质就是做了一次唤醒,不用read,也不用write,与eventfd_write的区别是不用阻塞。


说了这么多,我们来看一个例子,理解理解其中的含义:

[cpp]  view plain  copy
  1. #include <sys/eventfd.h>  
  2. #include <unistd.h>  
  3. #include <stdlib.h>  
  4. #include <stdio.h>  
  5. #include <stdint.h>             /* Definition of uint64_t */  
  6.   
  7. #define handle_error(msg) \  
  8.     do { perror(msg); exit(EXIT_FAILURE); } while (0)  
  9.   
  10. int  
  11. main(int argc, char *argv[])  
  12. {  
  13.     int efd, j;  
  14.     uint64_t u;  
  15.     ssize_t s;  
  16.       
  17.     if (argc < 2) {  
  18.         fprintf(stderr, "Usage: %s <num>...\n", argv[0]);  
  19.         exit(EXIT_FAILURE);  
  20.     }     
  21.   
  22.     efd = eventfd(0, 0);   
  23.     if (efd == -1)   
  24.         handle_error("eventfd");  
  25.       
  26.     switch (fork()) {  
  27.         case 0:  
  28.             for (j = 1; j < argc; j++) {  
  29.             printf("Child writing %s to efd\n", argv[j]);  
  30.             u = strtoull(argv[j], NULL, 0);   
  31.             /* strtoull() allows various bases */  
  32.      s = write(efd, &u, sizeof(uint64_t));  
  33.             if (s != sizeof(uint64_t))  
  34.                 handle_error("write");  
  35.             }  
  36.             printf("Child completed write loop\n");  
  37.             exit(EXIT_SUCCESS);  
  38.         default:  
  39.             sleep(2);  
  40.   
  41.             printf("Parent about to read\n");  
  42.             s = read(efd, &u, sizeof(uint64_t));  
  43.             if (s != sizeof(uint64_t))  
  44.                 handle_error("read");  
  45.             printf("Parent read %llu (0x%llx) from efd\n",  
  46.                    (unsigned long long) u, (unsigned long long) u);  
  47.             exit(EXIT_SUCCESS);  
  48.   
  49.         case -1:  
  50.             handle_error("fork");  
  51.   }  
  52. }  
输出:

$ ./a.out 1 2 4 7 14
           Child writing 1 to efd
           Child writing 2 to efd
           Child writing 4 to efd
           Child writing 7 to efd
           Child writing 14 to efd
           Child completed write loop
           Parent about to read
           Parent read 28 (0x1c) from efd

注意:这里用了sleep(2)保证子进程循环写入完毕,得到的值就是综合28。如果不用sleep(2)来保证时序,当子进程写入一个值,父进程会立马从eventfd读出该值。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值