• linux内核阻塞IO


    阻塞操作是指在执行设备操作时,若不能获得资源,则挂起进程直到满足可操作的条件后再进行操作。被挂起的进程进入休眠状态,被从调度器的运行队列移走,知道等待的条件被满足。而非阻塞的进程在不能进行设备操作时,并不挂起,它或者放弃,或者不停地查询,直到可以操作为止。

    在linux驱动程序中,可以使用等待队列(wait queue)来实现阻塞进程的唤醒。

    1. 等待队列头

    一个等待队列有一个“等待队列头”来管理,wait_queue_head_t定义在linux/wait.h,实现在kernel/wait.c中。

    struct __wait_queue_head {
        spinlock_t      lock;
        struct list_head    task_list;
    };
    typedef struct __wait_queue_head wait_queue_head_t;
    DECLARE_WAIT_QUEUE_HEAD(name);  //静态
    wait_queue_head_t my_queue;
    init_waitqueue_head(&my_queue);

    2. 定义等待队列

    typedef struct __wait_queue wait_queue_t;
    typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int flags, void *key);
    int default_wake_function(wait_queue_t *wait, unsigned mode, int flags, void *key);
    
    struct __wait_queue {
        unsigned int        flags;
    #define WQ_FLAG_EXCLUSIVE   0x01
        void            *private;
        wait_queue_func_t   func;
        struct list_head    task_list;
    };

    DECLARE_WAIT_QUEUE(name, tsk);

    该宏用于定义并初始化一个名为name的等待队列。

    #define __WAITQUEUE_INITIALIZER(name, tsk) {                
        .private    = tsk,                      
        .func       = default_wake_function,            
        .task_list  = { NULL, NULL } }
    
    #define DECLARE_WAITQUEUE(name, tsk)                    
        wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)

    3. 移除和添加等待队列

    extern void add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);
    extern void add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait);
    extern void remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait);

    4. 等待事件

    #define wait_event(wq, condition) 
    #define wait_event_timeout(wq, condition, timeout) 
    #define wait_event_interruptible(wq, condition)
    #define wait_event_interruptible_timeout(wq, condition, timeout) 
    /**
     * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
     * @wq: the waitqueue to wait on
     * @condition: a C expression for the event to wait for
     * @timeout: timeout, in jiffies
     *
     * The process is put to sleep (TASK_INTERRUPTIBLE) until the
     * @condition evaluates to true or a signal is received.
     * The @condition is checked each time the waitqueue @wq is woken up.
     *
     * wake_up() has to be called after changing any variable that could
     * change the result of the wait condition.
     *
     * Returns:
     * 0 if the @timeout elapsed, -%ERESTARTSYS if it was interrupted by
     * a signal, or the remaining jiffies (at least 1) if the @condition
     * evaluated to %true before the @timeout elapsed.
     */

    等待第一个参数wq作为等待队列头的等待队列被唤醒,而且第2个参数condition必须满足,否则继续阻塞。

    timeout以jiffy为单位。
    5. 唤醒队列

    void wake_up(wait_queue_head_t *queue);
    void wake_up_interruptible(wait_queue_head_t *queue);

    6. 在等待队列上睡眠

    extern void sleep_on(wait_queue_head_t *q);
    extern long sleep_on_timeout(wait_queue_head_t *q, signed long timeout);
    extern void interruptible_sleep_on(wait_queue_head_t *q);
    extern long interruptible_sleep_on_timeout(wait_queue_head_t *q, signed long timeout);

    将当前进程添加到等待队列中,从而在等待队列上睡眠。当超时发生时,进程被唤醒。

    参考:

    1. 等待队列

  • 相关阅读:
    【强转】QEMU+GDB调试linux内核全过程
    从上往下打印二叉树
    栈的压入、弹出序列
    包含main函数的栈
    顺时针打印矩阵
    Linux终端美化
    Linux 终端美化
    KDE桌面环境自带的Konsole终端配置
    KDE美化及常用设置
    KDE常用桌面插件总结
  • 原文地址:https://www.cnblogs.com/embedded-linux/p/8596419.html
Copyright © 2020-2023  润新知