当前位置:网站首页>Epolloneshot event of epoll -- instance program
Epolloneshot event of epoll -- instance program
2022-04-23 14:40:00 【m0_ fifty-one million five hundred and fifty-one thousand three】
This example program is a server , He receives user data , And output to standard output , He is characterized by : For each of these EPOLLIN
event , Wake up a thread to deal with him , And the main process continues to execute , That is to enter the next cycle , call epoll_wait
Listen for other events .
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<assert.h>
#include<stdio.h>
#include<errno.h>
#include<stdlib.h>
#include<string.h>
#include<fcntl.h>
#include<sys/epoll.h>
#include<pthread.h>
#define MAX_EVENT_NUMBER 1024
#define BUFFER_SIZE 1024
struct fds
{
int epollfd;
int sockfd;
};
int setnonblocking(int fd)
{
int old_option = fcntl(fd, F_GETFL);
int new_option = old_option | O_NONBLOCK;
fcntl(fd, F_SETFL, new_option);
return old_option;
}
/* take fd Upper EPOLLIN and EPOLLET Event registration to epollfd Directed epoll In the kernel event table , Parameters oneshot Specify whether to register fd Upper EPOLLONESHOT event */
void addfd(int epollfd, int fd, bool oneshot)
{
epoll_event event;
event.events = EPOLLIN | EPOLLET;
if (oneshot)
{
event.events |= EPOLLONESHOT;
}
epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &event);
setnonblocking(fd);
}
/* Reset fd Events on the . After doing this , Even though fd Upper EPOLLONESHOT Event is registered , But the operating system still triggers fd Upper EPOLLIN event , And trigger only once */
void reset_oneshot(int epollfd, int fd)
{
epoll_event event;
event.data.fd = fd;
event.events = EPOLLIN | EPOLLET | EPOLLONESHOT;
epoll_ctl(epollfd, EPOLL_CTL_MOD, fd, &event);
}
/* The worker thread */
void* worker(void* arg)
{
int sockfd = ((fds*)arg)->sockfd;
int epollfd = ((fds*)arg)->epollfd;
printf("start new thread to receive data on fd %d\n", sockfd);
char buf[BUFFER_SIZE];
memset(buf, '\0', BUFFER_SIZE);
/* Cyclic reading sockfd The data on the , Guide encountered EAGAIN error */
while (1)
{
int ret = recv(sockfd, buf, BUFFER_SIZE - 1, 0);
if (ret == 0)
{
close(sockfd);
printf("foreiner closed the connection\n");
break;
}
else if (ret < 0)
{
if (errno == EAGAIN)
{
reset_oneshot(epollfd, sockfd);
printf("read later\n");
break;
}
}
else
{
printf("get content: %s\n", buf);
/* Sleep 5s, Simulate the data processing process */
sleep(5);
}
}
printf("end thread receiving data on fd: %d\n", sockfd);
}
int main(int argc, char* argv[])
{
if (argc <= 2)
{
return 1;
}
const char* ip = argv[1];
int port = atoi(argv[2]);
int ret = 0;
struct sockaddr_in address;
bzero(&address, sizeof(address));
address.sin_family = AF_INET;
address.sin_port = htons(port);
inet_pton(AF_INET, ip, &address.sin_addr);
int listenfd = socket(PF_INET, SOCK_STREAM, 0);
assert(listenfd >= 0);
ret = bind(listenfd, (struct sockaddr*)&address, sizeof(address));
assert(ret != -1);
ret = listen(listenfd, 5);
assert(ret != -1);
epoll_event events[MAX_EVENT_NUMBER];
int epollfd = epoll_create(10);
assert(epollfd != -1);
/* Be careful , monitor sockfd listenfd Can't register on EPOLLONESHOT event , Otherwise, because the program can only handle one client connection ! Because the connection request of subsequent customers will no longer trigger listenfd Upper EPOLLIN event */
addfd(epollfd, listenfd, false);
while (1)
{
int ret = epoll_wait(epollfd, events, MAX_EVENT_NUMBER, -1);
if (ret < 0)
{
printf("epoll failure\n");
break;
}
for (int i = 0; i < ret; i++)
{
int sockfd = events[i].data.fd;
if (sockfd == listenfd)
{
struct sockaddr_in client_address;
socklen_t client_addrlength = sizeof(client_address);
int connfd = accept(listenfd, (struct sockaddr*)&client_address, &client_addrlength);
/* Register for each non listening file descriptor EPOLLONESHOT event */
addfd(epollfd, connfd, true);
}
else if (events[i].events & EPOLLIN) /* For each EPOLLIN Event opens a thread , Then the process proceeds to the next epoll_wait */
{
pthread_t thread;
fds fds_for_new_worker;
fds_for_new_worker.epollfd = epollfd;
fds_for_new_worker.sockfd = sockfd;
/* Start a new worker thread as sockfd service */
pthread_create(&thread, NULL, worker, (void*)&fds_for_new_worker);
}
else
{
printf("something else happened\n");
}
}
}
close(listenfd);
return 0;
}
版权声明
本文为[m0_ fifty-one million five hundred and fifty-one thousand three]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231435333870.html
边栏推荐
- [servlet] detailed explanation of servlet (use + principle)
- ASEMI三相整流桥和单相整流桥的详细对比
- OC 转 Swift 条件编译、标记、宏、 Log、 版本检测、过期提示
- Electronic scale weighing system design, hx711 pressure sensor, 51 single chip microcomputer (proteus simulation, C program, schematic diagram, thesis and other complete data)
- 一个月把字节,腾讯,阿里都面了,写点面经总结……
- 科技的成就(二十一)
- 金九银十,入职字节跳动那一天,我哭了(蘑菇街被裁,奋战7个月拿下offer)
- 初始c语言大致框架适合复习和初步认识
- QT interface optimization: QT border removal and form rounding
- 《JVM系列》 第七章 -- 字节码执行引擎
猜你喜欢
ASEMI三相整流桥和单相整流桥的详细对比
PCIe X1 插槽的主要用途是什么?
I thought I could lie down and enter Huawei, but I was confused when I received JD / didi / iqiyi offers one after another
Arduino for esp8266串口功能简介
面试官:说一下类加载的过程以及类加载的机制(双亲委派机制)
51单片机的花卉、农田自动浇水灌溉系统开发,Proteus仿真,原理图和C代码
交通灯系统51单片机设计(附Proteus仿真、C程序、原理图及PCB、论文等全套资料)
详解TCP的三次握手
【Servlet】Servlet 详解(使用+原理)
基于单片机的DS18B20的数字温度监控报警系统设计【LCD1602显示+Proteus仿真+C程序+论文+按键设置等】
随机推荐
Find daffodils - for loop practice
C语言知识点精细详解——初识C语言【1】
基于TLC5615的多路可调数控直流稳压电源,51单片机,含Proteus仿真和C代码等
Arduino for esp8266串口功能简介
八路抢答器系统51单片机设计【附Proteus仿真、C程序、原理图及PCB文件、元器件清单和论文等】
DVWA之暴力破解(Brute Force)Low-->high
Matrix exchange row and column
MQ-2和DS18B20的火灾温度-烟雾报警系统设计,51单片机,附仿真、C代码、原理图和PCB等
[untitled]
do(Local scope)、初始化器、内存冲突、Swift指针、inout、unsafepointer、unsafeBitCast、successor、
Detailed explanation of C language knowledge points -- first knowledge of C language [1]
51单片机+LCD12864液晶显示的俄罗斯方块游戏,Proteus仿真、AD原理图、代码、论文等
redis的五种数据类型
Logical volume creation and expansion
A good tool: aardio
线程同步、生命周期
Four ways of SSH restricting login
A blog allows you to learn how to write markdown on vscode
capacitance
Qt界面优化:Qt去边框与窗体圆角化