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操作 的相关文章

  • 内核模式下的线程(和进程)与用户模式下的线程(和进程)有什么区别?

    我的问题 1 书中现代操作系统 它说线程和进程可以处于内核模式或用户模式 但没有明确说明它们之间有什么区别 2 为什么内核态线程和进程的切换比用户态线程和进程的切换花费更多 3 现在 我正在学习Linux 我想知道如何在LINUX系统中分别
  • 如何在 Bash 中给定超时后终止子进程?

    我有一个 bash 脚本 它启动一个子进程 该进程时不时地崩溃 实际上是挂起 而且没有明显的原因 闭源 所以我对此无能为力 因此 我希望能够在给定的时间内启动此进程 如果在给定的时间内没有成功返回 则将其终止 有没有simple and r
  • 是否可以创建一个脚本来保存和恢复权限?

    我正在使用 Linux 系统 需要对一组嵌套文件和目录进行一些权限实验 我想知道是否没有某种方法可以保存文件和目录的权限 而不保存文件本身 换句话说 我想保存权限 编辑一些文件 调整一些权限 然后将权限恢复到目录结构中 将更改的文件保留在适
  • 删除 Git 存储库,但保留所有文件

    在我使用 Linux 的过程中的某个时刻 我决定将我的主目录中的所有内容都放入源代码管理中是个好主意 我不是在问这是否是一个好主意 我是在问如何撤销它 删除存储库的原因是我最近安装了 Oh My Zsh 而且我非常喜欢它 问题是我的主目录有
  • php exec 返回的结果比直接进入命令行要少

    我有一个 exec 命令 它的行为与通过 Penguinet 给 linux 的相同命令不同 res exec cd mnt mydirectory zcat log file gz echo res 当将命令直接放入命令行时 我在日志文件
  • 如果在等待“read -s”时中断,在子进程中运行 bash 会破坏 tty 的标准输出吗?

    正如 Bakuriu 在评论中指出的那样 这基本上与BASH 输入期间按 Ctrl C 会中断当前终端 https stackoverflow com questions 31808863 bash ctrlc during input b
  • 如何构建任务“gems:install”

    我正在将 Rails 应用程序部署到 Linux 服务器 并且缺少一些 rake 任务 包括 rake gems install 和 rake db 我正在运行来自 GEM 的 Rails 2 3 4 为什么是这样 我该如何解决 我可以以某
  • Urwid:使光标不可见

    我正在使用 urwid 它是一个用于在 ncurses 中设计终端用户界面的 Python 框架 但有一件事我在 urwid 中无法做到 而这在 Curses 中很容易做到 使光标不可见 现在 选择按钮时光标是可见的 而且看起来很丑 有办法
  • 在 C++ linux 中将 STRINGS 写入串口

    我知道这个问题遍布互联网 但仍然没有任何东西能让我完全解决这个问题 我想用 C linux 将数据写入 Propeller 板的串行端口 从控制台获取输入时程序运行良好 但是当我向它写入字符串时总是返回 ERROR Invalid comm
  • 如何允许应用程序声明“https”方案 URI? (即如何从 https URL 打开桌面应用程序?)

    目前我正在尝试为 OAuth 2 0 授权流程创建一个客户端 实际上是一个本机应用程序 并且在规范中就在这儿 https www rfc editor org rfc rfc8252 section 7 2据说有 3 种方法来处理重定向 U
  • Mcrt1.o和Scrt1.o有什么用?

    我坚持使用以下两个文件 即 Mcrt1 o 和 Scrt1 o 谁能帮我知道这两个文件的用途 如何使用它 我们以 gcrt1 o 为例 在使用 pg 选项编译进行性能测试时非常有用 谢谢 表格的文件 crt o总是 C 运行时启动代码 大部
  • 如何在 GNU/Linux 上设置 Subversion (SVN) 服务器 - Ubuntu [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我有一台运行 Ubuntu 的笔记本电脑 我想将其用作 Subversion 服务器 既让我自己在本地承诺 也让其他人远程承诺 要使其
  • Linux 上的 RTLD_LOCAL 和dynamic_cast

    我们有一个由应用程序中的一些共享库构成的插件 我们需要在应用程序运行时更新它 出于性能原因 我们在卸载旧插件之前加载并开始使用新插件 并且只有当所有线程都使用旧插件完成后 我们才卸载它 由于新插件和旧插件的库具有相同的符号 我们dlopen
  • 在 Ubuntu 中找不到 X11/Xlib.h

    我试图在 Linux 上使用 open gl 编写一个相当简单的程序 但在编译时它说 编译拇指 egl 我对 GL 完全陌生 不知道出了什么问题 快速搜索使用 apt search Xlib h 打开 libx11 dev 包 但纯 Ope
  • Linux中使用管道进行进程间通信

    我已经编写了在 linux 中写入数字以进行管道传输的代码 如下所示 但显示错误 任何人都可以帮助我解决这个问题 基本上该程序的问题陈述如下 一个程序将打开一个管道 向管道写入一个数字 其他程序将打开同一管道 读取数字并打印它们 关闭两个管
  • 使用 inotify 的正确方法是什么?

    我想使用inotifyLinux 上的机制 我希望我的应用程序知道文件何时aaa被改变了 您能给我提供一个如何做到这一点的示例吗 文档 来自监视文件系统活动 inotify https developer ibm com tutorials
  • 如何将后台作业的输出分配给 bash 变量?

    我想在 bash 中运行后台作业并将其结果分配给一个变量 我不喜欢使用临时文件 并且希望同时运行多个类似的后台任务 root root var echo hello world root root echo var hello world
  • 如何使用 echo 写入非 ASCII 字符?

    如何写非ASCII http en wikipedia org wiki ASCII使用 echo 的字符 是否有转义序列 例如 012或类似的东西 我想使用以下方法将 ASCII 字符附加到文件中 echo gt gt file 如果您关
  • 码头无故停止

    我需要经验丰富的码头用户的建议 我在负载均衡器 亚马逊云 后面维护着 2 台 Linux 机器 使用 Jetty 9 0 3 有时我的 Jetty 容器会被 Thread 2 无故关闭 同时地 显示以下日志并且容器无故停止 没有错误 没有例
  • Linux“屏幕”的 Windows 等效项还是其他替代方案?

    我正在寻找一种在 Windows 环境中控制程序的方法 我希望它与 Linux 软件有点相似 screen 我搜索的原因是我需要使用标识符启动一个程序 在 Windows 上 这样我以后就能够关闭该特定程序 而无需关闭其他任何程序 即使实际

随机推荐

  • 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系统调用最终会引发设备驱动