c/c++ llinux epoll系列5 解除epoll_wait状态
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;
}
执行方法:
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;
}
c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854
c/c++ llinux epoll系列5 解除epoll_wait状态的更多相关文章
- c/c++ llinux epoll系列4 利用epoll_wait实现非阻塞的connect
llinux epoll系列4 利用epoll_wait实现非阻塞的connect connect函数是阻塞的,而且不能设置connect函数的timeout时间,所以一旦阻塞太长时间,影响用户的体验 ...
- c/c++ linux epoll系列3 利用epoll_wait设置timeout时间长度
linux epoll系列3 利用epoll_wait设置timeout时间长度 epoll_wait函数的第四个参数可以设置,epoll_wait函数的等待时间(timeout时间长度). 例子1, ...
- c/c++ linux epoll系列2 利用epoll_wait查看是否可以送信
linux epoll系列2 利用epoll_wait查看是否可以送信 write函数本来是非阻塞函数,但是当缓存区被写满后,再往缓存区里写的时候,就必须等待缓存区再次变成可写,所以这是write就变 ...
- UNIX网络编程——epoll 系列函数简介、与select、poll 的区别
前面博客<<UNIX环境高级编程--epoll函数使用详解>>有关于epoll函数的讲解. 一.epoll 系列函数简介 #include <sys/epoll.h> ...
- c/c++ linux epoll系列1 创建epoll
linux epoll系列1 创建epoll 据说select和poll的弱点是,随着连接(socket)的增加,性能会直线下降. epoll不会随着连接(socket)的增加,性能直线下降. 知识点 ...
- epoll 系列函数简介、与select、poll 的区别
一.epoll 系列函数简介 #include <sys/epoll.h> int epoll_create(int size); int epoll_create1(int flags) ...
- IO复用——epoll系列系统调用
1.内核事件表 epoll是Linux特有的I/O复用函数.epoll把用户关心的文件描述上的事件放在内核里的一个事件表中,并用一个额外的文件描述符来标识该内核事件表.这个额外文件描述符使用函数epo ...
- IO复用之epoll系列
epoll是什么? epoll是Linux内核为处理大批量文件描述符而作了改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显著提高程序在大量并发连接中只有少量活跃的 ...
- HDU 4540 威威猫系列故事——打地鼠 (状态压缩DP)
威威猫系列故事——打地鼠 Time Limit: 300/100 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total ...
随机推荐
- 『Candies 差分约束系统』
差分约束系统 我们先来认识一下差分约束系统鸭! 差分约束系统是一种特殊的\(n\)元一次不等式组,它包含了\(n\)个变量\(x_1-x_n\)以及\(m\)个不等式(约束条件).其中每一个不等式形如 ...
- iPhone多次输入错误密码锁机后刷机恢复(原有内容会丢失)
这个操作会完全丢失手机当前存储的资料,已经备份到iTunes的内容,将来可以通过iTunes恢复.已经被自动备份到iCloud的内容,比如通讯录,将来可以自动从iCloud恢复.以前没有备份过的资料, ...
- Asp.Net SignalR 多平台的Client与Server
多平台 SignalR在.Net的大环境下都可以做到即时通讯,也就是说都可以使用,客户端也不仅是js.下面就来一个控制台的Client 我们需要在nuget上下载包 Microsoft.AspNet. ...
- LeetCode专题-Python实现之第28题: Implement strStr()
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- LeetCode专题-Python实现之第27题:Remove Element
导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...
- RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2->新增“行政区域管理”,同时大批量树采用异步加载
行政区划:简称政区,是国家为了进行分级管理而实行的区域划分.中国现行的行政区划实行如下原则:1.全国分为省.自治区.直辖市:2.省.自治区分为自治州.县.自治县.市:3.自治州分为县.自治县.市:4. ...
- 如何理解git checkout -- file和git reset HEAD -- file
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/001374831943254ee ...
- Spark框架详解
一.引言 作者:Albert陈凯链接:https://www.jianshu.com/p/f3181afec605來源:简书 Introduction 本文主要讨论 Apache Spark 的设计与 ...
- Linux学习笔记之Python3的安装以及创建虚拟环境(CentOS)
安装python3 一.安装需要编译的关联库 yum instal -y zlib zlib-devel (根据自己系统的情况,安装需要的关联库,同样用yum安装即可) yum install ope ...
- PHP Composer 依赖管理的用法
1:下载 1.1:方法一: 通过PHP来安装 cd G:\web\es6 curl -sS https://getcomposer.org/installer | php #这个命令会下载compos ...