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.百度传课.网易 ...
随机推荐
- ServiceNow在中国还有没有模仿者?
美国版的“ServiceNow”:https://www.servicenow.com 中国版的“ServiceHot” :http://www.itsmcn.com
- SpringBoot使用Mybatis-Generator
本文介绍如何将Maven和Mybatis-Generator配合使用. 简介 Mybatis-Generator是Mybatis提供的一个便捷型插件,自动可以为项目生产对应的实体类,Mapper,da ...
- SpringBoot扫描不到controller
访问报错: 原因:启动类文件存放位置问题,应该放在controller包同级目录下,如下图: ---不积跬步无以至千里,不积小流无以成江海
- 185. [USACO Oct08] 挖水井
185. [USACO Oct08] 挖水井(点击转到COGS) 输入文件:water.in 输出文件:water.out 时间限制:1 s 内存限制:128 MB 描述 农夫约翰决定给他 ...
- JN5139 zigbee 资料
JN5139模块是一系列可以使使用者在最短的时间内在最低的成本下实现IEEE802.15.4或ZigBee兼容系统的表贴模块.此款模块减少了用户对于RF板设计和测试框架的昂贵漫长的开发时间.这些模块利 ...
- entity framework core 2.0 & sqlite 配置教程
我用的是vs2017,需要下载.net core 2.0 sdk. .net core 下载地址:点我下载 1.在Visual Studio之中创建一个.net core的控制台项目 2.修改cspr ...
- python网络编程(九)
单进程服务器-非堵塞模式 服务器 #coding=utf-8 from socket import * import time # 用来存储所有的新链接的socket g_socketList = [ ...
- JS 模仿块级作用域
function outputNumbers(count) { for (var i=0; i<count; i++) { console.log(i); } var i; // 重新声明变量 ...
- hadoop实现倒排索引
hadoop实现倒排索引 本文用hadoop实现倒排索引算法,用基本的分两步完成,不使用combine 第一步 读入文档,统计文档中各个单词的个数,与word count类似,但这里把word-fil ...
- go test :Wrong test signature
err: Wrong test signature 写测试函数出了个错,找半天才找到原因:虽然很愚蠢,还是记录下. 是因为 参数没加*导致的.