标签:
在用户程序中,select()和poll()也是与设备阻塞与非阻塞访问息息相关的,使用非阻塞I/O的应用程序通常会使用select和poll系统调用查询是否可对设备进行无阻塞的访问。select系统调用最终会引发设备驱动中的poll函数被执行。
一、select()系统调用:
用于多路监控,当没有一个文件满足要求时,select将阻塞调用进程。
1.select()原型:
int select(int maxfdp,fd_set *readfds,fd_set *writefds,fd_set *exceptfds,const struct timeval *timeout);
/*
*@maxfd : 需要检查的文件描述符个数,数值应该比是三组fd_set中最大数更大(即一般取所有文件描述符的最大值加1),而不是实际文件描述符的总数。
*@readfds: 用来检查可读性的一组文件描述符。
*@writesfds: 用来检查可写性的一组文件描述符。
*@exceptsfds:用来检查意外状态的文件描述符。(注:错误并不是意外状态)
*@timeout:NULL指针代表无限等待,否则是指向timeval结构的指针,代表最长等待时间。(如果其中tv_sec和tv_usec都等于0, 则文件描述符的状态不被影响,但函数并不挂起)
返回值:
(1)正常情况下返回满足要求的文件描述符个数;
(2)经过了timeout等待后仍无文件满足要求,返回0;
(3)如果select被某个信号中断,将返回-1并设置errno为EINTR;
(4)若出错,返回-1并设置相应的errno;
2.select的使用方法:
(1)将要监控的文件添加到文件描述符集;
(2)调用select开始监控;
(3)判断文件是否发生变化;
3.系统提供四个宏对描述符集进行操作
void FD_SET(int fd, fd_set *fdset); //将文件描述符fd添加到文件描述符集fdset中;
void FD_CLR(int fd, fd_set *fdset); //从文件描述符集fdset中清除文件描述符fd;
void FD_ISSET(int fd, fd_set *fdset); //在调用select后使用FD_ISSET来检测文件描述符集中的文件fd发生了变化
void FD_ZERO(fd_set *fdset);//清空文件描述符集
二、Poll方法:
1.poll函数原型:
unsigned int(*poll)(struct file *filp, struct poll_table *wait);
//第一个参数为file结构体指针,第二个参数为轮询表指针。
这个函数应该进行以下两项工作:
(1)对可能引起设备文件状态变化的等待队列调用poll_wait()函数,将对应等待队列添加到poll_table中;
(2)返回表示是否能对设备进行无阻塞可读或可写访问的掩码;
位掩码:POLLRDNORM, POLLIN,POLLOUT,POLLWRNORM
设备可读,通常返回:(POLLIN | POLLRDNORM)
设备可写,通常返回:(POLLOUT | POLLWRNORM)
三、调用过程:
Linux下select调用的过程:
1、用户层应用程序调用select(),底层调用poll())
2、核心层调用sys_select() ------> do_select()
最终调用文件描述符fd对应的struct file类型变量的struct file_operations *f_op的poll函数。
poll指向的函数返回当前可否读写的信息。
1)如果当前可读写,返回读写信息。
2)如果当前不可读写,则阻塞进程,并等待驱动程序唤醒,重新调用poll函数,或超时返回。
3、驱动需要实现poll函数。
当驱动发现有数据可以读写时,通知核心层,核心层重新调用poll指向的函数查询信息。
poll_wait(filp,&wait_q,wait) // 此处将当前进程加入到等待队列中,但并不阻塞
在中断中使用wake_up_interruptible(&wait_q)唤醒等待队列。
四、实例分析:
1.memdev.h
- #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
-
- struct mem_dev
- {
- char *data;
- unsigned long size;
- wait_queue_head_t inq;
- };
-
- #endif /* _MEMDEV_H_ */
2.memdev.c
- #include <linux/module.h>
- #include <linux/types.h>
- #include <linux/fs.h>
- #include <linux/errno.h>
- #include <linux/mm.h>
- #include <linux/sched.h>
- #include <linux/init.h>
- #include <linux/cdev.h>
- #include <asm/io.h>
- #include <asm/system.h>
- #include <asm/uaccess.h>
-
- #include <linux/poll.h>
- #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 (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;
- }
-
-
- static loff_t mem_llseek(struct file *filp, loff_t offset, int whence)
- {
- loff_t newpos;
-
- switch(whence) {
- case 0:
- newpos = offset;
- break;
-
- case 1:
- newpos = filp->f_pos + offset;
- break;
-
- case 2:
- newpos = MEMDEV_SIZE -1 + offset;
- break;
-
- default:
- return -EINVAL;
- }
- if ((newpos<0) || (newpos>MEMDEV_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_wait(filp, &dev->inq, wait);
-
-
- if (have_data) mask |= POLLIN | POLLRDNORM;
-
- 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 < 0)
- return result;
-
-
- 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 < MEMDEV_NR_DEVS; 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));
-
- }
-
- 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);
3.app-write.c
- #include <stdio.h>
-
- 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;
-
- }
4.app-read.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/ioctl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <sys/select.h>
- #include <sys/time.h>
- #include <errno.h>
-
- int main()
- {
- int fd;
- fd_set rds;
- int ret;
- char Buf[128];
-
-
- strcpy(Buf,"memdev is char dev!");
- printf("BUF: %s\n",Buf);
-
-
- fd = open("/dev/memdev0",O_RDWR);
-
- FD_ZERO(&rds);
- FD_SET(fd, &rds);
-
-
- strcpy(Buf,"Buf is NULL!");
- printf("Read BUF1: %s\n",Buf);
-
- ret = select(fd + 1, &rds, NULL, NULL, NULL);
- if (ret < 0)
- {
- 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;
- }
Linux高级字符设备之Poll操作
标签:
原文地址:http://www.cnblogs.com/liuchengchuxiao/p/4214243.html