poll同select,用于监控file descriptor事件,推荐用poll的升级版epool来实现功能,但在简单应用中使用poll更方便。

#include <poll.h>
int poll(struct pollfd *fds, nfds_t nfds, int timeout);
struct pollfd {
  int fd;
  short events;
  short revents;
};

fds是文件描述符集的数组,nfds指定数组元素个数。

pollfd中fd是打开文件的文件描述符;events是输入参数,指定监控事件;revents是输出参数,被内核填充,只是实际发生的事件。

timeout指定超时毫秒数(milliseconds)。

成功返回正数,表示非0revents的fd个数,0表示超时,错误返回-1并设置errno。

On success, a positive number is returned; this is the number of structures which have  nonzero  revents  fields  
(in other words, those descriptors with events or errors reported).
A value of indicates that the call timed out and no file descriptors were ready.
On error, - is returned, and errno is set appropriately.

应用举例

监控一个网络socket是否发生错误(刚链接时)。

int usock_wait_ready(int fd, int msecs) {
struct pollfd fds[];
int res; fds[].fd = fd;
fds[].events = POLLOUT; res = poll(fds, , msecs);
if (res < ) {
return errno;
} else if (res == ) {
return -ETIMEDOUT;
} else {
int err = ;
socklen_t optlen = sizeof(err); res = getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &optlen);
if (res)
return errno;
if (err)
return err;
} return ;
}

poll--wait for some event on a file descriptor的更多相关文章

  1. python uwsgi报错epoll_ctl(): Bad file descriptor

    今天安装了uwsgi+supervisord+nginx,一直访问不了 直接启动uwsgi使用nginx访问,查看有大量报错:epoll_ctl(): Bad file descriptor [cor ...

  2. 出现epoll failed: Bad file descriptor的原因

    今天遇到了这个问题,之前找了半天原来是IO事件的socket描述符在epoll_ctl()处理之前关闭了. if(epoll_ctl(epollFd, EPOLL_CTL_DEL, ev->fd ...

  3. 近期编程问题——epoll failed:bad file descriptor

    出现问题:epoll_wait:Bad file descriptor 原因:IO时间的socket描述符在epoll_ctl处理前就关闭了. 解决方法:不要在epoll_ctl之前关闭socket描 ...

  4. mongodb在ubuntu下的couldn‘t remove fs lock errno:9 Bad file descriptor的错误

    按照官网上的安装方法: 在ubuntu系统下有可能出现如下错误: couldn't remove fs lock errno:9 Bad file descriptor 此时需要修改文件所有者 $ s ...

  5. 用dup2和dup产生一份file descriptor 的拷贝

    在类Unix操作系统里面,.dup2和dup都通过系统调用来产生一份file descriptor 的拷贝.   dup对我来说还很简单   int dup(int filedes);   dup2就 ...

  6. shell 中最常使用的 FD (file descriptor)

    在 shell 程式中,最常使用的 FD (file descriptor) 大概有三个, 分别是: 0 是一个文件描述符,表示标准输入(stdin)1 是一个文件描述符,表示标准输出(stdout) ...

  7. python print 在windows上 出现 Bad file descriptor error

    先说一下情况,一个python写的采集程序,做成windows服务在windows上运行. 这个问题出现的挺奇特,本来一套采集程序,一个采集文件的时候没问题,两个采集文件的时候也没问题,当三个采集文件 ...

  8. windows pm2 启动nodejs失败:Error: EBADF: bad file descriptor, uv_pipe_open

    windows下打开命令窗口,安装pm2:npm install pm2 -g pm2成功安装,在项目目录下用pm2启动服务:pm2 start index.js,结果启动失败,错误如下: .pm2\ ...

  9. fastDFS errcode:9 path:Bad file descriptor errcode:22 path:Invalid argument

    fastDFS errcode:9 path:Bad file descriptor errcode:22 path:Invalid argument <error>status:4 er ...

随机推荐

  1. BIND9源码分析之acl 的实现

    BIND配置中一大堆一大堆的acl,什么allow-query, allow-recursion, allow-update还有view的match-clients等等等等. acl中的主要存储的就是 ...

  2. automake安装出错

    automake命令出错 configure.ac:64: error: possibly undefined macro: AM_ICONV    If this token and others ...

  3. oracle导表小结

    事件描述:从A主机oracle服务器导出.sql文件到B主机,发现1.导入存在乱码 2.提示USERS表空没有权限(A B主机均为window系统) 1.针对第一点乱码 首先确认系统的默认字符编码GB ...

  4. SQL批量删除用户表(先删除所有外键约束,再删除所有表)

    --批量删除用户表 --1.删除外键约束DECLARE c1 cursor for     select 'alter table ['+ object_name(parent_obj) + '] d ...

  5. HDU1024 Max Sum Plus Plus 【DP】

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  6. 把git上的larave项目通过SourceTree安装上再通过composer安装依赖库

    1.项目地址克隆 https://gitee.com/fps2tao/laravel5.5-alitaobao.git 通过SourceTree工具下载到本地 2.在命令行方式打开项目地址安装依赖库( ...

  7. jQuery.ajax() 如何设置 Headers 中的 Accept 内容

    其实很简单,首先如果是常见类型,则请直接设置 dataType 属性 $.ajax({ dataType: "json", type: "get", succe ...

  8. 通过GUID生成可持久化的PID

    byte[] buffer = Guid.NewGuid().ToByteArray(); ); GUID是微软针对UUID的实现,直接生成会大于long类型的最大长度. 但只要转换一下即可

  9. Oracle SQLDeveloper ORA-01017 invalid username/password;logon denied (密码丢失解决方案)

    oracle sys账号用sql developer工具无法登陆 用户名和密码不一致 如果的确是丢失密码的话:在SQL*Plus中执行如下命令:   SQL>conn/as sysdba (操作 ...

  10. 李洪强和你一起学习前端之(6)css行高,盒模型,外边距

    李洪强和你一起学习前端之(6)css行高,盒模型,外边距 复习昨天的知识 1.1css书写位置: 内嵌式写法 外联式写法 <link href = "1.css" rel = ...