linux epoll系列5 解除epoll_wait状态

有时候会有解除epoll_wait状态的需求。

实现方法:

1,给执行epoll_wait的程序发signal。

2,使用sockpair。

1,给执行epoll_wait的程序发signal。

#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <sys/epoll.h> void sigusr1_handler(int sig){
write(fileno(stdout), "signal called\n", 14);
} int main(){
int nfds;
int epfd; signal(SIGUSR1, sigusr1_handler); epfd = epoll_create(1);
if(epfd < 0){
perror("epoll_crreate");
return 1;
} printf("before epoll_wait\n"); //一直等下去
nfds = epoll_wait(epfd, NULL, 1, -1);
printf("after epoll_wait:%d\n", nfds); printf("%d\n", errno);
perror("perror after epoll_wait"); return 0;
}

github源代码

执行方法:

1,执行程序

2,先用下面的命令找到当前执行程序的PID

ps -e | grep a.out

结果:

ys@ys-VirtualBox:~/cpp/network$ ps -e | grep a.out
2882 pts/0 00:00:00 a.out

3,给个执行中的程序发signal

kill -s SIGUSR1 2882

结果:

before epoll_wait
signal called
after epoll_wait:-1
4
perror after epoll_wait: Interrupted system call

2,使用sockpair。

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/epoll.h> #define EVENTS 8 int soc[2]; void processA(){
sleep(3);
printf("processA: send message\n");
write(soc[0], "HELLO\n", 6);
return;
} void processB(){
int epfd;
epoll_event ev, ev_ret[EVENTS];
int nfds;
int i;
char buf[128]; epfd = epoll_create(1);
if(epfd < 0){
perror("epoll_create");
return ;
} memset(&ev, 0, sizeof(ev));
ev.events = EPOLLIN;
ev.data.fd = soc[1]; if(epoll_ctl(epfd, EPOLL_CTL_ADD, soc[1], &ev) != 0){
perror("epoll_clt");
return ;
} memset(&ev, 0, sizeof(ev));
ev.events = EPOLLIN;
ev.data.fd = fileno(stdin); if(epoll_ctl(epfd, EPOLL_CTL_ADD, fileno(stdin), &ev) != 0){
perror("epoll_clt1");
return ;
} while(1){
printf("before epoll_wait\n"); nfds = epoll_wait(epfd, ev_ret, EVENTS , -1);
if(nfds < 0){
perror("epoll_wait");
return;
} printf("after epoll_wait\n"); for(i = 0; i < nfds; ++i){
if(ev_ret[i].data.fd == soc[1]){
printf("processB:break message from socketpair\n");
goto outofloop;
}
else if(ev_ret[i].data.fd == fileno(stdin)){
read(fileno(stdin), buf, sizeof(buf));
printf("processB:input from stdin\n");
}
}
} outofloop:
printf("process B:outside of loop\n"); return ;
}
int main(){
int ret; ret = socketpair(AF_UNIX, SOCK_STREAM, 0, soc);
if(ret != 0){
perror("socketpair");
return 1;
} if(fork() == 0){
processA();
}
else{
processB();
} return 0;
}

github源代码

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

c/c++ llinux epoll系列5 解除epoll_wait状态的更多相关文章

  1. c/c++ llinux epoll系列4 利用epoll_wait实现非阻塞的connect

    llinux epoll系列4 利用epoll_wait实现非阻塞的connect connect函数是阻塞的,而且不能设置connect函数的timeout时间,所以一旦阻塞太长时间,影响用户的体验 ...

  2. c/c++ linux epoll系列3 利用epoll_wait设置timeout时间长度

    linux epoll系列3 利用epoll_wait设置timeout时间长度 epoll_wait函数的第四个参数可以设置,epoll_wait函数的等待时间(timeout时间长度). 例子1, ...

  3. c/c++ linux epoll系列2 利用epoll_wait查看是否可以送信

    linux epoll系列2 利用epoll_wait查看是否可以送信 write函数本来是非阻塞函数,但是当缓存区被写满后,再往缓存区里写的时候,就必须等待缓存区再次变成可写,所以这是write就变 ...

  4. UNIX网络编程——epoll 系列函数简介、与select、poll 的区别

    前面博客<<UNIX环境高级编程--epoll函数使用详解>>有关于epoll函数的讲解. 一.epoll 系列函数简介 #include <sys/epoll.h> ...

  5. c/c++ linux epoll系列1 创建epoll

    linux epoll系列1 创建epoll 据说select和poll的弱点是,随着连接(socket)的增加,性能会直线下降. epoll不会随着连接(socket)的增加,性能直线下降. 知识点 ...

  6. epoll 系列函数简介、与select、poll 的区别

    一.epoll 系列函数简介 #include <sys/epoll.h> int epoll_create(int size); int epoll_create1(int flags) ...

  7. IO复用——epoll系列系统调用

    1.内核事件表 epoll是Linux特有的I/O复用函数.epoll把用户关心的文件描述上的事件放在内核里的一个事件表中,并用一个额外的文件描述符来标识该内核事件表.这个额外文件描述符使用函数epo ...

  8. IO复用之epoll系列

    epoll是什么? epoll是Linux内核为处理大批量文件描述符而作了改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显著提高程序在大量并发连接中只有少量活跃的 ...

  9. HDU 4540 威威猫系列故事——打地鼠 (状态压缩DP)

    威威猫系列故事——打地鼠 Time Limit: 300/100 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total ...

随机推荐

  1. MyBatis 处理关系运算符

    MyBatis mapper文件是xml文件,使用关系运算符需要进行转义. 关系运算符 转义后字符 < < <= <= > > > >=

  2. scala的reduce

    spark 中的 reduce 非常的好用,reduce 可以对 dataframe 中的元素进行计算.拼接等等.例如生成了一个 dataframe : //配置spark def getSparkS ...

  3. Java 多线程(一)—— 概念的引入

      并发和并行 并行:指两个或多个时间在同一时刻发生(同时发生): 并发:指两个或多个事件在一个时间段内发生. 在操作系统中,安装了多个程序,并发指的是在一段时间内宏观上有多个程序同时运行,这在单 C ...

  4. [Python Web]部署完网站需要做的基本后续工作

    简述 今天自己上线了一个简单的 Page,没有什么功能就是一个展示页. 但是,我发现部署完,上线后,还要弄不少东西.下面就是我记录.整理的一些上线网站基本都会用到的网站和配置. 加入统计代码 这个是必 ...

  5. 配置最新版LAMP环境

    本篇文章讲解的是在centos7.3下配置 Apache2.4 + MySQL5.7 + PHP7.1.8 (如果是Nginx请跳过Apache流程继续往下看,所有流程本人已临床验证无数遍,绝无问题) ...

  6. MaxCompute安全管理指南-基础篇

    背景及目的 方便和辅助MaxCompute的project owner或安全管理员进行project的日常安全运维,保障数据安全. MaxCompute有安全模型,DataWorks也有安全模型,当通 ...

  7. 痞子衡嵌入式:ARM Cortex-M文件那些事(7)- 反汇编文件(.s/.lst/.dump)

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家讲的是嵌入式开发里的反汇编文件(.s, .lst, .dump). 痞子衡在第四.五.六节课分别介绍了编译器/链接器生成的3种output文件( ...

  8. Perl线程池

    Thread::Pool模块提供了Perl解释器线程的线程池,手册:https://metacpan.org/pod/Thread::Pool.

  9. C# Task 篇幅一

    在https://www.cnblogs.com/loverwangshan/p/10415937.html中我们有讲到委托的异步方法,Thread,ThreadPool,然后今天来讲一下Task, ...

  10. python-IO编程,文件读写

    一.文件读写 1.打开文件 函数:open(name[. mode[. buffering]]) 参数: name:必须:文件的文件名(全路径或执行文件的相对路径.)) mode:可选:对文件的读写模 ...