zoukankan      html  css  js  c++  java
  • linux嵌入式系统驱动程序的阻塞与异步

    对于那些需要进程独占的设备,需要使用linux提供的阻塞编程。步骤如下:

    1.在设备驱动程序中定义该设备的进程等待列多,并将其初始化

    static wait_queue_head_t wait_queue;

    init_waitqueue_head(&wait_queue);

    2.在设备驱动程序的读操作中,调用函数wait_event 实现阻塞访问

    int mixled_read(struct file * filp,char__user * buffer,size_t size,loff_t * ppos)

    {

    wait_event(wait_queue,Elmixled_dev->full_flag!=0);

    elmixled_dev->full_flag = 0;

    if(copy_to_user(buffer,Elmixled_dev->buf,read_lea))

    {

    printk(“copy to user err!”);

    }

    return lead_len;

    }

    在写操作中,调用wake_up函数唤醒该设备等待进程列队上的进程

    int mixled_write(struct file * filp, const char__user * buffer,size_t size,loff_t * ppos)

    {

    int write_len =size;

    if(copy_from_user(Elmixled_dev->buf,buffer,write_len))

    {

    printk(“copy from user err”);

    }

    elmixled_dev->full_flag = 1;

    wake_up(&wait_queue);

    return write_len;

    }

    对于异步驱动编程,需要做的步骤如下:

    1.定义设备驱动程序的异步通知列队

    struct fasync_struct * fasync_queue;

    实现设备驱动程序的异步操作函数fasync,并在异步操作函数fasync中调用fasync_helper函数将当前进程添加到设备驱动程序的异步通知列队

    int mixled_fasync(int fd,struct file * file ,int on)

    {

    printk(“enter mixled_fasync function”);

    return fasync_helper(fd,file,on,&Elmixled_dev->fasync_queue);

    }

    struct file_operations mixled_ops={

        …

        .fasync = mixled_fasync

    }

    3.在设备驱动程序中,当检测到设备状态信息发生变化时,如数据到达或者按键被按下就需要通知应用程序,调用函数kill_fasync想应用程序发送异步通知消息

    irqeturn_t mixled_isr(int irq,void * dev_id)

    {

    printk(“key interrupt take place”);

    if(Elmixled_dev-》fasync_queue)

    {

    kill_fasync(&Elmixled_dev->fasync_queue,SIGIO,POLLMSG)

    }

    return IRQ_HANDLED;

    }

    4.在设备驱动的close函数中,调用mixled_fasync函数将当前进程从设备的异步通知列队中移除

    mixled_fasync(-1,file,0)

  • 相关阅读:
    前端学习笔记之闭包——看了一张图终于明白啥是闭包了
    前端学习笔记之闭包——看了一张图终于明白啥是闭包了
    前端学习笔记之闭包——看了一张图终于明白啥是闭包了
    前端学习笔记之闭包——看了一张图终于明白啥是闭包了
    Unity碰撞检测
    Unity碰撞检测
    Unity碰撞检测
    Unity碰撞检测
    关于JavaScript中事件的一些重要说明
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/pavkoo/p/3479163.html
Copyright © 2011-2022 走看看