失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 什么是epoll的水平触发与边缘触发?两段代码彻底理解

什么是epoll的水平触发与边缘触发?两段代码彻底理解

时间:2024-06-26 22:32:45

相关推荐

什么是epoll的水平触发与边缘触发?两段代码彻底理解

Edge trigger and level trigger of epoll

水平触发

对于读操作:只要缓冲内容不为空,LT模式返回读就绪。对于写操作:只要缓冲区还不满,LT模式会返回写就绪。

#include <unistd.h>#include <stdio.h>#include <sys/epoll.h>int main(){int epfd, nfds;char buf[256];struct epoll_event event, events[5];epfd = epoll_create(1);event.data.fd = STDIN_FILENO;event.events = EPOLLIN; // LT是默认模式epoll_ctl(epfd, EPOLL_CTL_ADD, STDIN_FILENO, &event);while (1) {nfds = epoll_wait(epfd, events, 5, -1);int i;for (i = 0; i < nfds; ++i) {if (events[i].data.fd == STDIN_FILENO) {read(STDIN_FILENO, buf, 1);printf("hello world\n");}}}}

结果:

# ./a.out ahello worldhello worldabhello worldhello worldhello worldabchello worldhello worldhello worldhello world

边缘触发

对于读操作

(1)当缓冲区由不可读变为可读的时候,即缓冲区由空变为不空的时候。

(2)当有新数据到达时,即缓冲区中的待读数据变多的时候。

(3)当缓冲区有数据可读,且应用进程对相应的描述符进行EPOLL_CTL_MOD 修改EPOLLIN事件时。

对于写操作

(1)当缓冲区由不可写变为可写时。

(2)当有旧数据被发送走,即缓冲区中的内容变少的时候。

(3)当缓冲区有空间可写,且应用进程对相应的描述符进行EPOLL_CTL_MOD 修改EPOLLOUT事件时。

#include <unistd.h>#include <stdio.h>#include <sys/epoll.h>int main(){int epfd, nfds;struct epoll_event event, events[5];char buf[256];epfd = epoll_create(1);event.data.fd = STDIN_FILENO;event.events = EPOLLIN | EPOLLET;epoll_ctl(epfd, EPOLL_CTL_ADD, STDIN_FILENO, &event);while (1) {nfds = epoll_wait(epfd, events, 5, -1);int i;for (i = 0; i < nfds; ++i) {if (events[i].data.fd == STDIN_FILENO) {read(STDIN_FILENO, buf, 1);printf("hello world\n");}}}}

结果:

# ./a.out ahello worldabhello worldabchello world

如果觉得《什么是epoll的水平触发与边缘触发?两段代码彻底理解》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。