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 ...
随机推荐
- 【LeetCode】22. Generate Parentheses (2 solutions)
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- 【LeetCode】142. Linked List Cycle II (2 solutions)
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- Linux-支持中文
转自:http://www.centoscn.com/CentosBug/osbug/2014/0919/3776.html 英文版的linux系统默认不支持中文显示 那么如何显示中文呢? 可以使用l ...
- Android 手机震动功能实现
1首先在AndroidManifest.xml中添加震动权限 <uses-permission android:name="android.permission.VIBRATE&quo ...
- shell 基本学习
1)查看当前shell echo $SHELL 2)查看兼容shell more /etc/shells 3) 脚本第一行 #!/bin/bash 4) 变量(变量名称的开头是一个字母或下划线符号,后 ...
- Linux 配置rdate时间服务器方法
配置多台服务器时,经常需要让各个服务器之间的时间保持同步,如果服务器有外网环境,可以直接同外部的时间服务器更新时间,可以采用rdate命令更新时间: rdate -s tick.greyware.co ...
- mysql工作中常见问题
1.Error Code: 1044 - Access denied for user 'root'@'localhost' to database 'information_schema'Query ...
- Android开发6——布局中的wrap_content和fill_parent以及match_parent
一.言简意赅 fill_parent 是让控件宽或者高占全屏 wrap_content是让控件的高或宽仅仅把控件里的内容包裹住而不是全屏 二.分别来看 1 fill_parent 设置一个构件的布局 ...
- extjs中组件监听器里面的回调函数说明
近期在看项目源代码的时候发现了例如以下代码,当中_searchSupplierStore是JsonStore对象 _searchSupplierStore.on('beforeload',functi ...
- Linux下的tree命令 --Linux下文件夹树查看
Linux下的tree命令 --Linux下文件夹树查看 有时我们须要生成文件夹树结构,能够使用的有ls -R,可是实际效果并不好 这时须要用到tree命令,可是大部分Linux系统是默认不安装该命令 ...