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. 为Github 托管项目的访问添加SSH keys

    为了便于访问远程仓库,各个协作者将自己的本地的项目内容推送到远程仓库中,使用 SSH keys 验证github的好处:不用每次提交代码时都输入用户名和密码. 如果SSH key没有添加到github ...

  2. selenium+python : Waits---study

    from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.s ...

  3. C 标准IO 库函数与Unbuffered IO函数

    先来看看C标准I/O库函数是如何用系统调用实现的. fopen(3) 调用open(2)打开指定的文件,返回一个文件描述符(就是一个int 类型的编号),分配一 个FILE 结构体, 通常里面包含了: ...

  4. js 重写 bootstrap 样式 alert/confirm 消息窗口

    相信很多人都受够了 alert.confirm 的样子,最近正在用 bootstrap 做项目,顺便封装了一个 bootstrap 样式的消息框. 实现起来很简单,bootstrap 本身就自带了 m ...

  5. ArraySegmentSample

    class Program { static void Main() { , , , , , }; , , , , , , }; ] { , ), , ) }; var sum = SumOfSegm ...

  6. 自定义相机下使用clippingNode注意事项

    调用完clippingNode->setCameraMask(myCameraMask)后,还需要clipNode->getStencil()->setCameraMask(myCa ...

  7. AngularJS概述-3D

    最近工作需要用到,接触的比较多,在此系统学习一下. angular.js 是新一代web MVC开发框架,对应 模型,视图,控制器.,相比 jquery 模式,这种新玩意竟然不需要开发者直接去操作do ...

  8. [Jobdu] 题目1545:奇怪的连通图

    题目描述: 已知一个无向带权图,求最小整数k.使仅使用权值小于等于k的边,节点1可以与节点n连通. 输入: 输入包含多组测试用例,每组测试用例的开头为一个整数n(1 <= n <= 100 ...

  9. paip.索引的种类以及实现attilax 总结

    paip.索引的种类以及实现attilax 总结 1. 索引的类型 1 2. Btree索引(分块索引) 1 3. Hash 索引(编号索引) 1 4. 倒排索引 2 5. 对大型文本文件做索引 2 ...

  10. Diamond 3.5简易教程(二)------软件的简单使用

    二.软件的简单使用 工程建立后我们就可以进行程序的编写添加了. 选择左下角file list 选项卡 这里主要是工程的信息. 在input files 上右键弹出选项addànew file... 在 ...