Linux高级字符设备之Poll操作

2023-05-16

在用户程序中,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

<div class="language_js" style="border: 1px solid rgb(204, 204, 204); padding: 5px; overflow: auto; margin: 5px 0px; font-family: 'Courier New' !important; background-color: rgb(245, 245, 245);"><img id="code_img_opened_38253450-1c6c-400f-a62a-602c08555f78" class="code_img_opened" src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="" style="border: 0px; vertical-align: middle; padding-right: 5px;" /><div id="language_js_open_38253450-1c6c-400f-a62a-602c08555f78" class="language_js_hide"><div class="language_js_toolbar" style="margin-top: 5px;"><span class="language_js_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a target=_blank title="复制代码" style="color: rgb(119, 0, 0); font-size: 13px; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; font-family: 'Courier New' !important;">#ifndef _MEMDEV_H_
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">#define</span> _MEMDEV_H_

#ifndef MEMDEV_MAJOR
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">#define</span> MEMDEV_MAJOR 0   /*预设的mem的主设备号*/
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">#endif</span>

#ifndef MEMDEV_NR_DEVS
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">#define</span> MEMDEV_NR_DEVS 2    /*设备数*/
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">#endif</span>

#ifndef MEMDEV_SIZE
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">#define</span> MEMDEV_SIZE 4096
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">#endif</span>


/*mem设备描述结构体*/
struct mem_dev                                     
{                                                        
  char *data;                      
  unsigned long size; 
  wait_queue_head_t inq;  
};

#endif /* _MEMDEV_H_ */  


  
<span style="line-height: 23.3999996185303px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: rgb(238, 238, 221);">2.</span><span style="line-height: 23.3999996185303px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: rgb(238, 238, 221);">memdev.c</span>
<span style="line-height: 23.3999996185303px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; background-color: rgb(238, 238, 221);">
</span><div class="language_js_toolbar" style="margin-top: 5px; font-family: 'Courier New'; line-height: 21.6000003814697px; background-color: rgb(245, 245, 245);"><span class="language_js_copy" style="padding-right: 5px; line-height: 1.5 !important;"><a target=_blank title="复制代码" style="color: rgb(119, 0, 0); font-size: 13px; border: none !important;"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none !important;" /></a></span></div><pre style="margin-top: 0px; margin-bottom: 0px; white-space: pre-wrap; word-wrap: break-word; line-height: 21.6000003814697px; font-family: 'Courier New' !important;">#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 <span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">memdev.h</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span>

<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">static</span> mem_major = MEMDEV_MAJOR;
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">bool</span> have_data = <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">false</span>; <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">表明设备有足够数据可供读</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>

module_param(mem_major, <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span>, S_IRUGO);

<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> mem_dev *mem_devp; <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">设备结构体指针</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>

<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> cdev cdev; 

<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">文件打开函数</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> mem_open(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> inode *inode, <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> file *filp)
{
    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> mem_dev *dev;
    
    <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">获取次设备号</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> num = MINOR(inode->i_rdev);

    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (num >= MEMDEV_NR_DEVS) 
            <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> -ENODEV;
    dev = &mem_devp[num];
    
    <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">将设备描述结构指针赋值给文件私有数据指针</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
    filp->private_data = dev;
    
    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>; 
}

<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">文件释放函数</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> mem_release(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> inode *inode, <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> file *filp)
{
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>;
}

<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">读函数</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">static</span> ssize_t mem_read(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> file *filp, <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">char</span> __user *buf, size_t size, loff_t *ppos)
{
  unsigned <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">long</span> p =  *ppos;
  unsigned <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> count = size;
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> ret = <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>;
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> mem_dev *dev = filp->private_data; <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">获得设备结构体指针</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>

  <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">判断读位置是否有效</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (p >= MEMDEV_SIZE)
    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>;
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (count > MEMDEV_SIZE - p)
    count = MEMDEV_SIZE - p;
    
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">while</span> (!have_data) <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 没有数据可读,考虑为什么不用if,而用while </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  {
        <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (filp->f_flags & O_NONBLOCK)
            <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> -EAGAIN;
    
    wait_event_interruptible(dev->inq,have_data);
  }

  <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">读数据到用户空间</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (copy_to_user(buf, (<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">void</span>*)(dev->data + p), count))
  {
    ret =  - EFAULT;
  }
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">else</span>
  {
    *ppos += count;
    ret = count;
   
    printk(KERN_INFO <span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">read %d bytes(s) from %d\n</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span>, count, p);
  }
  
  have_data = <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">false</span>; <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 表明不再有数据可读 </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 唤醒写进程 </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> ret;
}

<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">写函数</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">static</span> ssize_t mem_write(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> file *filp, <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">const</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">char</span> __user *buf, size_t size, loff_t *ppos)
{
  unsigned <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">long</span> p =  *ppos;
  unsigned <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> count = size;
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> ret = <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>;
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> mem_dev *dev = filp->private_data; <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">获得设备结构体指针</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  
  <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">分析和获取有效的写长度</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (p >= MEMDEV_SIZE)
    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>;
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (count > MEMDEV_SIZE - p)
    count = MEMDEV_SIZE - p;

  <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">从用户空间写入数据</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (copy_from_user(dev->data + p, buf, count))
    ret =  - EFAULT;
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">else</span>
  {
    *ppos += count;
    ret = count;
    
    printk(KERN_INFO <span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">written %d bytes(s) from %d\n</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span>, count, p);
  }
  
  have_data = <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">true</span>; <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 有新的数据可读 </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
    
    <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 唤醒读进程 </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
    wake_up(&(dev->inq));

  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> ret;
}

<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> seek文件定位函数 </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">static</span> loff_t mem_llseek(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> file *filp, loff_t offset, <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> whence)
{ 
    loff_t newpos;

    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">switch</span>(whence) {
      <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">case</span> <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>: <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> SEEK_SET </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
        newpos = offset;
        <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">break</span>;

      <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">case</span> <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">1</span>: <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> SEEK_CUR </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
        newpos = filp->f_pos + offset;
        <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">break</span>;

      <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">case</span> <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">2</span>: <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> SEEK_END </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
        newpos = MEMDEV_SIZE -<span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">1</span> + offset;
        <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">break</span>;

      <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">default</span>: <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> can't happen </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
        <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> -EINVAL;
    }
    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> ((newpos<<span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>) || (newpos>MEMDEV_SIZE))
        <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> -EINVAL;
        
    filp->f_pos = newpos;
    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> newpos;

}
unsigned <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> mem_poll(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> file *filp, poll_table *wait)
{
    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> mem_dev  *dev = filp->private_data; 
    unsigned <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> mask = <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>;
    
   <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">将等待队列添加到poll_table </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
    poll_wait(filp, &dev->inq,  wait);
 
    
    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (have_data)         mask |= POLLIN | POLLRDNORM;  <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> readable </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>

    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> mask;
}


<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">文件操作结构体</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">static</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">const</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> 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,
};

<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">设备驱动模块加载函数</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">static</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> memdev_init(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">void</span>)
{
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> result;
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">int</span> i;

  dev_t devno = MKDEV(mem_major, <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>);

  <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 静态申请设备号</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (mem_major)
    result = register_chrdev_region(devno, <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">2</span>, <span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">memdev</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span>);
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">else</span>  <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 动态分配设备号 </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  {
    result = alloc_chrdev_region(&devno, <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>, <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">2</span>, <span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">memdev</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span>);
    mem_major = MAJOR(devno);
  }  
  
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (result < <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>)
    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> result;

  <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">初始化cdev结构</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  cdev_init(&cdev, &mem_fops);
  cdev.owner = THIS_MODULE;
  cdev.ops = &mem_fops;
  
  <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 注册字符设备 </span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  cdev_add(&cdev, MKDEV(mem_major, <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>), MEMDEV_NR_DEVS);
   
  <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;"> 为设备描述结构分配内存</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  mem_devp = kmalloc(MEMDEV_NR_DEVS * <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">sizeof</span>(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> mem_dev), GFP_KERNEL);
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">if</span> (!mem_devp)    <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">申请失败</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  {
    result =  - ENOMEM;
    <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">goto</span> fail_malloc;
  }
  memset(mem_devp, <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>, <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">sizeof</span>(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">struct</span> mem_dev));
  
  <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">为设备分配内存</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">for</span> (i=<span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>; 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, <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>, MEMDEV_SIZE);
  
      <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">初始化等待队列</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
     init_waitqueue_head(&(mem_devp[i].inq));
     <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">//</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">init_waitqueue_head(&(mem_devp[i].outq));</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">
</span>  }
   
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>;

  fail_malloc: 
  unregister_chrdev_region(devno, <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">1</span>);
  
  <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">return</span> result;
}

<span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">模块卸载函数</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">static</span> <span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">void</span> memdev_exit(<span style="color: rgb(0, 0, 255); line-height: 1.5 !important;">void</span>)
{
  cdev_del(&cdev);   <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">注销设备</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  kfree(mem_devp);     <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">释放设备结构体内存</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
  unregister_chrdev_region(MKDEV(mem_major, <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">0</span>), <span style="color: rgb(128, 0, 128); line-height: 1.5 !important;">2</span>); <span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">/*</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">释放设备号</span><span style="color: rgb(0, 128, 0); line-height: 1.5 !important;">*/</span>
}

MODULE_AUTHOR(<span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">David Xie</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span>);
MODULE_LICENSE(<span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">GPL</span><span style="color: rgb(128, 0, 0); line-height: 1.5 !important;">"</span>);

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];
    
    /*初始化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 < 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;    
}  



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);//清空文件描述符集  

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Linux高级字符设备之Poll操作 的相关文章

  • ssh 连接超时

    我无法在 git 中 ssh 到 github bitbucket 或 gitlab 我通常会收到以下错误消息 如何避免它 输出 ssh T email protected cdn cgi l email protection i ssh
  • Tomcat Intellij Idea:远程部署

    RackSpace 云服务器 Ubuntu 12 04 Intellij Idea 11 1 2 Windows 8 Tomcat 7 0 26 JDK 6 在 Intellij Idea 上 当我尝试在远程 Tomcat 7 服务器上运行
  • vmsplice() 和 TCP

    在原来的vmsplice 执行 有人建议 http lwn net Articles 181169 如果您的用户态缓冲区是管道中可容纳的最大页面数的 2 倍 则缓冲区后半部分成功的 vmsplice 将保证内核使用缓冲区的前半部分完成 但事
  • 找不到包“gdk-pixbuf-2.0”

    我正在尝试在 Amazon Linux 发行版实例上构建 librsvg 我已经通过 yum 安装了大部分依赖项 其中一些在实例上启用的默认 yum 存储库中不可用 因此必须从头开始构建它们 我已经走了很远 但还停留在最后一点 跑步时sud
  • 仅使用containerd(不使用Docker)修剪容器镜像

    如果我刚刚containerd安装在 Linux 系统上 即 Docker 是not安装 如何删除未使用的容器映像以节省磁盘空间 Docker 就是这么方便docker system prune https docs docker com
  • Google BQ:运行参数化查询,其中参数变量是 BQ 表目标

    我正在尝试从 Linux 命令行为 BQ 表目标运行 SQL 此 SQL 脚本将用于多个日期 客户端和 BQ 表目标 因此这需要在我的 BQ API 命令行调用中使用参数 标志 parameter 现在 我已经点击此链接来了解参数化查询 h
  • 在 Linux 上以编程方式设置 DNS 名称服务器

    我希望能够通过我的 C C 程序为 Linux 上的 DNS 名称服务器添加 IP 地址 我在一个带有只读 etc resolv conf 的嵌入式平台上 这意味着我不能简单地将 nameserver xxx xxx xxx xxx 行添加
  • linux-x64 二进制文件无法在 linuxmusl-x64 平台上使用错误

    我正在安装Sharp用于使用 package json 的 Nodejs 项目的 docker 映像上的映像压缩包 当我创建容器时 我收到有关 Sharp 包的以下错误 app node modules sharp lib libvips
  • 如何在linux中以编程方式获取dir的大小?

    我想通过 C 程序获取 linux 中特定目录的确切大小 我尝试使用 statfs path struct statfs 但它没有给出确切的大小 我也尝试过 stat 但它返回任何目录的大小为 4096 请建议我如何获取 dir 的确切大小
  • 从 ttyUSB0 写入和读取,无法得到响应

    我对 Linux tty 不太有经验 我的环境是带有丰富 USB 串行的 Raspbian 什么有效 stty F dev ttyUSB0 38400 cu l dev ttyUSB0 s 38400 cu to dev ttyUSB0作品
  • C 语言的符号表

    我目前正在开发一种执行模式匹配的静态分析工具 我在用Flex https github com westes flex生成词法分析器 我编写了代码来管理符号表 我不太有经验C 所以我决定将符号表实现为线性链表 include
  • 为什么 fopen("any_path_name",'r') 不给出 NULL 作为返回值?

    在调试一些代码时 我得到如下内容 include
  • 如何让R使用所有处理器?

    我有一台运行 Windows XP 的四核笔记本电脑 但查看任务管理器 R 似乎一次只使用一个处理器 如何让 R 使用全部四个处理器并加速我的 R 程序 我有一个基本系统 我使用它在 for 循环上并行化我的程序 一旦您了解需要做什么 此方
  • 在 Mono 上运行 .Net MVC5 应用程序

    我正在 Windows 上的 Visual Studio 2013 中开发 Net 4 5 1 MVC5 应用程序 现在我想知道 是否可以在Linux Ubuntu 12 04 上运行这个应用程序 可以使用OWIN吗 Owin 可以自托管运
  • 从 Xlib 转换为 xcb

    我目前正在将我的一个应用程序从 Xlib 移植到 libxcb 但在查找有关我有时使用的 XInput2 扩展的信息时遇到了一些麻烦 libxcb 中有 XInput2 实现吗 如果是的话 在哪里可以找到文档 目前我在使用此功能时遇到问题
  • 如何使用waf构建共享库?

    我想使用构建一个共享库waf http code google com p waf 因为它看起来比 GNU 自动工具更容易 更简洁 到目前为止 我实际上有几个与我开始编写的 wscript 有关的问题 VERSION 0 0 1 APPNA
  • 配置tomat的server.xml文件并自动生成mod_jk.conf

    我在用apache 2 2 15 and tomcat6 6 0 24 on CentOS 6 4并希望使用 tomcat 服务器的功能 通过添加以下内容自动生成 mod jk conf 文件
  • 如何使用Android获取Linux内核的版本?

    如何在 Android 应用程序中获取 Linux 内核的版本 不是 100 确定 但我认为调用 uname r 需要 root 访问权限 无论如何 有一种不太肮脏的方法可以做到这一点 那就是 System getProperty os v
  • 尽管我已在 python ctypes 中设置了信号处理程序,但并未调用它

    我尝试过使用 sigaction 和 ctypes 设置信号处理程序 我知道它可以与python中的信号模块一起使用 但我想尝试学习 当我向该进程发送 SIGTERM 时 但它没有调用我设置的处理程序 只打印 终止 为什么它不调用处理程序
  • 从 Linux 内核模块中调用用户空间函数

    我正在编写一个简单的 Linux 字符设备驱动程序 以通过 I O 端口将数据输出到硬件 我有一个执行浮点运算的函数来计算硬件的正确输出 不幸的是 这意味着我需要将此函数保留在用户空间中 因为 Linux 内核不能很好地处理浮点运算 这是设

随机推荐

  • MFC改变控件位置和大小

    最近经常要用到改变控件在对话框上的位置和大小 xff0c 一直找不到有效的方法 xff0c 查看了很多资料 这篇博文还算靠谱 xff0c 转载到这里了 void CmyqeDlg OnSize UINT nType int cx int c
  • mfc控件位置调整和坐标确定 .

    在mfc工程中控件或者窗口位置的调整是经常遇到的 xff0c 特别是基于对话框的工程 位置的调整包括坐标 长度和宽度的变化 xff0c 一般在窗口类的OnSize函数中实现 控件位置的调整涉及的函数有 xff1a GetWindowRect
  • MFC笔记2(控件位置调整)

    1 根据计算 xff0c 使用GetClientRect amp 就可以调整好 2 遇到了OK和Cancel按钮通过GetDlgItem xff08 ID xff09 找不到id资源号的情况 xff0c 最后通过给控件绑定控件变量到类中就可
  • MFC控件随窗口大小变化原理及实现

    本文主要针对MFC的dialog xff0c 实现控件随窗口大小变化 原理 xff1a 首先获取dialog的初始大小 xff0c 当窗口发送变动时 xff0c 调用OnSize事件和方法 xff0c 计算缩放比例 xff0c 然后对界面中
  • MFC对话框中处理Enter或Esc按键事件方法

    建立好的MFC的对话框 xff0c 按下Enter或Esc时 xff0c 系统会调用 默认 事件处理函数 xff0c 也就是OnOK xff0c 倘若自己的CFormView子类或者CDialog子类没有重写OnOK 函数 xff0c 则会
  • VS2010 MFC中控件、对话框等背景颜色动态修改的方法

    通过类向导 xff0c 或者手动添加消息 xff1a WM CTLCOLOR xff0c 其消息响应函数为 xff1a afx msg HBRUSH OnCtlColor CDC pDC CWnd pWnd UINT nCtlColor 1
  • MFC窗口风格 WS_style/WS_EX_style

    窗口风格 Window style WS BORDER 有边框窗口 WS CAPTION 必须和WS BORDER风格配合 xff0c 但不能与WS DLGFRAME风格一起使用 指示窗口包含标题要部分 WS CHILD 说明窗口为子窗口
  • MFC 窗体样式修改

    窗体创建之后 xff0c 如何设置窗体的样式呢 xff1f 一般情况下使用GetWindowLongW与SetWindowLongW即可实现窗体样式的修改或者使用ModifyStyle 关于MFC存在GetWindowLongW和GetWi
  • 更改MFC生成的程序的默认exe图标

    一般更改打开程序时的左上角的程序图标使用如下方法 xff1a 对话框为例 xff0c 在对话框构造函数中m hIcon 61 AfxGetApp gt LoadIcon IDI ICON3 将最后的IDR MAINFRAME改为自己的图标即
  • C#与USB设备通信

    最近有一个项目 xff0c 也是我硕士大论文要写的东西 xff0c 就是从两个线阵相机上读取数据 gt 分析数据 gt 做到利用线阵相机检测接触线 铁路接触网 几何参数的功能 由于线阵相机是从武汉的一个创业公司买的 xff0c 实在是坑死个
  • C++常用类型转换

    char是C语言标准数据类型 xff0c 字符型 xff0c 至于由几个字节组成通常由编译器决定 xff0c 一般一个字节 Windows为了消除各编译器的差别 xff0c 重新定义了一些数据类型 CHAR为单字节字符 还有个WCHAR为U
  • C++在dll中获取自身路径(非exe调用路径)

    include 34 stdafx h 34 include lt fstream gt include lt iostream gt include lt windows h gt using namespace std HMODULE
  • MFC/VC++中怎样设置位图按钮并且位图不会覆盖文字——–位图按钮

    1 第一次尝试 设置 IDC BUTTON3按钮风格的bitmap为true 在OnInitialDilog中 xff1a CButton cbpTest 61 NULL HINSTANCE hInstance 61 AfxGetResou
  • MFC 之 重绘按键Cbutton

    上次我们学习了如何美化对话框的界面 xff0c 这次我们为上次的对话框添加两个按钮 xff0c 一个是关闭按钮 xff0c 另一个是最小化按钮 xff0c 好 xff0c 现在我们先看一下效果 xff1a 是不是很难看 xff0c 因为我们
  • VC的MFC中重绘函数的使用总结(整理)

    原文网址 xff1a http www cnblogs com x8023z archive 2008 12 09 mfc33 html 在刷新窗口时经常要调用重绘函数 MFC提供了三个函数用于窗口重绘 InvalidateRect amp
  • DrawItem

    原文链接 http blog csdn net jiftlixu article details 4893505 今天从CButton派生了一个类CUIButton xff0c 主要用于自绘 xff0c 按照基本的流程 xff0c 重写Dr
  • C/C++报错:全局变量重定义或是多次定义

    很多人可能直接把全局变量写进 h文件 xff0c 然后用多个文件包含这个头文件 xff0c 编译时就会报错 xff1a 变量重定义 头文件的作用就是要给外部提供接口使用的 xff0c 所以请记住 xff0c 只在 h中做声明 xff0c 在
  • C++ 包含目录、库目录、附加依赖项总结

    在使用opencv库 xff0c 以及其他库的时候 xff0c 经常会需要添加包含目录 库目录 附加依赖项等 现做一个总结吧 1 包含目录 是 h的头文件所在的目录 xff0c 如果没有正确包含目录 xff0c 代码中会出现红色的警告 xf
  • c++设置不适用预编译头

    编译器提示在末尾是否忘了添加stdafx h 可右键相应的 cpp文件 xff0c 设置c 43 43 设置不适用预编译头
  • Linux高级字符设备之Poll操作

    在用户程序中 xff0c select 和poll 也是与设备阻塞与非阻塞访问息息相关的 xff0c 使用非阻塞I O的应用程序通常会使用select和poll系统调用查询是否可对设备进行无阻塞的访问 select系统调用最终会引发设备驱动