poll调用深入解析 http://blog.csdn.net/zmxiangde_88/article/details/8099049
poll调用和select调用实现的功能一样,都是网络IO利用的一种机制。先看一下poll的调用形式
poll调用
#include <poll.h>
int poll(struct pollfd fds[], nfds_t nfds, int timeout);
struct pollfd结构如下:【在源码文件poll.h文件中】
struct pollfd {
int fd;
short events;
short revents;
};
这个结构中fd表示文件描述符,events表示请求检测的事件,revents表示检测之后返回的事件,如果当某个文件描述符有状态变化时,revents的值就不为空。
二,参数说明
四、韦东山的按键中断驱动代码中,加入poll机制的示例:
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <poll.h> /* forthdrvtest */ int main(int argc, char **argv) { int fd; unsigned char key_val; int ret; struct pollfd fds[1]; fd = open("/dev/buttons", O_RDWR); if (fd < 0) { printf("can‘t open!\n"); } fds[0].fd = fd; fds[0].events = POLLIN; while (1) { ret = poll(fds, 1, 5000); if (ret == 0) { printf("time out\n"); } else { read(fd, &key_val, 1); printf("key_val = 0x%x\n", key_val); } } return 0; }
韦东山jz2440 CD1\drivers_and_test\forth_drv中,一个字符设备要实现poll的功能,就在原来的的基础上,加了如下代码:
static unsigned forth_drv_poll(struct file *file, poll_table *wait) { unsigned int mask = 0; poll_wait(file, &button_waitq, wait); // 不会立即休眠 if (ev_press) mask |= POLLIN | POLLRDNORM; return mask; } static struct file_operations sencod_drv_fops = { .owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */ .open = forth_drv_open, .read = forth_drv_read, .release = forth_drv_close, .poll = forth_drv_poll, };
button_waitq是驱动中定义的等待队列头 static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
DECLARE_WAIT_QUEUE_HEAD()的说明 http://blog.csdn.net/commandow/article/details/5774587
static DECLARE_WAIT_QUEUE_HEAD(button_waitq); 生成一个等待队列头wait_queue_head_t, 名字为button_waitq。
原文:http://www.cnblogs.com/mylinux/p/5090264.html