linux 驱动学习之poll与select
来源:互联网 发布:淘宝卖家怎么修改主营 编辑:程序博客网 时间:2024/06/05 02:55
在使用socket或串口的时候应用代码经常使用select来判断有没接收到数据,驱动需要做什么处理,应用层用户才能正确判断有数据收到并读取数据呢?使用select能够监视我们需要监视的文件描述符的变化情况——读写或是异常。
先建立简单模型,应用使用select监听设备是否可读,当设备可读将数据读出后继续监听。已经有了一个监听设备可读状态的进程,还要打开终端使用echo xxx >/dev/moddev60向设备写入数据,设备驱动收到数据后控制设备变成可读状态,select进程select函数返回,查询设备可读后读出数据。代码为了演示做了简化不做并发出错处理等。
先看下应用c代码
#include <stdio.h>#include <fcntl.h>unsigned char rdBuf[1024];int main (int *argc,char**argv){ int fd,res;fd_set fset; FD_ZERO(&fset); fd=open("/dev/moduledev60",O_RDWR); if(fd<0){ printf("open devices error\n");return -1; } while(1) { FD_ZERO(&fset); FD_SET(fd,&fset); res=select(fd+1,&fset,NULL,NULL,NULL); if(res==-1) { close(fd); return -1; } else if(res==0) break; else { /*select 返回后判断可读*/ if(FD_ISSET(fd,&fset)) { read(fd,rdBuf,1024); printf("read data:%s\n",rdBuf); } } }close(fd);return 0;}
驱动关键代码
struct file_operations ops={ .owner=THIS_MODULE , .read =fileops_read , .write=fileops_write, .poll =fileops_poll};
DECLARE_WAIT_QUEUE_HEAD(rdWait);//poll_wait中使用static unsigned char rdBuf[1024];//测试用读写缓存 写入数据和读取数据0结尾static unsigned char rdFlag=0; //可读标记 0不可读缓存内无数据 1可读ssize_t fileops_read(struct file *filp, char __user *buff, size_t count, loff_t *offp){ unsigned int len; if(rdFlag==0) return 0; //不可读时直接返回 len=strlen(rdBuf); //读缓存字符串长 这个模型只做字符串(0结尾)的写入和读取 copy_to_user(buff,rdBuf,len); // rdFlag=0; //清标记 return len;}ssize_t fileops_write(struct file *filp, const char __user *buff, size_t count, loff_t *offp){ copy_from_user(rdBuf,buff,count);// rdBuf[count]=0; //补字符串结束标记 rdFlag=1; //标记可读 poll函数检测这个标记 wake_up_interruptible(&rdWait); /*fileops_poll 函数使用了 poll_wait 加入了rdWait等待列队,执行后fileops_poll会被调用,如果不使用等待列队 fileops_poll只在应用调用select时执行一次 这样有数据select也不会返回*/ return count;}unsigned int fileops_poll(struct file *filp,poll_table *wait){ /*这里测试只处理的可读标记 也可以增加可写标记*/ int mask=0; poll_wait(filp,&rdWait,wait); //这个函数不阻塞的,当状态改变时,应该唤醒列队这个函数才会重新调用更新新的状态。 if(rdFlag) { mask=POLLIN|POLLRDNORM; } return mask; }
编译后测试结果先运行应用,监听设备是否可读[root@localhost ctest]# ./main 打开另外一个终端,写入数据[root@localhost ctest]# echo eeee>/dev/moduledev60[root@localhost ctest]# echo eeee>/dev/moduledev60[root@localhost ctest]# echo eeee>/dev/moduledev60应用判断到可读后读出数据[root@localhost ctest]# ./main read data:eeeeread data:eeeeread data:eeee
- linux 驱动学习之poll与select
- Linux内核驱动之poll和select
- linux之select/poll
- linux设备驱动--非阻塞IO与select,poll调用
- linux驱动之poll
- Linux学习:多路复用 select / poll
- linux高级字符设备驱动之 四 poll方法(select多路监控原理与实现)
- Linux 设备驱动--- Poll 方法 --- Select
- Linux I/O多路复用之select,poll与epoll区别
- Linux I/O多路复用之select,poll与epoll区别
- linux驱动之poll操作
- Linux驱动之poll机制
- linux驱动---file_operations之poll
- 驱动学习之POLL机制
- Linux设备驱动学习之 Poll机制分析
- Linux驱动学习8(非阻塞IO的实现--select/poll方法)
- Linux 内核驱动--阻塞与非阻塞机制及Poll/Select分析if
- linux设备驱动--非阻塞IO与select,poll调用 (续1)
- 用C#写Excel插件
- php生成xml文件
- 常用正则表代式集
- STL binder1st binder2nd bind1st bind2nd区别
- 正则表达式语法
- linux 驱动学习之poll与select
- OIO与NIO区别
- 互联网通信-HttpClient、XML解析(W3C)
- 设定sql server定期自动备份数据库
- centos安装scrapy
- 使用boost io_service时,需要注意的东西
- Redhat6.1安装oracle11g
- WaitForMultipleObjects()
- 编写自己的jquery插件