#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学习的更多相关文章

  1. linux epoll 学习

    一.epoll介绍 epoll是linux内核为处理大批量句柄而作的改进的poll,是linux下IO多路复用select.poll的增强版,它能显著减少程序在大量并发连接中只有少量活跃的情况下的系统 ...

  2. c/c++ linux epoll系列3 利用epoll_wait设置timeout时间长度

    linux epoll系列3 利用epoll_wait设置timeout时间长度 epoll_wait函数的第四个参数可以设置,epoll_wait函数的等待时间(timeout时间长度). 例子1, ...

  3. c/c++ linux epoll系列2 利用epoll_wait查看是否可以送信

    linux epoll系列2 利用epoll_wait查看是否可以送信 write函数本来是非阻塞函数,但是当缓存区被写满后,再往缓存区里写的时候,就必须等待缓存区再次变成可写,所以这是write就变 ...

  4. c/c++ linux epoll系列1 创建epoll

    linux epoll系列1 创建epoll 据说select和poll的弱点是,随着连接(socket)的增加,性能会直线下降. epoll不会随着连接(socket)的增加,性能直线下降. 知识点 ...

  5. Linux.NET学习手记(7)

    前一篇中,我们简单的讲述了下如何在Linux.NET中部署第一个ASP.NET MVC 5.0的程序.而目前微软已经提出OWIN并致力于发展VNext,接下来系列中,我们将会向OWIN方向转战. 早在 ...

  6. Linux.NET学习手记(8)

    上一回合中,我们讲解了Linux.NET面对OWIN需要做出的准备,以及介绍了如何将两个支持OWIN协议的框架:SignalR以及NancyFX以OwinHost的方式部署到Linux.NET当中.这 ...

  7. 关于《Linux.NET学习手记(8)》的补充说明

    早前的一两天<Linux.NET学习手记(8)>发布了,这一篇主要是讲述OWIN框架与OwinHost之间如何根据OWIN协议进行通信构成一套完整的系统.文中我们还直接学习如何直接操作OW ...

  8. Linux LVM学习总结——扩展卷组VG

    Linux服务器由于应用变更或需求的缘故,有可能出现分区空间不足的情况,此时往往需要进行扩容(要增加分区的空间),而采用LVM的好处就是可以在不需停机的情况下可以方便地调整各个分区大小.如下所示,分区 ...

  9. linux的学习记录随笔

    为什么学习linux 因为操作系统是一种介质,你要接触其中的东西,首先必须要有介质,而linux在服务器端是老大哥的地位,所以呢,学习linux吧. 学习的方式 可以看视频 imooc.百度传课.网易 ...

随机推荐

  1. C# JSON 转换

    // using System.Web.Script.Serialization; var jser = new JavaScriptSerializer(); var json = jser.Ser ...

  2. excel计算时间差值

    excel计算时间差值 2018/10/1 10:59:00 减去 2018/9/21 1:05:13 获取 多少天. 如1.2天.这种. ==

  3. 网上的很多Android项目源码有用吗?Android开发注意的地方。

    在Android项目开发中,我们可以在网上看到很多项目源码,大部分也不是很精致, 比如 06.Android阿福多媒体播放器开发教程+源码 还有什么浏览器源码. 那么这些有用吗?  价值在哪里? 精致 ...

  4. BZOJ.4825.[AHOI/HNOI2017]单旋(线段树)

    BZOJ LOJ 洛谷 这题不难啊,我怎么就那么傻,拿随便一个节点去模拟.. 我们只需要能够维护,将最小值或最大值转到根.模拟一下发现,对于最小值,它的右子树深度不变(如果存在),其余节点深度全部\( ...

  5. 设置 IntelliJ IDEA 智能提醒时忽略大小写

    1.Ctrl + Alt + S 打开设置界面(或者从菜单File - settings 进来) 2.在搜索框输入Completion定位到Code Completion功能项 3.在右侧配置界面选中 ...

  6. 修改mybatis plus Generator模板生成字段注释枚举常量

    修改mybatis plus Generator模板生成字段注释枚举常量 本文基于最新的mybatis-plus 3.0.1版本源码修改,如果使用其它版本,处理方式也类似,主要是生成Entity的Fr ...

  7. C++程序设计方法1:分离代码

    使用头文件,分离声明与定义 防止重复包含: #ifndef FUNC_H #define FUNC_H .... #endif main函数的命令行参数: #include <iostream& ...

  8. flask内容之数据库的管理

    #! /usr/bin/env python # *-* coding: utf-8 *-* from flask import Flask, flash, redirect from flask i ...

  9. python之面向对象的程序设计

    一.什么是面向对象的程序设计 面向过程的程序设计的核心是过程,过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西. 优点是:极大的降低了程序的复杂度 缺点是: ...

  10. Servlet(2)—java项目下web应用程序

    在java项目下手动写一个web程序 步骤: ①创建一个java项目并在根目录创建一个WebContent目录文件 ②WebContent下创建WEB-INF目录文件 ③WEB-INF下创建class ...