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. HDUOJ---What Are You Talking About

    What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K ...

  2. APK 本地化和去广告

    APK 本地化       说起APK的汉化,目前大部分教程都是让用Hex Workshop或者Android ResEdit来做.但是实际操作后,就会发现: 1.Hex Workshop操作繁琐,经 ...

  3. 使用Windbg调试.Net应用程序 [转]

    Windbg+Procdump解决w3wp.exe CPU过百问题[转] High CPU Dump收集工具 - ProcDump使用方法[转] ProcDump v9.0[官方] windbg查找c ...

  4. 【php】基础学习5

    主要包括PHP的错误.异常处理和调试.具体如下: <html xmlns=http://www.w3.org/1999/xhtml> <head> <meta http- ...

  5. Oracle分组函数以及数据分组

    简单总结一下对于数据的分组和分组函数. 本文所举实例,数据来源oracle用户scott下的emp,dept ,salgrade 3表:数据如下: 一.分组函数 1.sum()求和函数.max()求最 ...

  6. python学习笔记——多进程间通信——Linux信号基础

    1 信号的基本描述 Signal信号(其全程为软中断信号)是Linux系统编程中非常重要的概念,信号是异步进程中通信的一种方式. 作用是通知进程发生了异步事件.进程之间可以调用系统来传递信号, 本身内 ...

  7. python练习笔记——求三位的水仙花数

    百位数的立方 + 十位数的立方 +个位数的立方 = 原数 for m in range(1,10): for n in range(0,10): for i in range(0,10): a = m ...

  8. JMeter学习笔记--详解JMeter逻辑控制器

    JMeter使用逻辑控制器来决定采样器的处理顺序 简单控制器(Simple Controller):存储设备(将某些采样器归组) 循环控制器(Loop Controller:设置循环次数 仅一次控制器 ...

  9. Linux调度器 - deadline调度器

    一.概述 实时系统是这样的一种计算系统:当事件发生后,它必须在确定的时间范围内做出响应.在实时系统中,产生正确的结果不仅依赖于系统正确的逻辑动作,而且依赖于逻辑动作的时序.换句话说,当系统收到某个请求 ...

  10. Cocos2d-x游戏移植到WP8之路 -- c++和c#交互

    Cocos2d-x是眼下最流行的手机游戏引擎之中的一个,开源.轻量.多平台等的诸多特性使得它被非常多国内外手游开发人员所喜爱. 利用Cocos2d-x来开发Windows Phone 8的游戏相同也是 ...