linux 内核阻塞,linux内核阻塞IO

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

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

1. 等待队列头

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

struct__wait_queue_head {

spinlock_tlock;structlist_head task_list;

};

typedefstruct __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;

typedefint (*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 {

unsignedintflags;#define WQ_FLAG_EXCLUSIVE 0x01

void *private;

wait_queue_func_t func;structlist_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 longtimeout);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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值