IO监控 poll

来源:互联网 发布:乌克兰胖爸 知乎 编辑:程序博客网 时间:2024/06/16 07:30

poll可移植,epoll不可移植。

poll, ppoll - wait for some event on a file descriptor

poll() performs a similar task to select(2): it waits for one of a set of file descriptors to become ready to perform I/O.
poll() 执行的任务与select()相似:他等待一个集合中某一个文件描述符就绪。


int poll(struct pollfd *fds, nfds_t nfds, int timeout);

fds
The set of file descriptors to be monitored is specified in the fds argument, which is an array of structures of the following form:
struct pollfd {
int fd; /* file descriptor */
short events; /* requested events */
short revents; /* returned events */
};
被监控的文件描述符在参数fds中被指定,这是一个上面数据类型的元素组成的数组。
fd 输入参数,是你关心的文件描述符,如果写为负值,则events被忽略,revents返回0;
events 是输入参数,是你关心的文件描述符事件,如果输入0,那么所有关于fd的事件都被忽略,revents返回0;
revents 是输出参数,当事件发生后由内核填充。返回值可能是events中指定的任意值,或者POLLERR, POLLHUP, POLLNVAL其中的值.

nfds The caller should specify the number of items in the fds array in nfds.
调用poll()的程序需要在nfds中指明等待的文件描述符项目的序号。

timeout The timeout argument specifies the number of milliseconds that poll() should block waiting for a file descrip‐tor to become ready.
timeout指定等待的毫秒数,timeout指定为负数值表示无限超时,当timeout为0的时候会立即返回。

poll()会在指定时间内等待指定的文件描述符的指定事件发生后返回,否则将阻塞。

返回值和错误代码
成功时,poll()返回结构体中revents域不为0的文件描述符个数;
如果在超时前没有任何事件发生,poll()返回0;
失败时,poll()返回-1,并设置errno为下列值之一:
EBADF 一个或多个结构体中指定的文件描述符无效。
EFAULT fds指针指向的地址超出进程的地址空间。
EINTR 请求的事件之前产生一个信号,调用可以重新发起。
EINVAL nfds参数超出PLIMIT_NOFILE值。
ENOMEM 可用内存不足,无法完成请求。

ppoll()与poll的关系和select与pselect的关系类似。


/*=========================================*/

#include <stdio.h>
#include <unistd.h>
#include <poll.h>

#define TIMEOUT  5

int main(void)
{
struct pollfd fds[2];
int ret;

//watch stdin for input
fds[0].fd = STDIN_FILENO;
fds[0].events = POLLIN;

//watch stdout for ability to write (almost always true)
fds[1].fd = STDOUT_FILENO; //set to -1 to test stdin only
fds[1].events = POLLOUT;

//all set, block
ret = poll(fds, 2, TIMEOUT*1000);
if(ret == -1){
perror("poll");
return 1;
}else if(ret == 0){
printf("%d seconds elapsed.\n", TIMEOUT);
return 0;
}

if(fds[0].revents & POLLIN)
printf("stdin is readable\n");
if(fds[1].revents & POLLOUT)
printf("stdout is writeable\n");

return 0;
}




The bits that may be set/returned in events and revents are defined in <poll.h>:

POLLIN There is data to read.

POLLPRI
There is urgent data to read (e.g., out-of-band data on TCP socket; pseudoterminal master in
packet mode has seen state change in slave).

POLLOUT
Writing now will not block.

POLLRDHUP (since Linux 2.6.17)
Stream socket peer closed connection, or shut down writing half of connection. The _GNU_SOURCE
feature test macro must be defined (before including any header files) in order to obtain this
definition.

POLLERR
Error condition (output only).

POLLHUP
Hang up (output only).

POLLNVAL
Invalid request: fd not open (output only).
When compiling with _XOPEN_SOURCE defined, one also has the following, which convey no further information
beyond the bits listed above:

POLLRDNORM
Equivalent to POLLIN.

POLLRDBAND
Priority band data can be read (generally unused on Linux).

POLLWRNORM
Equivalent to POLLOUT.

POLLWRBAND
Priority data may be written.

Linux also knows about, but does not use POLLMSG.

0 0
原创粉丝点击