线程间通信之eventfd

线程间通信之eventfd

man手册中的解释:
eventfd()创建了一个“eventfd对象”, 通过它能够实现用户态程序间(我觉得这里主要指线程而非进程)的等待/通知机制,以及内核态向用户态通知的机制(未考证)。
此对象包含了一个被内核所维护的计数(uint64_t), 初始值由initval来决定。


int eventfd(unsigned int initval, int flags);创建一个eventfd文件描述符
int eventfd_read(int fd, eventfd_t *value); 向eventfd中写入一个值
int eventfd_write(int fd, eventfd_t value); 从eventfd中读出一个值

例一、子线程多次写入多个值,主线程一次读出所有值的和

复制代码

 1 #include <sys/eventfd.h>2 #include <unistd.h>3 #include <stdlib.h>4 #include <stdio.h>5 #include <stdint.h>  6 7 int main(int argc, char**argv[])8 {9     int efd, j;
10     uint64_t u;
11     ssize_t s;
12     
13     if (argc < 2)
14     {
15         printf("number of argc is wrong!\n");
16         return 0;
17     }
18     
19     efd = eventfd(0,0);
20     if (-1 == efd)
21     {
22         printf("failed to create eventfd\n");
23     }
24     
25     switch(fork())
26     {
27         case 0:
28         {
29             for(j=1; j<argc;j++)
30             {
31                 printf("child writing %s to efd\n", argv[j]);
32                 u = strtoull(argv[j], NULL, 0);
33                 s = write(efd, &u, sizeof(uint64_t));
34                 if (s!=sizeof(uint64_t))
35                 {
36                     printf("write efd failed\n");
37                 }
38             }
39             printf("Child completed write loop\n");
40             exit(0);
41         }
42         default:
43             sleep(2);
44             printf("Parents about to read\n");
45             s = read(efd, &u, sizeof(uint64_t));
46             if (s != sizeof(uint64_t))
47             {
48                 printf("read efd failed\n");
49             }
50             printf("Parents first read %llu (0x%llx) from efd\n", u, u);
51             exit(0);
52         case -1:
53         {
54             printf("fork error\n");
55         }
56     }
57     
58     return 0;
59 }
60 
61 运行结果
62 kane@kanelinux:/mnt/hgfs/kanelinuxshare/eventfd$ ./a.out 1 2 3 4
63 child writing 1 to efd
64 child writing 2 to efd
65 child writing 3 to efd
66 child writing 4 to efd
67 Child completed write loop
68 Parents about to read
69 Parents first read 10 (0xa) from efd
70 
71 如果有写入操作,但是并没有导致初始值变化,则主线程会一直挂在read操作上
72 kane@kanelinux:/mnt/hgfs/kanelinuxshare/eventfd$ ./a.out 0 0 0 0
73 child writing 0 to efd
74 child writing 0 to efd
75 child writing 0 to efd
76 child writing 0 to efd
77 Child completed write loop
78 Parents about to read
79 ^C

复制代码

 

例二、eventfd可以被epoll监控, 一旦有状态变化,可以触发通知

复制代码

  1 #include <sys/eventfd.h>2 #include <unistd.h>3 #include <stdlib.h>4 #include <stdio.h>5 #include <stdint.h>  6 #include <sys/epoll.h>  7 #include <string.h>  8 #include <pthread.h>  9 10 int g_iEvtfd = -1;11 12 void *eventfd_child_Task(void *pArg)13 {14     uint64_t uiWrite = 1;15     16     while(1)17     {18         sleep(2);19         if (0 != eventfd_write(g_iEvtfd, uiWrite))20         {21             printf("child write iEvtfd failed\n");22         }    23     }24 25     return;26 }27 28 int main(int argc, char**argv[])29 {30     int iEvtfd, j;31     uint64_t uiWrite = 1;32     uint64_t uiRead;33     ssize_t s;34     int iEpfd;35     struct epoll_event stEvent;36     int iRet = 0;37     struct epoll_event stEpEvent;38     pthread_t stWthread;39     40     iEpfd = epoll_create(1);41     if (-1 == iEpfd)42     {43         printf("Create epoll failed.\n");44         return 0;45     }46     47     iEvtfd = eventfd(0,0);48     if (-1 == iEvtfd)49     {50         printf("failed to create eventfd\n");51         return 0;52     }53     54     g_iEvtfd = iEvtfd;55     56     memset(&stEvent, 0, sizeof(struct epoll_event));57     stEvent.events = (unsigned long) EPOLLIN;58     stEvent.data.fd = iEvtfd;59     iRet = epoll_ctl(iEpfd, EPOLL_CTL_ADD, g_iEvtfd, &stEvent);60     if (0 != iRet)61     {62         printf("failed to add iEvtfd to epoll\n");63         close(g_iEvtfd);64         close(iEpfd);65         return 0;66     }67     68     iRet = pthread_create(&stWthread, NULL, eventfd_child_Task, NULL);69     if (0 != iRet)70     {71         close(g_iEvtfd);72         close(iEpfd);73         return;74     }75     76     for(;;)77     {78         iRet = epoll_wait(iEpfd, &stEpEvent, 1, -1);79         if (iRet > 0)80         {81             s = eventfd_read(iEvtfd, &uiRead);82             if (s != 0)83             {84                 printf("read iEvtfd failed\n");85                 break;86             }87             printf("Read %llu (0x%llx) from iEvtfd\n", uiRead, uiRead);88         }89     }90     91     close(g_iEvtfd);92     close(iEpfd);93     return 0;94 }95 运行结果96 kane@kanelinux:/mnt/hgfs/kanelinuxshare/eventfd$ ./a.out97 Read 1 (0x1) from iEvtfd98 Read 1 (0x1) from iEvtfd99 Read 1 (0x1) from iEvtfd
100 Read 1 (0x1) from iEvtfd
101 Read 1 (0x1) from iEvtfd
102 Read 1 (0x1) from iEvtfd
103 Read 1 (0x1) from iEvtfd
104 Read 1 (0x1) from iEvtfd
105 ^C

复制代码

例三、被epoll监控的eventfd,如果在子线程中被多次写入,在主线程中是怎么读的?

复制代码

#include <sys/eventfd.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>  
#include <sys/epoll.h>  
#include <string.h>  
#include <pthread.h>  int g_iEvtfd = -1;void *eventfd_child_Task(void *pArg)
{uint64_t uiWrite = 1;while(1){sleep(2);eventfd_write(g_iEvtfd, uiWrite);eventfd_write(g_iEvtfd, uiWrite);}return;
}int main(int argc, char**argv[])
{int iEvtfd, j;uint64_t uiWrite = 1;uint64_t uiRead;ssize_t s;int iEpfd;struct epoll_event stEvent;int iRet = 0;struct epoll_event stEpEvent;pthread_t stWthread;iEpfd = epoll_create(1);if (-1 == iEpfd){printf("Create epoll failed.\n");return 0;}iEvtfd = eventfd(0,0);if (-1 == iEvtfd){printf("failed to create eventfd\n");return 0;}g_iEvtfd = iEvtfd;memset(&stEvent, 0, sizeof(struct epoll_event));stEvent.events = (unsigned long) EPOLLIN;stEvent.data.fd = iEvtfd;iRet = epoll_ctl(iEpfd, EPOLL_CTL_ADD, g_iEvtfd, &stEvent);if (0 != iRet){printf("failed to add iEvtfd to epoll\n");close(g_iEvtfd);close(iEpfd);return 0;}iRet = pthread_create(&stWthread, NULL, eventfd_child_Task, NULL);if (0 != iRet){close(g_iEvtfd);close(iEpfd);return;}for(;;){iRet = epoll_wait(iEpfd, &stEpEvent, 1, -1);if (iRet > 0){s = eventfd_read(iEvtfd, &uiRead);if (s != 0){printf("read iEvtfd failed\n");break;}printf("Read %llu (0x%llx) from iEvtfd\n", uiRead, uiRead);}}close(g_iEvtfd);close(iEpfd);return 0;
}运行结果:
kane@kanelinux:/mnt/hgfs/kanelinuxshare/eventfd$ ./a.out
Read 1 (0x1) from iEvtfd
Read 1 (0x1) from iEvtfdRead 1 (0x1) from iEvtfd
Read 1 (0x1) from iEvtfdRead 1 (0x1) from iEvtfd
Read 1 (0x1) from iEvtfdRead 1 (0x1) from iEvtfd
Read 1 (0x1) from iEvtfd^C

复制代码

例一中并没有epoll做监控,
因此在read前,如果eventfd被写多次,在read的时候也是一次全部读出。

 

注:eventfd中的SEMAPHORE标志用法

* If EFD_SEMAPHORE was not specified and the eventfd counter
has a nonzero value, then a read(2) returns 8 bytes contain‐
ing that value, and the counter's value is reset to zero.

* If EFD_SEMAPHORE was specified and the eventfd counter has a
nonzero value, then a read(2) returns 8 bytes containing the
value 1, and the counter's value is decremented by 1.

通过测试发现。如果eventfd在创建的时候传入EFD_SEMAPHORE 标志,则会按上面man手册中提到的那样,每次在eventfd_read的时候只减一,并不是把值一次性全部读出。见下例 :

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/384983.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【linux 开发】定时器使用setitimer

setitimer Linux 为每一个进程提供了 3 个 setitimer 间隔计时器&#xff1a; ITIMER_REAL&#xff1a;减少实际时间&#xff0c;到期的时候发出 SIGALRM 信号。ITIMER_VIRTUAL&#xff1a;减少有效时间 (进程执行的时间)&#xff0c;产生 SIGVTALRM 信号。ITIMER_PROF&#…

文件操作(写)

/*** file.c ***/ #include<stdio.h>int main() {//用写的方式打开一个文件 //w的意思是文件如果不存在&#xff0c;就建立一个文件&#xff0c;如果文件存在就覆盖FILE *p fopen("/home/exbot/wangqinghe/C/20190716/file1.txt","w");fputs(&qu…

定时器timerfd

1.为什么要加入此定时器接口 linux2.6.25版本新增了timerfd这个供用户程序使用的定时接口&#xff0c;这个接口基于文件描述符&#xff0c;当超时事件发生时&#xff0c;该文件描述符就变为可读。我首次接触这个新特性是在muduo网络库的定时器里看到的&#xff0c;那么新增一个…

文件操作(读)

读一行&#xff1a; #include<stdio.h> #include<string.h> #include<stdlib.h> const int maxn 10; int main() {char s[1024] {0};FILE *p fopen("/home/exbot/wangqinghe/C/20190716/file.txt","r");//第一个参数是一个内存地址&…

timerfd与epoll

linux timerfd系列函数总结 网上关于timerfd的文章很多&#xff0c;在这儿归纳总结一下方便以后使用&#xff0c;顺便贴出一个timerfd配合epoll使用的简单例子 一、timerfd系列函数 timerfd是Linux为用户程序提供的一个定时器接口。这个接口基于文件描述符&#xff0c;通过文…

文件操作(解密加密)

文件加密&#xff1a; #include<stdio.h> #include<string.h> #include<stdlib.h>void code(char *s) {while(*s){(*s);s;} }int main() {char s[1024] {0};FILE *p fopen("/home/exbot/wangqinghe/C/20190716/file.txt","r");FILE *p…

linux僵尸进程产生的原因以及如何避免产生僵尸进程defunct

给进程设置僵尸状态的目的是维护子进程的信息&#xff0c;以便父进程在以后某个时间获取。这些信息包括子进程的进程ID、终止状态以及资源利用信息(CPU时间&#xff0c;内存使用量等等)。如果一个进程终止&#xff0c;而该进程有子进程处于僵尸状态&#xff0c;那么它的所有僵尸…

linux下僵尸进程(Defunct进程)的产生与避免

在测试基于 DirectFBGstreamer 的视频联播系统的一个 Demo 的时候&#xff0c;其中大量使用 system 调用的语句&#xff0c;例如在 menu 代码中的 system("./play") &#xff0c;而且多次执行&#xff0c;这种情况下&#xff0c;在 ps -ef 列表中出现了大量的 defunc…

文件操作函数

fopen()函数参数&#xff1a; r 只读的方式打开文件。 打开成功返回文件指针&#xff0c; 打开失败返回NULL r 以读写方式打开文件。 文件必须存在 rb 以二进制模式读写文件&#xff0c;文件必须存在 rw 读写一个二进制文件&#xff0c;允许读和写 w 打开只写文件&…

读过的最好的epoll讲解

首先我们来定义流的概念&#xff0c;一个流可以是文件&#xff0c;socket&#xff0c;pipe等等可以进行I/O操作的内核对象。 不管是文件&#xff0c;还是套接字&#xff0c;还是管道&#xff0c;我们都可以把他们看作流。 之后我们来讨论I/O的操作&#xff0c;通过read&#xf…

文件操作函数(读写)

文件文本排序&#xff1a; 数组冒泡&#xff1a; #include<stdio.h>void swap(int *a,int *b) {int temp *a;*a *b;*b temp; }void bubble(int *p,int n) {int i;int j;for(i 0; i < n; i){for(j 1; j < n - i; j){if(p[j - 1] > p[j]){swap(&p[j-1],&…

文件操作(升级)

计算字符串“25 32 ” #include<stdio.h> #include<string.h>int calc_string(char *s) {char buf1[100] {0};char oper 0;char buf2[100] {0};int len strlen(s);int i;for(i 0; i < len; i){if( s[i] || - s[i] || * s[i] || / s[i] ){strncpy…

C语言指针转换为intptr_t类型

C语言指针转换为intptr_t类型 1、前言 今天在看代码时&#xff0c;发现将之一个指针赋值给一个intptr_t类型的变量。由于之前没有见过intptr_t这样数据类型&#xff0c;凭感觉认为intptr_t是int类型的指针。感觉很奇怪&#xff0c;为何要将一个指针这样做呢&#xff1f;如是果…

nginx epoll详解

nginx epoll 事件模型 nginx做为一个异步高效的事件驱动型web服务器&#xff0c;在linux平台中当系统支持epoll时nginx默认采用epoll来高效的处理事件。nginx中使用ngx_event_t结构来表示一个事件&#xff0c;先介绍下ngx_event_t结构体中成员的含义&#xff1a; struct ngx_ev…

Inotify机制

描述 Inotify API用于检测文件系统变化的机制。Inotify可用于检测单个文件&#xff0c;也可以检测整个目录。当检测的对象是一个目录的时候&#xff0c;目录本身和目录里的内容都会成为检测的对象。 此种机制的出现的目的是当内核空间发生某种事件之后&#xff0c;可以立即通…

文件操作(二进制文件加密解密)

加密 #include<stdio.h> #include<string.h>void code(char *p,size_t n) {size_t i;for(i 0; i < n; i){p[i] 3;} }int main() {FILE *p1 fopen("./a.txt","r");FILE *p2 fopen("./b.txt","w");char buf[1024] {…

北京加密机现场select问题

问题描述 北京项目通过调用我们提供的库libsigxt.a与加密机通信&#xff0c;c/s架构&#xff0c;客户端启用多个线程&#xff0c;每个线程流程有以下三步&#xff0c;连接加密机&#xff0c;签名&#xff0c;关闭链接。在正常运行一段时间后会出现不能连接加密机服务问题。 连…

拼接字符串(带参程序)

1.用strcat拼接函数可以实现 #include<stdio.h> #include<string.h>int main(int argc,char ** argv) {char str[100] {0};int i;for( i 1; i < argc; i){strcat(str,argv[i]);}printf("str %s\n",str);return 0; } 2.用sprintf函数也可以实现 #in…

详细解释signal和sigaction以及SIG_BLOCK

signal&#xff0c;此函数相对简单一些&#xff0c;给定一个信号&#xff0c;给出信号处理函数则可&#xff0c;当然&#xff0c;函数简单&#xff0c;其功能也相对简单许多&#xff0c;简单给出个函数例子如下&#xff1a; [cpp] view plain copy 1 #include <signal.h>…

处理SIGCHLD信号

在上一讲中&#xff0c;我们使用fork函数得到了一个简单的并发服务器。然而&#xff0c;这样的程序有一个问题&#xff0c;就是当子进程终止时&#xff0c;会向父进程发送一个SIGCHLD信号&#xff0c;父进程默认忽略&#xff0c;导致子进程变成一个僵尸进程。僵尸进程一定要处理…