在用户程序中,select()和poll()也是与设备阻塞与非阻塞访问相关的的论题。使用非阻塞IO的应用程序通常会使用select()和poll()系统调用查询是否可对设备进行无阻塞的访问。select()和poll()系统调用最终会引发设备驱动中的poll()函数被执行。select()和poll的本质是一样的,前者在BSD UNIX中引入,后者在System V中引入。 select系统调用(功能):用于多路监控,当没有一个文件满足要求时,select将阻塞调用进程。 int select(int maxfd,fd_set *readfds,fd_set *writefds,fd_set *exceptfds,const struct timeval *timeval *timeout)maxfd:文件描述符范围,比待检测的最大文件描述符大1readfds:被读监控的文件描述符集writefds:被写监控的文件描述符集exceptfds:被异常监控的文件描述符集timeout:定时器 1、timeout为0时,不管是否有文件满足要求,都立刻返回,无文件满足要求返回0,有文件满足要求返回一个正值。2、timeout为NULL,select阻塞进程,直到某个文件满足要求。3、timeout值为正整数时,就是等待的最长时间,即select在timeout时间内阻塞进程。select返回值:1、正常情况下返回满足要求的文件描述符个数2、经过timeout等待后扔无文件满足要求,返回值为03、如果select被某个信号中断,它将返回-1并设置为errno为EINTR4、如果出错,返回-1并设置相应的errno系统提供了4个宏对描述符集进行操作#includevoid FD_SET(int fd,fd_set *fdset) 将文件描述符fd到文件描述符fdset中void FD_CLR(int fd,fd_set *fdset) 从文件描述符集fdset中清除文件描述符fd;void FD_ZERO(fd_set *fdset) 清空文件描述集fdsetvoid FD_ISSET(int fd,fd_set *fdset)检测文件描述符集fdset中的文件fd发生了变化,通过这个文件查看文件状态是否发生变化,然后就判断根据poll中的mask状态来判断文件是否可读。驱动程序poll实现方法:unsigned int (*poll)(struct file *filp,poll_table *wait)1、使用poll_wait将等待队列添加到poll_table中2、返回描述设备是否可读或可写的掩码位掩码:POLLIN 设备可读POLLRDNORM 数据可读POLLOUT 设备可写POLLWRNORM 数据可写设备可读通常返回(POLLIN|POLLRDNORM)设备可写通常返回(POLLOUT|POLLWRNONRM)驱动程序#ifndef _MEMDEV_H_#define _MEMDEV_H_#ifndef MEMDEV_MAJOR#define MEMDEV_MAJOR 0 /*预设的mem的主设备号*/#endif#ifndef MEMDEV_NR_DEVS#define MEMDEV_NR_DEVS 2 /*设备数*/#endif#ifndef MEMDEV_SIZE#define MEMDEV_SIZE 4096#endif/*mem设备描述结构体*/struct mem_dev { char *data; unsigned long size; wait_queue_head_t inq; };#endif /* _MEMDEV_H_ */#include #include #include #include #include #include #include #include #include #include #include #include #include "memdev.h"static mem_major = MEMDEV_MAJOR;bool have_data = false; /*表明设备有足够数据可供读*/module_param(mem_major, int, S_IRUGO);struct mem_dev *mem_devp; /*设备结构体指针*/struct cdev cdev; /*文件打开函数*/int mem_open(struct inode *inode, struct file *filp){ struct mem_dev *dev; /*获取次设备号*/ int num = MINOR(inode->i_rdev); if (num >= MEMDEV_NR_DEVS) return -ENODEV; dev = &mem_devp[num]; /*将设备描述结构指针赋值给文件私有数据指针*/ filp->private_data = dev; return 0; }/*文件释放函数*/int mem_release(struct inode *inode, struct file *filp){ return 0;}/*读函数*/static ssize_t mem_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos){ unsigned long p = *ppos; unsigned int count = size; int ret = 0; struct mem_dev *dev = filp->private_data; /*获得设备结构体指针*/ /*判断读位置是否有效*/ if (p >= MEMDEV_SIZE) return 0; if (count > MEMDEV_SIZE - p) count = MEMDEV_SIZE - p; while (!have_data) /* 没有数据可读,考虑为什么不用if,而用while */ { if (filp->f_flags & O_NONBLOCK) return -EAGAIN;wait_event_interruptible(dev->inq,have_data); } /*读数据到用户空间*/ if (copy_to_user(buf, (void*)(dev->data + p), count)) { ret = - EFAULT; } else { *ppos += count; ret = count; printk(KERN_INFO "read %d bytes(s) from %d\n", count, p); } have_data = false; /* 表明不再有数据可读 */ /* 唤醒写进程 */ return ret;}/*写函数*/static ssize_t mem_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos){ unsigned long p = *ppos; unsigned int count = size; int ret = 0; struct mem_dev *dev = filp->private_data; /*获得设备结构体指针*/ /*分析和获取有效的写长度*/ if (p >= MEMDEV_SIZE) return 0; if (count > MEMDEV_SIZE - p) count = MEMDEV_SIZE - p; /*从用户空间写入数据*/ if (copy_from_user(dev->data + p, buf, count)) ret = - EFAULT; else { *ppos += count; ret = count; printk(KERN_INFO "written %d bytes(s) from %d\n", count, p); } have_data = true; /* 有新的数据可读 */ /* 唤醒读进程 */ wake_up(&(dev->inq)); return ret;}/* seek文件定位函数 */static loff_t mem_llseek(struct file *filp, loff_t offset, int whence){ loff_t newpos; switch(whence) { case 0: /* SEEK_SET */ newpos = offset; break; case 1: /* SEEK_CUR */ newpos = filp->f_pos + offset; break; case 2: /* SEEK_END */ newpos = MEMDEV_SIZE -1 + offset; break; default: /* can't happen */ return -EINVAL; } if ((newposMEMDEV_SIZE)) return -EINVAL; filp->f_pos = newpos; return newpos;}unsigned int mem_poll(struct file *filp, poll_table *wait){ struct mem_dev *dev = filp->private_data; unsigned int mask = 0; /*将等待队列添加到poll_table */ poll_wait(filp, &dev->inq, wait); if (have_data) mask |= POLLIN | POLLRDNORM; /* readable */ return mask;}/*文件操作结构体*/static const struct file_operations mem_fops ={ .owner = THIS_MODULE, .llseek = mem_llseek, .read = mem_read, .write = mem_write, .open = mem_open, .release = mem_release, .poll = mem_poll,};/*设备驱动模块加载函数*/static int memdev_init(void){ int result; int i; dev_t devno = MKDEV(mem_major, 0); /* 静态申请设备号*/ if (mem_major) result = register_chrdev_region(devno, 2, "memdev"); else /* 动态分配设备号 */ { result = alloc_chrdev_region(&devno, 0, 2, "memdev"); mem_major = MAJOR(devno); } if (result return result; /*初始化cdev结构*/ cdev_init(&cdev, &mem_fops); cdev.owner = THIS_MODULE; cdev.ops = &mem_fops; /* 注册字符设备 */ cdev_add(&cdev, MKDEV(mem_major, 0), MEMDEV_NR_DEVS); /* 为设备描述结构分配内存*/ mem_devp = kmalloc(MEMDEV_NR_DEVS * sizeof(struct mem_dev), GFP_KERNEL); if (!mem_devp) /*申请失败*/ { result = - ENOMEM; goto fail_malloc; } memset(mem_devp, 0, sizeof(struct mem_dev)); /*为设备分配内存*/ for (i=0; i { mem_devp[i].size = MEMDEV_SIZE; mem_devp[i].data = kmalloc(MEMDEV_SIZE, GFP_KERNEL); memset(mem_devp[i].data, 0, MEMDEV_SIZE); /*初始化等待队列*/ init_waitqueue_head(&(mem_devp[i].inq)); //init_waitqueue_head(&(mem_devp[i].outq)); } return 0; fail_malloc: unregister_chrdev_region(devno, 1); return result;}/*模块卸载函数*/static void memdev_exit(void){ cdev_del(&cdev); /*注销设备*/ kfree(mem_devp); /*释放设备结构体内存*/ unregister_chrdev_region(MKDEV(mem_major, 0), 2); /*释放设备号*/}MODULE_AUTHOR("David Xie");MODULE_LICENSE("GPL");module_init(memdev_init);module_exit(memdev_exit);读测试程序#include #include #include #include #include #include #include #include #include #include int main(){int fd;fd_set rds;int ret;char Buf[128];/*初始化Buf*/strcpy(Buf,"memdev is char dev!");printf("BUF: %s\n",Buf);/*打开设备文件*/fd = open("/dev/memdev0",O_RDWR);FD_ZERO(&rds);FD_SET(fd, &rds);/*清除Buf*/strcpy(Buf,"Buf is NULL!");printf("Read BUF1: %s\n",Buf);ret = select(fd + 1, &rds, NULL, NULL, NULL);if (ret { printf("select error!\n"); exit(1);}if (FD_ISSET(fd, &rds)) read(fd, Buf, sizeof(Buf));/*检测结果*/printf("Read BUF2: %s\n",Buf);close(fd);return 0;}写测试程序#include int main(){FILE *fp = NULL;char Buf[128];/*打开设备文件*/fp = fopen("/dev/memdev0","r+");if (fp == NULL){printf("Open Dev memdev Error!\n");return -1;}/*写入设备*/strcpy(Buf,"memdev is char dev!");printf("Write BUF: %s\n",Buf);fwrite(Buf, sizeof(Buf), 1, fp);sleep(5);fclose(fp);return 0;}(待续) 11-17 03:32