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. Java引入的一些新特性

    Java引入的一些新特性 Java 8 (又称为 jdk 1.8) 是 Java 语言开发的一个主要版本. Oracle 公司于 2014 年 3 月 18 日发布 Java 8 ,它支持函数式编程, ...

  2. Java第三次上机随笔

    就记录一下新的收获吧~ 1.定点数(BigDecimal) 先区分一下浮点数和定点数: 浮点数(float/double):小数点可以任意浮动,称为浮点表示法 定点数(BigDecimal):一种数约 ...

  3. HBase之CF持久化系列(续2)

    正如上篇博文所说,在本节我将为大家带来StoreFlusher.finalizeWriter..如果大家没有看过我的上篇博文<HBase之CF持久化系列(续1)>,那我希望大家还是回去看一 ...

  4. [Abp 源码分析]五、系统设置

    0.简要介绍 Abp 本身有两种设置,一种就是 上一篇文章 所介绍的模块配置 Configuration,该配置主要用于一些复杂的数据类型设置,不仅仅是字符串,也有可能是一些 C# 运行时的一些变量. ...

  5. 5.Django cookie

    概述 1.获取cookie request.COOKIES['key'] request.COOKIES.get('key') request.get_signed_cookie(key, defau ...

  6. 从锅炉工到AI专家(11)(END)

    语音识别 TensorFlow 1.x中提供了一个语音识别的例子speech_commands,用于识别常用的命令词汇,实现对设备的语音控制.speech_commands是一个很成熟的语音识别原型, ...

  7. linux内核中听过就能记住的概念

    打算给我们部门弄个内部分享.发现大家对一些底层知识的认知停留在一句一句的,比如听说JVM使用-XX:-UseBiasedLocking取消偏向锁可以提高性能,因为它只适用于非多线程高并发应用.使用数字 ...

  8. C语言实现链队列的初始化&进队&出队

    /*链表实现队列的一系列操作*/ #include<stdio.h> #include<stdlib.h> #define OK 1 #define ERROR 0 typed ...

  9. leetcode — word-ladder-ii

    import java.util.*; /** * Source : https://oj.leetcode.com/problems/word-ladder-ii/ * * * Given two ...

  10. ASP.NET Core使用Jaeger实现分布式追踪

    前言 最近我们公司的部分.NET Core的项目接入了Jaeger,也算是稍微完善了一下.NET团队的技术栈. 至于为什么选择Jaeger而不是Skywalking,这个问题我只能回答,大佬们说了算. ...