zoukankan      html  css  js  c++  java
  • 驱动开发 —— 多路复用(select 和 poll)

    多路复用

    1、概念

    假设:在单进程与单线程情况下,应用程序要同时处理多路IO流(多个设备)

    如果在应用程序中采用while(1) { read()  ... }进行处理,那么每次只能处理一个设备,其他设备有请求时,则会被延迟,甚至丢失数据。如果在一个设备处理中阻塞,则不会接着执行,也不能处理其他设备的数据。因此,可以采用多路复用的方法:select 、 poll

      阻塞模式下会有两种情况:等待 、对数据进行操作。多路复用就是实现了等待的动作。

    2、多路复用的思想

      —先构造一张有关描述符的表,然后调用一个函数(select/poll)。当这些文件描述符中的一个或多个已经准备好进行IO时,函数才返回。

      —函数返回时,告诉进程那个描述符已经就绪,可以进行IO操作。 

    3、实现步骤

    1. 把关心的文件描述符加入到fd_set集合中
    2. 调用select / poll函数去监控集合fd_set中的文件描述符(阻塞等待集合中一个或多个文件描述符有数据)
    3. 当有数据时,退出select()阻塞
    4. 依次判断哪个文件描述符有数据
    5. 依次处理有数据的文件描述符的数据

     4、poll的应用

    1, 需要打开多个文件(多个设备)
    
    2, 利用poll来实现监控fd的读,写,出错
        #include <poll.h>
    
       int poll(struct pollfd *fds, nfds_t nfds, int timeout);
       参数1: 表示多个文件描述符集合
            struct pollfd描述的是文件描述符的信息
            struct pollfd {
               int   fd;  //文件描述符
               short events;   //希望监控fd的什么事件:读,写,出错
                            POLLIN 读,
                            POLLOUT 写,
                            POLLERR出错
               short revents;    //结果描述,表示当前的fd是否有读,写,出错
                            //用于判断,是内核自动赋值
                            POLLIN 读,
                            POLLOUT 写,
                            POLLERR出错
            };
        参数2:被监控的fd的个数
        参数3: 监控的时间:
                    正: 表示监控多少ms
                    负数: 无限的时间去监控
                    0: 等待0ms,类似于非阻赛
        返回值: 负数:出错
                大于0,表示fd中有数据
                等于0: 时间到

    5、在驱动代码中实现poll接口

      如果应用中使用poll对设备文件进行了监控,那么设备驱动就必须实现poll接口

    unsigned int key_drv_poll(struct file *filp, struct poll_table_struct *pts)
    {
        
        // 返回一个mask值
        unsigned int mask;
        // 调用poll_wait,将当前到等待队列注册系统中
        poll_wait(filp, &key_dev->wq_head, pts);
        
        // 1,当没有数据到时候返回一个0
        if(!key_dev->key_state)
            mask = 0;
    
        // 2,有数据返回一个POLLIN
        if(key_dev->key_state)
            mask |= POLLIN;
    return mask; } const struct file_operations key_fops = { .poll = key_drv_poll, };

    6、示例--多路复用poll实现

      1 #include <linux/init.h>
      2 #include <linux/module.h>
      3 #include <linux/of.h>
      4 #include <linux/of_irq.h>
      5 #include <linux/interrupt.h>
      6 #include <linux/slab.h>
      7 #include <linux/fs.h>
      8 #include <linux/device.h>
      9 #include <linux/kdev_t.h>
     10 #include <linux/err.h>
     11 #include <linux/device.h>
     12 #include <asm/io.h>
     13 #include <asm/uaccess.h>
     14 #include <linux/wait.h>
     15 #include <linux/sched.h>
     16 #include <linux/poll.h>
     17 
     18 
     19 #define GPXCON_REG 0X11000C20   //不可以从数据寄存器开始映射,要配置寄存器
     20 #define KEY_ENTER  28
     21 
     22 //0、设计一个描述按键的数据的对象
     23 struct key_event{
     24     int code;    //按键类型:home,esc,enter
     25     int value;   //表状态,按下,松开
     26 };
     27 
     28 //1、设计一个全局对象——— 描述key的信息
     29 struct key_desc{
     30     unsigned int dev_major;
     31     int irqno;  //中断号
     32     struct class  *cls;
     33     struct device *dev;
     34     void *reg_base;
     35     struct key_event event;
     36     wait_queue_head_t wq_head;
     37     int key_state;   //表示是否有数据
     38 };
     39 
     40 struct key_desc *key_dev;
     41 
     42 
     43 irqreturn_t key_irq_handler(int irqno, void *devid)
     44 {
     45     printk("----------%s---------",__FUNCTION__);
     46 
     47     int value;
     48     //读取按键状态
     49     value = readl(key_dev->reg_base + 4) & (0x01<<2);
     50     
     51     if(value){
     52         printk("key3 up
    ");
     53         key_dev->event.code  = KEY_ENTER;
     54         key_dev->event.value = 0;
     55     }else{
     56         printk("key3 down
    ");
     57         key_dev->event.code  = KEY_ENTER;
     58         key_dev->event.value = 1;
     59     }
     60 
     61     //表示有数据,唤醒等待队列中的等待项
     62     wake_up_interruptible(&key_dev->wq_head);
     63     
     64     //同时设置标志位,表示有数据
     65     key_dev->key_state = 1;
     66     
     67     return IRQ_HANDLED;
     68 }
     69 
     70 
     71 //获取中断号
     72 int get_irqno_from_node(void)
     73 {
     74     int irqno;
     75     //获取设备树中的节点
     76     struct device_node *np = of_find_node_by_path("/key_int_node");
     77     if(np){
     78         printk("find node success
    ");
     79     }else{
     80         printk("find node failed
    ");
     81     }
     82 
     83     //通过节点去获取中断号
     84     irqno = irq_of_parse_and_map(np, 0);
     85     printk("iqrno = %d",key_dev->irqno);
     86 
     87     return irqno;
     88 }
     89 
     90 ssize_t key_drv_read (struct file * filp, char __user * buf, size_t count, loff_t * fops)
     91 {
     92     //printk("----------%s---------",__FUNCTION__);
     93     int ret;
     94 
     95     //在没有数据时,进行休眠
     96         //key_state在zalloc初始化空间后,为0,则阻塞
     97     wait_event_interruptible(key_dev->wq_head, key_dev->key_state);
     98     
     99     ret = copy_to_user(buf, &key_dev->event, count);
    100     if(ret > 0)
    101     {
    102         printk("copy_to_user error
    ");
    103         return -EFAULT;
    104     }
    105 
    106     
    107     //传递给用户数据后,将数据清除,否则APP每次读都是第一次的数据
    108     memset(&key_dev->event, 0, sizeof(key_dev->event));
    109     key_dev->key_state = 0;
    110     
    111     return count;
    112 }
    113 
    114 ssize_t key_drv_write (struct file *filp, const char __user * buf, size_t count, loff_t * fops)
    115 {
    116     printk("----------%s---------",__FUNCTION__);
    117     return 0;
    118 }
    119 
    120 int key_drv_open (struct inode * inode, struct file *filp)
    121 {
    122     printk("----------%s---------",__FUNCTION__);
    123     return 0;
    124 }
    125 
    126 int key_drv_close (struct inode *inode, struct file *filp)
    127 {
    128     printk("----------%s---------",__FUNCTION__);
    129     return 0;
    130 }
    131 
    132 unsigned int key_drv_poll (struct file * filp, struct poll_table_struct *pts)
    133 {
    134     //返回一个mask值
    135     unsigned int mask = 0;
    136     
    137     //调用poll_wait,将当前的等待队列注册到系统中
    138     poll_wait(filp, &key_dev->wq_head, pts);
    139     //1、当没有数据的时候返回0
    140     if(!key_dev->key_state)
    141     {
    142         mask = 0;
    143     }
    144     //2、当有数据的时候返回POLLIN
    145     if(key_dev->key_state)
    146         mask |= POLLIN;
    147 
    148     return mask;
    149 }
    150 
    151 
    152 const struct file_operations key_fops = {
    153     .open    = key_drv_open,
    154     .read    = key_drv_read,
    155     .write   = key_drv_write,
    156     .release = key_drv_close,
    157     .poll    = key_drv_poll,
    158 
    159 };
    160 
    161 
    162 
    163 static int __init key_drv_init(void)
    164 {
    165     //演示如何获取到中断号
    166     int ret;
    167     
    168     //1、设定全局设备对象并分配空间
    169     key_dev = kzalloc(sizeof(struct key_desc), GFP_KERNEL);  //GFP_KERNEL表正常分配内存
    170                           //kzalloc相比于kmalloc,不仅分配连续空间,还会将内存初始化清零
    171 
    172     //2、动态申请设备号
    173     key_dev->dev_major = register_chrdev(0, "key_drv", &key_fops);
    174 
    175     //3、创建设备节点文件
    176     key_dev->cls = class_create(THIS_MODULE, "key_cls");
    177     key_dev->dev = device_create(key_dev->cls, NULL, MKDEV(key_dev->dev_major, 0), NULL, "key0");
    178 
    179     //4、硬件初始化 -- 地址映射或中断申请    
    180     
    181     key_dev->reg_base = ioremap(GPXCON_REG,8);
    182 
    183     key_dev->irqno = get_irqno_from_node();
    184     
    185     ret = request_irq(key_dev->irqno, key_irq_handler, IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING, 
    186         "key3_eint10", NULL);
    187     if(ret != 0)
    188     {
    189         printk("request_irq error
    ");
    190         return ret;
    191     }
    192 
    193     //初始化等待队列头
    194     init_waitqueue_head(&key_dev->wq_head);  //wait_queue_head_t *q
    195     
    196     
    197     
    198     return 0;
    199 }
    200 
    201 static void __exit key_drv_exit(void)
    202 {
    203     iounmap(GPXCON_REG);
    204     free_irq(key_dev->irqno, NULL);  //free_irq与request_irq的最后一个参数一致
    205     device_destroy(key_dev->cls, MKDEV(key_dev->dev_major, 0));
    206     class_destroy(key_dev->cls);
    207     unregister_chrdev(key_dev->dev_major, "key_drv");
    208     kfree(key_dev);
    209 }
    210 
    211 
    212 module_init(key_drv_init);
    213 module_exit(key_drv_exit);
    214 
    215 MODULE_LICENSE("GPL");
    key_drv.c
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <stdlib.h>
     4 #include <unistd.h>
     5 #include <sys/types.h>
     6 #include <sys/stat.h>
     7 #include <fcntl.h>
     8 #include <poll.h>
     9 
    10 
    11 #define KEY_ENTER  28
    12 
    13 //0、设计一个描述按键的数据的对象
    14 struct key_event{
    15     int code;    //按键类型:home,esc,enter
    16     int value;   //表状态,按下,松开
    17 };
    18 
    19 
    20 int main(int argc, char *argv[])
    21 {
    22     struct key_event event;
    23     int ret;
    24     char in_buf[128];
    25     
    26     int fd;
    27     fd = open("/dev/key0", O_RDWR);
    28     if(fd < 0)
    29     {
    30         perror("open");
    31         exit(1);
    32     }
    33 
    34     //监控多个文件fd
    35     struct pollfd pfd[2];
    36     pfd[0].fd = fd;  //监控按键输入
    37     pfd[0].events = POLLIN;
    38 
    39     pfd[1].fd = 0; //标准输入:0,标准输出:1,标准出错:2
    40     pfd[1].events = POLLIN;
    41 
    42     while(1)
    43     {
    44         printf("-----------------start to poll--------------------
    ");
    45         ret = poll(pfd, 2, -1);  //在驱动中fops要实现poll接口
    46         
    47         if(ret > 0)
    48         {
    49             //表示2个fd中至少一个发生读事件
    50             if(pfd[0].revents & POLLIN)  //revents用于判断,会由内核自动填充
    51             {
    52                 read(pfd[0].fd, &event, sizeof(struct key_event));  //每次读必有数据
    53                 if(event.code == KEY_ENTER)
    54                 {
    55                     if(event.value)
    56                     {
    57                         printf("APP__ key enter down
    ");
    58                     }else{
    59 
    60                         printf("APP__ key enter up
    ");
    61                     }
    62                 }
    63             }
    64 
    65             if(pfd[1].revents & POLLIN)
    66             {
    67                 fgets(in_buf, 128, stdin);  //从标准输入中获取128字节存入in_buf
    68                 printf("in_buf = %s
    ",in_buf);
    69             }
    70         }
    71         else{
    72                 perror("poll");
    73                 exit(1);
    74             }
    75         printf("----------------------End poll----------------------
    ");
    76     }
    77     
    78     
    79     close(pfd[0].fd);
    80 
    81     return 0;
    82 }
    key_test.c

        

  • 相关阅读:
    【jQuery基础学习】03 jQuery中的事件与动画
    【jQuery基础学习】02 jQuery的DOM操作
    【jQuery基础学习】01 jQuery选择器
    【jQuery基础学习】00 序
    【JS复习笔记】07 复习感想
    【JS复习笔记】06 方法
    洛谷P1067 多项式输出 NOIP 2009 普及组 第一题
    嵊州D6T2 城市 city
    博弈论小结
    嵊州D2T4 十七个中毒的英国人 poisoning
  • 原文地址:https://www.cnblogs.com/y4247464/p/12390653.html
Copyright © 2011-2022 走看看