linux epoll学习
#include <sys/time.h> /* For portability */
#include <sys/select.h> int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
Returns number of ready file descriptors, 0 on timeout, or –1 on error #include <sys/select.h>
void FD_ZERO(fd_set *fdset);
void FD_SET(int fd, fd_set *fdset);
void FD_CLR(int fd, fd_set *fdset);
int FD_ISSET(int fd, fd_set *fdset);
Returns true (1) if fd is in fdset, or false (0) otherwise FD_ZERO() initializes the set pointed to by fdset to be empty.
FD_SET() adds the file descriptor fd to the set pointed to by fdset.
FD_CLR() removes the file descriptor fd from the set pointed to by fdset.
FD_ISSET() returns true if the file descriptor fd is a member of the set pointed to
by fdset. struct timeval {
time_t tv_sec; /* Seconds */
suseconds_t tv_usec;/* Microseconds (long int) */
}; typedef struct
{
/*XPG4.2requiresthismembername.Otherwiseavoidthename
fromtheglobalnamespace.*/
#ifdef__USE_XOPEN
__fd_maskfds_bits[__FD_SETSIZE/__NFDBITS];
#define__FDS_BITS(set)((set)->fds_bits)
#else
__fd_mask__fds_bits[__FD_SETSIZE/__NFDBITS];
#define__FDS_BITS(set)((set)->__fds_bits)
#endif
}fd_set; /* linux下为 1024/8 = 128 Bytes */ ------------------ #include <sys/epoll.h>
int epoll_create(int size);
Returns file descriptor on success, or –1 on error Starting with kernel 2.6.27, Linux supports a new system call, epoll_create1().
This system call performs the same task as epoll_create(), but drops the obsolete
size argument and adds a flags argument that can be used to modify the behavior
of the system call. One flag is currently supported: EPOLL_CLOEXEC, which causes
the kernel to enable the close-on-exec flag (FD_CLOEXEC) for the new file descriptor.
This flag is useful for the same reasons as the open() O_CLOEXEC flag described in
Section 4.3.1 #include <sys/epoll.h>
int epoll_ctl(int epfd, int op, int fd, struct epoll_event *ev);
Returns 0 on success, or –1 on error The fd argument identifies which of the file descriptors in the interest list is to have
its settings modified. This argument can be a file descriptor for a 'pipe, FIFO,
socket, POSIX message queue, inotify instance, terminal, device, or even another epoll
descriptor' (i.e., we can build a kind of hierarchy of monitored descriptors). However,
fd can’t be a file descriptor for a regular file or a directory (the error EPERM results). The op argument specifies the operation to be performed, and has one of the
following values:
EPOLL_CTL_ADD
EPOLL_CTL_MOD
EPOLL_CTL_DEL 共12字节:
struct epoll_event {
uint32_t events; /* epoll events (bit mask) */
epoll_data_t data; /* User data */
}; The data field of the epoll_event structure is typed as follows:
typedef union epoll_data {
void *ptr; //Pointer to user-defined data
int fd; //File descriptor */
uint32_t u32; //32-bit integer */
uint64_t u64; //64-bit integer */
} epoll_data_t; #include <sys/epoll.h>
int epoll_wait(int epfd, struct epoll_event *evlist, int maxevents, int timeout);
Returns number of ready file descriptors, 0 on timeout, or –1 on error Table 63-8: Bit-mask values for the epoll events field Bit
EPOLLIN Data other than high-priority data can be read
EPOLLPRI High-priority data can be read
EPOLLRDHUP Shutdown on peer socket (since Linux 2.6.17)
EPOLLOUT Normal data can be written
EPOLLET Employ edge-triggered event notification
EPOLLONESHOT Disable monitoring after event notification
EPOLLERR An error has occurred
EPOLLHUP A hangup has occurred Input to Returned by
epoll_ctl()? epoll_wait()?
• •
• •
• •
• •
•
•
•
•
// gcc epoll_input_fifo.c lib/error_functions.c lib/get_num.c -Ilib -o epoll_input_fifo.out
#include <sys/epoll.h>
#include <fcntl.h>
#include "tlpi_hdr.h" #define MAX_BUF 1000
#define MAX_EVENTS 5 int main(int argc, char *argv[]){
int epfd, ready, fd, s, j, numOpenFds;
struct epoll_event ev;
struct epoll_event evlist[MAX_EVENTS];
char buf[MAX_BUF]; printf("EPOLLIN: 0x%x\n", EPOLLIN);
printf("EPOLLHUP: 0x%x\n", EPOLLHUP);
printf("EPOLLERR: 0x%x\n", EPOLLERR); if (argc < 2 || strcmp(argv[1], "--help") == 0)
usageErr("%s file...\n", argv[0]); epfd = epoll_create(argc-1);
if(epfd == -1)
errExit("epoll_create"); for(j = 1; j < argc; j++){
fd = open(argv[j], O_RDONLY);
if(fd == -1){
errExit("open");
}
printf("Opened '%s' on fd %d\n", argv[j], fd); ev.events = EPOLLIN; // Only interested in input events
ev.data.fd = fd;
if(epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev) == -1)
errExit("epoll_ctl");
}
numOpenFds = argc - 1;
while(numOpenFds > 0){
printf("About to epoll_wait()\n");
ready = epoll_wait(epfd, evlist, MAX_EVENTS, -1);
if(ready == -1){
if(errno == EINTR)
continue;
else
errExit("epoll_wait");
}
printf("Ready: %d\n", ready);
for(j=0;j<ready;j++){
printf(" fd=%d; events: %s%s%s\n", evlist[j].data.fd,
(evlist[j].events & EPOLLIN) ? "EPOLLIN " : "",
(evlist[j].events & EPOLLHUP) ? "EPOLLHUP " : "",
(evlist[j].events & EPOLLERR) ? "EPOLLERR " : "");
if(evlist[j].events & EPOLLIN){
s = read(evlist[j].data.fd, buf, MAX_BUF);
if(s == -1)
errExit("read");
printf(" read %d bytes: %.*s\n", s,s,buf);
} else if(evlist[j].events & (EPOLLHUP | EPOLLERR)) {
printf(" closing fd %d\n", evlist[j].data.fd);
if(close(evlist[j].data.fd)==-1)
errExit("close");
numOpenFds--;
}
}
}
printf("All file descriptors closed. bye\n");
exit(EXIT_SUCCESS);
}
from: 《The Linux Programming Interface》P1355
linux epoll学习的更多相关文章
- linux epoll 学习
一.epoll介绍 epoll是linux内核为处理大批量句柄而作的改进的poll,是linux下IO多路复用select.poll的增强版,它能显著减少程序在大量并发连接中只有少量活跃的情况下的系统 ...
- 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就变 ...
- c/c++ linux epoll系列1 创建epoll
linux epoll系列1 创建epoll 据说select和poll的弱点是,随着连接(socket)的增加,性能会直线下降. epoll不会随着连接(socket)的增加,性能直线下降. 知识点 ...
- Linux.NET学习手记(7)
前一篇中,我们简单的讲述了下如何在Linux.NET中部署第一个ASP.NET MVC 5.0的程序.而目前微软已经提出OWIN并致力于发展VNext,接下来系列中,我们将会向OWIN方向转战. 早在 ...
- Linux.NET学习手记(8)
上一回合中,我们讲解了Linux.NET面对OWIN需要做出的准备,以及介绍了如何将两个支持OWIN协议的框架:SignalR以及NancyFX以OwinHost的方式部署到Linux.NET当中.这 ...
- 关于《Linux.NET学习手记(8)》的补充说明
早前的一两天<Linux.NET学习手记(8)>发布了,这一篇主要是讲述OWIN框架与OwinHost之间如何根据OWIN协议进行通信构成一套完整的系统.文中我们还直接学习如何直接操作OW ...
- Linux LVM学习总结——扩展卷组VG
Linux服务器由于应用变更或需求的缘故,有可能出现分区空间不足的情况,此时往往需要进行扩容(要增加分区的空间),而采用LVM的好处就是可以在不需停机的情况下可以方便地调整各个分区大小.如下所示,分区 ...
- linux的学习记录随笔
为什么学习linux 因为操作系统是一种介质,你要接触其中的东西,首先必须要有介质,而linux在服务器端是老大哥的地位,所以呢,学习linux吧. 学习的方式 可以看视频 imooc.百度传课.网易 ...
随机推荐
- 合并排序 java
java 实现合并排序 package How; import java.io.BufferedReader; import java.io.BufferedWriter; import java. ...
- IDEA中的常用设置
ps:对于开发工具,不同的开发人员有不同的设置喜好,这里介绍的是我个人的配置,不喜勿喷. Appearance:个人喜欢全黑主题,雅黑字体 背景图片, 效果如下,编写代码的时候有个自己喜欢的背景图片, ...
- LeetCode(15. 三数之和)
问题描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复 ...
- 潭州课堂25班:Ph201805201 django 项目 第三课 码云注册,django项目 (课堂笔记)
d码云注册,登录, 创建项目: 生成秘钥 想看秘钥: 添加公钥 复制 ssh 连接 通过 git clone <ssh> 连接 在服务器上创建 python3 虚拟环境, 创建项目 p ...
- Django——日志
日志级别 5 个级别 debug 调试 info 普通信息 warning : 提醒警告 error: 发生了错误 critical: 严重的错误 在settings中添加: LOGGING = { ...
- 将map中的值赋值给一个java对象
Map tag=new HashMap(); tag.put("001"," 张三"); tag.put("002","李四&qu ...
- 浏览器JS报错Uncaught RangeError Maximum call stack size exceeded
JavaScript错误:Uncaught RangeError: Maximum call stack size exceeded 堆栈溢出 原因:有小类到大类的递归查询导致溢出 解决方法思想: A ...
- Python基础-修改excel、redis、接口开发、组织代码
pymysql模块补充内容 1. 游标.description():显示表的字段属性 (什么是游标:游标用于交互式应用,就好比word里的光标一样,要修改某个地方,要先把光标移动到这里) 用好这个方法 ...
- 【网站管理6】_一个网站SEO优化方案
首先,前端/页编人员主要负责站内优化,主要从四个方面入手: 第一个,站内结构优化 合理规划站点结构(1.扁平化结构 2.辅助导航.面包屑导航.次导航) 内容页结构设置(最新文章.推荐文章.热门文章.增 ...
- 经典SQL面试题(转)
以下题目都在MySQL上测试可行,有疏漏或有更优化的解决方法的话欢迎大家提出,我会持续更新的:) 有三个表,如果学生缺考,那么在成绩表中就不存在这个学生的这门课程成绩的记录,写一段SQL语句,检索出每 ...