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

在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);

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

参考:

linux 内核阻塞,linux内核阻塞IO相关推荐

  1. Linux 阻塞和非阻塞IO 实验

    目录 阻塞和非阻塞IO 阻塞和非阻塞简介 等待队列 轮询 Linux 驱动下的poll 操作函数 阻塞IO 实验 硬件原理图分析 实验程序编写 运行测试 非阻塞IO 实验 硬件原理图分析 实验程序编写 ...

  2. Linux IO - 同步,异步,阻塞,非阻塞

    From:http://blog.csdn.net/historyasamirror/article/details/5778378 同步/异步,阻塞/非阻塞概念深度解析:http://blog.cs ...

  3. Linux之阻塞与非阻塞IO

    目录 一.阻塞与非阻塞IO简介 1.阻塞IO 2.非阻塞IO 二.应用程序阻塞与非阻塞 1.阻塞 2.查询(非阻塞) ①select ②poll ③epoll 三.驱动程序阻塞与非阻塞 1.等待队列( ...

  4. Linux 阻塞和非阻塞 IO 实验

    目录 一.阻塞和非阻塞简介 1.IO 概念 2.阻塞与非阻塞 二.等待队列 1.等待队列头 2.等待队列项 3.将队列项添加/移除等待队列头 4.等待唤醒 5.等待事件 三.轮询 1.应用程序的非阻塞 ...

  5. Linux 阻塞和非阻塞 IO简介

    Linux 阻塞和非阻塞 IO简介 阻塞和非阻塞简介 1.阻塞IO 2.非阻塞IO 阻塞和非阻塞简介 这里的 IO 指的是 Input/Output,也就是输入/输出,是应用程序对驱动设备的输入/输出 ...

  6. linux驱动开发 - 10_阻塞和非阻塞 IO

    文章目录 1 阻塞和非阻塞 IO 1.1 阻塞和非阻塞简介 1.2 等待队列 1.等待队列头 2.等待队列项 3.将队列项添加/移除等待队列头 4.等待唤醒 5.等待事件 1.3 Linux驱动下的p ...

  7. 【Linux驱动开发】阻塞和非阻塞IO

    IO:应用程序对设备驱动进行输入/输出操作. 阻塞IO:当资源不用时,将应用程序对应的线程挂起,当资源可用时,唤醒任务. 非阻塞IO:当资源不可用时,应用程序轮询等待或放弃.具有超时处理机制 设备驱动 ...

  8. linux驱动学习笔记(三)阻塞与非阻塞IO

    Linux驱动中阻塞与非阻塞IO 前言 阻塞 非阻塞 一.等待队列 1.等待队列头 2.等待队列 模板 二.轮询 模板 总结 前言 阻塞和非阻塞io是两种不同的设备访问方式. 阻塞 阻塞IO表示在执行 ...

  9. Linux驱动(六)设备驱动中的阻塞与非阻塞IO

    我们在Linux学习(二十三)IO模型中了解了LINUX中IO模型,IO模型最简单的可以分为阻塞IO和非阻塞IO.并且学习了一个用如何使用阻塞操作和非阻塞操作.而应用层之所以能实现阻塞操作和非阻塞操作 ...

  10. 嵌入式Linux 阻塞和非阻塞 IO 驱动设备访问模式

    阻塞和非阻塞 IO 是 Linux 驱动开发里面很常见的两种设备访问模式, 在编写驱动的时候一定要考虑到阻塞和非阻塞. 阻塞与非阻塞简介 阻塞操作是指在执行设备操作时, 若不能获得资源, 则挂起进程直 ...

最新文章

  1. c语言顺序表有效元素长度,用C语言描述的顺序表类型
  2. android 联系人编辑界面,android – 以编程方式编辑联系人的姓名/电话号码
  3. 【SAP技术】SAP不能修改一个已经分配给交货单的HU
  4. Golomb及指数哥伦布编码原理介绍及实现
  5. 四、HTTP响应报文格式
  6. 如何将网页保存为图片_如何用浏览器插件一键批量下载网页图片?
  7. 如何在windows下刷amd显卡的bios和简易救活教程
  8. latex符号正下方下标
  9. 工程总承包(EPC)最高投标限价政策解说
  10. OC中继承代理委托类别
  11. ✨✨✨【C语言】带你用最短的时间刷题(附解题思路、具体代码)不断更新(二)✨✨✨
  12. 面向对象---抽象和封装
  13. 约瑟夫环 有15个人围成一圈,按顺序淘汰
  14. 使用阿里云的国内镜像仓库地址
  15. 用sublime搭建基于GoSublime+gocode+MarGo的下载SublimeGO开发环境
  16. 无感支付发展实践中存在的问题与政策建议
  17. KISSY基础篇乄KISSY简介
  18. ie9 下面输入框后面怎么出现一个黑色叉叉
  19. 框架成为新的编程语言的7种理由
  20. 查看pg 用户组_PostgreSQL 角色用户管理

热门文章

  1. 身份证号码的正则表达式及验证详解(JavaScript,Regex)
  2. XamarinForms教程构建XamarinForms开发环境
  3. 尹中立:“人造牛市”的结局可能会非常悲惨
  4. 安装终端服务和终端服务授权,激活终端服务授权
  5. Ubuntu Linux 下优化 swap 交换分区及调整swap大小
  6. 用于参考的学生信息管理系统(数据库简单 可自己参考创建)
  7. Lucene:基于Java的全文检索引擎简介 车东
  8. VGA12h与VGA寄存器
  9. CCF个贷违约预测0.891的baseline代码分享
  10. python——画一个笑脸