poll 结构体相关定义
/* sys/poll.h */
/* 文件描述符数量 */
typedef unsigned long int nfds_t;
/*
* 轮训请求的数据结构体定义
* fd : 待轮训的描述符
* events : poll 轮询关心的事件类型
* revents : 实际发生的事件类型
*/
struct pollfd
{
int fd;
short int events;
short int revents;
};
/*
* 函数定义
* *fds : struct pollfd 结构体
* nfds : 轮询描述符的数量
* timeout : 轮询的时间
* 返回值:
* 1)< 0:poll函数执行失败,设置全局变量errno。
* 2)= 0:待检测的描述符无事件发生。
* 3)> 0: 已准备好描述符事件的数目。
*/
int poll(struct pollfd *fds, nfds_t nfds, int timeout)
/*
* bits/poll.h
* 事件类型,这些bits用于设置events来指定感兴趣的事件类型,在revents中指示文件描述符状态。
*/
#define POLLIN 0X001 /* 有数据待读 there is data to read. */
#define POLLPRI 0X002 /* 有紧急的数据待读 there is urgent data to read. */
#define POLLOUT 0X004 /* 现在写将不会被阻塞 writing now will not block. */
/* these value are defined in XPG4.2 */
#define POLLRDNORM 0x040 /* normal data may be read. */
#define POLLRDBAND 0X080 /* priority data may be read. */
#define POLLWRNORM 0X100 /* writing now will be block. */
#define POLLWEBAND 0X200 /* priority data may be written. */
/* these are extensions for Linux */
#define POLLMSG 0X400
#define POLLREMOVE 0X1000
#define POLLRDHUP 0X2000
/* 事件类型将被隐式轮询,不需要设置events, 但是会在revents中指定描述符状态 */
#define POLLERR 0x008 /* error condition. */
#define POLLHUP 0X010 /* HUNG UP */
#define POLLNVAL 0X020 /* invalid polling request. */
poll 函数使用
int fd;
int ret;
struct pollfd fdset[1];
nfds_t nfds;
fd = open(device,type);
while(1){
memset((void *)fdset,0x0,sizeof(fdset));
fdset.fd = fd;
fdset.events = POLLPRI;
ret = poll(fdset,nfds, 2000)
if(ret < 0){
printf("poll err!");
}else if(ret == 0){
printf("polling...");
}else{
//相关操作
}
}