poll--wait for some event on a file descriptor
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的更多相关文章
- python uwsgi报错epoll_ctl(): Bad file descriptor
今天安装了uwsgi+supervisord+nginx,一直访问不了 直接启动uwsgi使用nginx访问,查看有大量报错:epoll_ctl(): Bad file descriptor [cor ...
- 出现epoll failed: Bad file descriptor的原因
今天遇到了这个问题,之前找了半天原来是IO事件的socket描述符在epoll_ctl()处理之前关闭了. if(epoll_ctl(epollFd, EPOLL_CTL_DEL, ev->fd ...
- 近期编程问题——epoll failed:bad file descriptor
出现问题:epoll_wait:Bad file descriptor 原因:IO时间的socket描述符在epoll_ctl处理前就关闭了. 解决方法:不要在epoll_ctl之前关闭socket描 ...
- mongodb在ubuntu下的couldn‘t remove fs lock errno:9 Bad file descriptor的错误
按照官网上的安装方法: 在ubuntu系统下有可能出现如下错误: couldn't remove fs lock errno:9 Bad file descriptor 此时需要修改文件所有者 $ s ...
- 用dup2和dup产生一份file descriptor 的拷贝
在类Unix操作系统里面,.dup2和dup都通过系统调用来产生一份file descriptor 的拷贝. dup对我来说还很简单 int dup(int filedes); dup2就 ...
- shell 中最常使用的 FD (file descriptor)
在 shell 程式中,最常使用的 FD (file descriptor) 大概有三个, 分别是: 0 是一个文件描述符,表示标准输入(stdin)1 是一个文件描述符,表示标准输出(stdout) ...
- python print 在windows上 出现 Bad file descriptor error
先说一下情况,一个python写的采集程序,做成windows服务在windows上运行. 这个问题出现的挺奇特,本来一套采集程序,一个采集文件的时候没问题,两个采集文件的时候也没问题,当三个采集文件 ...
- windows pm2 启动nodejs失败:Error: EBADF: bad file descriptor, uv_pipe_open
windows下打开命令窗口,安装pm2:npm install pm2 -g pm2成功安装,在项目目录下用pm2启动服务:pm2 start index.js,结果启动失败,错误如下: .pm2\ ...
- 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 ...
随机推荐
- HDUOJ-----4512吉哥系列故事——完美队形I(LCIS)
吉哥系列故事——完美队形I Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- 【LeetCode】93. Restore IP Addresses
Restore IP Addresses Given a string containing only digits, restore it by returning all possible val ...
- C# 自定义文件格式并即时刷新注册表 非关闭explorer
转自:http://blog.csdn.net/zhangtirui/article/details/4309492 最近公司在做一个项目 用到关于自定义格式的文件,但在注册表图标更改后 文件图标 ...
- Unix环境高级编程(二)文件和目录
本章主要介绍的是文件结构及目录.重点是通过stat函数获取文件的结构信息,然后是文件目录及其遍历.学完本章后,编写了一个输出给的目录下的文件信息的程序. 首先是包含在<sys/stat.h> ...
- poj 3735 Training little cats(构造矩阵)
http://poj.org/problem?id=3735 大致题意: 有n仅仅猫,開始时每仅仅猫有花生0颗,现有一组操作,由以下三个中的k个操作组成: 1. g i 给i仅仅猫一颗花生米 2. e ...
- Python modf() 函数
描述 modf() 方法返回x的整数部分与小数部分,两部分的数值符号与x相同,整数部分以浮点型表示. 语法 以下是 modf() 方法的语法: import math math.modf( x ) 注 ...
- mysql按日/周/月统计
一.mysql按日统计 ) count ' and start_time > '2017-06-28' group by days; 二.mysql按周统计 ) ' group by weeks ...
- 在T-SQL语句中访问远程数据库
1.启用Ad Hoc Distributed Queries 在使用openrowset/opendatasource前搜先要启用Ad Hoc Distributed Queries服务,因为这个服务 ...
- 机器学习(5): K-means 算法
3. K-means 算法: 3.1 Clustering 中的经典算法,数据挖掘十大经典算法之一 3.2 算法接受参数 k :然后将事先输入的n个数据对象划分为 k个聚类以便 ...
- ie11 下 input 默认有 X 关闭按钮的问题
&::-ms-clear, ::-ms-reveal{display: none;}