当前位置:网站首页>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
边栏推荐
- MCU function signal generator, output four kinds of waveforms, adjustable frequency, schematic diagram, simulation and C program
- 【STC8G2K64S4】比较器介绍以及比较器掉电检测示例程序
- 成都控制板设计提供_算是详细了_单片机程序头文件的定义、编写及引用介绍
- The art of automation
- The initial C language framework is suitable for review and preliminary understanding
- 分分钟掌握---三目运算符(三元运算符)
- pnpm安装使用
- Vous ne connaissez pas encore les scénarios d'utilisation du modèle de chaîne de responsabilité?
- I/O复用的高级应用:同时处理 TCP 和 UDP 服务
- Qt界面优化:鼠标双击特效
猜你喜欢
八路抢答器系统51单片机设计【附Proteus仿真、C程序、原理图及PCB文件、元器件清单和论文等】
电容
Chapter 7 of JVM series -- bytecode execution engine
详解TCP的三次握手
Multisim Simulation Design of DC adjustable regulated power supply of LM317 (with simulation + paper + reference)
AT89C51单片机的数字电压表开发,量程0~5V,proteus仿真,原理图PCB和C程序等
SHT11传感器的温度湿度监控报警系统单片机Proteus设计(附仿真+论文+程序等)
Mq-2 and DS18B20 fire temperature smoke alarm system design, 51 single chip microcomputer, with simulation, C code, schematic diagram, PCB, etc
Swift:Entry of program、Swift调用OC、@_silgen_name 、 OC 调用Swift、dynamic、String、Substring
【工厂模式详解】工厂方法模式
随机推荐
Solve the problem of SSH configuration file optimization and slow connection
L'externalisation a duré quatre ans.
C语言p2选择分支语句详解
ASEMI超快恢复二极管与肖特基二极管可以互换吗
DS1302的电子万年历_51单片机,年月日、星期、时分秒、农历和温度,带闹钟,全套资料
Eight way responder system 51 Single Chip Microcomputer Design [with Proteus simulation, C program, schematic diagram, PCB files, component list and papers, etc.]
OpenFaaS实战之四:模板操作(template)
Electronic perpetual calendar of DS1302_ 51 single chip microcomputer, month, day, week, hour, minute and second, lunar calendar and temperature, with alarm clock and complete set of data
单片机的函数信号发生器,输出4种波形,频率可调,原理图,仿真和C程序
I/O复用的高级应用之一:非阻塞 connect———使用 select 实现(也可以用 poll 实现)
A good tool: aardio
8.2 文本预处理
Swift:Entry of program、Swift调用OC、@_silgen_name 、 OC 调用Swift、dynamic、String、Substring
ASEMI三相整流桥和单相整流桥的详细对比
QT interface optimization: double click effect
Svn detailed use tutorial
asp.net使用MailMessage发送邮件的方法
【STC8G2K64S4】比较器介绍以及比较器掉电检测示例程序
想要成为架构师?夯实基础最重要
qt之.pro文件详解