等待队列

小白学Linux——等待队列(waitqueue) #等待队列介绍 当你编写Linux驱动程序、模块或内核程序时,某些进程会为了某些事件等待或休眠。在Linux中有多种处理睡眠和唤醒的方法,每种方法都适合不同的需求。等待队列(Waitqueue)也是处理这种情况的方法之一。 每当进程必须等待事件(例如数据到达或进程终止)时,它就应该进入睡眠状态。休眠会导致当前进程暂停执行,从而释放处理器给其它进程使用。一段时间后,进程等待的资源就绪之后,该进程会被唤醒。 等待队列是内核提供的一种实现等待的机制。顾名思义,等待队列是等待事件的进程列表。换句话说,等待队列用于在 阅读详情

How Processes Are Organized
------------------------------------   
    The runqueue lists group all processes in a TASK_RUNNING state. When it comes to grouping processes in other states, the various states call for different types of treatment, with Linux opting for one of the choices shown in the following list.
    运行队列链表把处于TASK_RUNNING状态的所有进程组织在一起。当要把其他状态的进程分组时,不同的状态要求不同的处理,Linux选择了下列方式之一:
    * Processes in a TASK_STOPPED, EXIT_ZOMBIE, or EXIT_DEAD state are not linked in specific lists. There is no need to group processes in any of these three states, because stopped, zombie, and dead processes are accessed only via PID or via linked lists of the child processes for a particular parent.
    * TASK_STOPPED, EXIT_ZOMBIE, or EXIT_DEAD 状态的进程不链接在专门的链表中。没有必要把处于这三种状态的进程进行分组。因为对于stopped, zombie, and dead进程而言,父进程只能通过PID或者子进程链表访问他们。

    * Processes in a TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE state are subdivided into many classes, each of which corresponds to a specific event. In this case, the process state does not provide enough information to retrieve the process quickly, so it is necessary to introduce additional lists of processes. These are called wait queues and are discussed next.
    * 把TASK_INTERRUPTIBLE or TASK_UNINTERRUPTIBLE状态的进程再分成很多类,每一类对应一个特定的事件。在这种情况下,进程状态提供的信息满足不了对进程的快速搜索,因此,有必要引入另外的进程链表。这些附加的链表叫等待队列(wait queue)

Wait queues
------------------------------------
    Wait queues have several uses in the kernel, particularly for interrupt handling, process synchronization, and timing. We'll just say here that a process must often wait for some event to occur, such as for a disk operation to terminate, a system resource to be released, or a fixed interval of time to elapse.
     等待队列在内核中有很多用途,尤其适合用于中断处理、进程同步及定时。我们在这里只说,进程经常必须等待某些事件的发生。例如,等待一个磁盘操作的终止,等待释放系统资源,或者等待时间经过固定的间隔。
    Wait queues implement conditional waits on events: a process wishing to wait for a specific event places itself in the proper wait queue and relinquishes control. Therefore, a wait queue represents a set of sleeping processes, which are woken up by the kernel when some condition becomes true.
    Wait queues 实现了在事件上的条件等待: 希望等待特定事件的进程把自己放进合适的等待队列,并放弃控制全。因此,等待队列表示一组睡眠的进程,当某一条件为真时,由内核唤醒它们。
   
    Wait queues are implemented as doubly linked lists whose elements include pointers to process descriptors. Each wait queue is identified by a wait queue head, a data structure of type wait_queue_head_t:
    等待队列由循环链表实现,其元素包括指向进程描述符的指针。每个等待队列都有一个等待队列头(wait queue head),等待队列头是一个类型为wait_queue_head_t的数据结构:

  • wait_queue_head_t
    struct __wait_queue_head {
        spinlock_t lock;
        struct list_head task_list;
    };
    typedef struct __wait_queue_head wait_queue_head_t;

wait_queue_head_t.lock
    Because wait queues are modified by interrupt handlers as well as by major kernel functions, the doubly linked lists must be protected from concurrent accesses. Synchronization is achieved by the lock spin lock in the wait queue head.

  • wait_queue_t
    Elements of a wait queue list are of type wait_queue_t:

    struct __wait_queue {
        unsigned int flags;
        struct task_struct * task;
        wait_queue_func_t func;
        struct list_head task_list;
    };
    typedef struct __wait_queue wait_queue_t;   
   
    Each element in the wait queue list represents a sleeping process, which is waiting for some event to occur; its descriptor address is stored in the task field. The task_list field contains the pointers that link this element to the list of processes waiting for the same event. 
    等待队列链表的每个元素代表一个睡眠进程,该进程等待某一事件的发生;它的描述符地址存放在task字段中。

    However, it is not always convenient to wake up all sleeping processes in a wait queue. For instance, if two or more processes are waiting for exclusive access to some resource to be released, it makes sense to wake up just one process in the wait queue. This process takes the resource, while the other processes continue to sleep. (This avoids a problem known as the "thundering herd," with which multiple processes are wakened only to race for a resource that can be accessed by one of them, with the result that remaining processes must once more be put back to sleep.)
    然而,要唤醒等待队列中所有睡眠的进程有时并不方便。例如,如果两个或多个进程正在等待互斥访问某一要释放的资源,仅唤醒等待队列中一个进程才有意义。这个进程占有资源,而其他进程继续睡眠.(这就避免了所谓的"雷鸣般群兽"问题,即唤醒多个进程只为了竞争一个资源,而这个资源只能有一个进程访问,结果是其他进程必须再次回去睡眠.)

wait_queue_t.flags
    Thus, there are two kinds of sleeping processes: exclusive processes (denoted by the value 1 in the flags field of the corresponding wait queue element) are selectively woken up by the kernel, while nonexclusive processes (denoted by the value 0 in the flags field) are always woken up by the kernel when the event occurs.
    因此,有两种睡眠进程: 互斥进程(等待队列元素的flag字段为1)由内核有选择地唤醒,而非互斥进程(flag值为0)总是由内核在事件发生时唤醒。

wait_queue_t.func
    A process waiting for a resource that can be granted to just one process at a time is a typical exclusive process. Processes waiting for an event that may concern any of them are nonexclusive. Consider, for instance, a group of processes that are waiting for the termination of a group of disk block transfers: as soon as the transfers complete, all waiting processes must be woken up. As we'll see next, the func field of a wait queue element is used to specify how the processes sleeping in the wait queue should be woken up.
                        
                       
                               task_struct             task_struct
                               |---------|<--+         |---------|<--+
                               |         |   |         |         |   |
                               |         |   |         |         |   |
                               |         |   |         |         |   |
                               |---------|   |         |---------|   |
                                             |                       |
                           wait_queue_t      |     wait_queue_t      |
                         |---------------|   |   |---------------|   |
 wait_queue_head_t       |    flags      |   |   |    flags      |   |
 |---------------|       |    *task      |---+   |    *task      |---+
 |     lock      |       |    func       |       |    func       |
 |---------------|       |---------------|       |---------------|
 |list_head *next|------>|list_head *next|------>|list_head *next|
 |list_head *prev|<------|list_head *prev|<------|list_head *prev|
 |---------------|       |---------------|       |---------------|




Handling wait queues
------------------------------------
DECLARE_WAITQUEUE()
init_waitqueue_head()
    A new wait queue head may be defined by using the DECLARE_WAIT_QUEUE_HEAD(name) macro, which statically declares a new wait queue head variable called name and initializes its lock and task_list fields. The init_waitqueue_head( ) function may be used to initialize a wait queue head variable that was allocated dynamically.
    可以用DECLARE_WAIT_QUEUE_HEAD(name)宏定义一个新的等待队列,该宏静态地声明和初始化名为name的等待队列头变量。 init_waitqueue_head()函数用于初始化已动态分配的wait queue head变量

    Wait queues are created statically via DECLARE_WAITQUEUE() or dynamically via init_waitqueue_head(). Processes put themselves on a wait queue and mark themselves not runnable.
    等待队列可以通过 DECLARE_WAITQUEUE()静态创建,也可以用 init_waitqueue_head()动态创建。进程把自己放入等待队列中并设置成不可执行状态。

    The init_waitqueue_entry(q, p) function initializes a wait_queue_t structure q as follows:

    q->flags = 0;
    q->task = p;
    q->func = default_wake_function;

add_wait_queue( )
add_wait_queue_exclusive( )
    The add_wait_queue( ) function inserts a nonexclusive process in the first position of a wait queue list.
    add_wait_queue()函数把一个非互斥进程插入等待队列链表的第一个位置

    The add_wait_queue_exclusive( ) function inserts an exclusive process in the last position of a wait queue list.
    add_wait_queue_exclusive( )函数把一个互斥进程插入等待队列链表的最后一个位置

try_to_wake_up( )
    The nonexclusive process p will be awakened by default_wake_function( ), which is a simple wrapper for the try_to_wake_up( ) function

remove_wait_queue( )
    The remove_wait_queue( ) function removes a process from a wait queue list.
    remove_wait_queue( )函数从等待队列链表中删除一个进程




The waitqueue_active( ) function checks whether a given wait queue list is empty.
waitqueue_active( )函数检查一个给定的等待队列是否为空


 
          



A process wishing to wait for a specific condition can invoke any of the functions shown in the following list.
希望等待一个特定事件的进程能调用下列函数中的任一个:

    * The sleep_on( ) function operates on the current process:

    void sleep_on(wait_queue_head_t *wq)
    {
        wait_queue_t wait;
        init_waitqueue_entry(&wait, current);
        current->state = TASK_UNINTERRUPTIBLE;
        add_wait_queue(wq,&wait); /*  wq points to the wait queue head  */
        schedule( );
        remove_wait_queue(wq, &wait);
    }
The function sets the state of the current process to TASK_UNINTERRUPTIBLE and inserts it into the specified wait queue. Then it invokes the scheduler, which resumes the execution of another process. When the sleeping process is awakened, the scheduler resumes execution of the sleep_on( ) function, which removes the process from the wait queue.
该函数把当前进程的状态设置为TASK_UNINTERRUPTIBLE,并把它插入到特定的等待队列。然后,它调用调度程序,而调度程序重新开始另一个进程的执行。当睡眠进程被唤醒时,调度程序重新开始执行sleep_on()函数,把该进程队列中删除。

    * The interruptible_sleep_on( ) function is identical to sleep_on( ), except that it sets the state of the current process to TASK_INTERRUPTIBLE instead of setting it to TASK_UNINTERRUPTIBLE, so that the process also can be woken up by receiving a signal.
    interruptible_sleep_on()与sleep_on()函数基本上是一样的,但是interruptible_sleep_on()把当前进程的状态设置为TASK_INTERRUPTIBLE而不是TASK_UNINTERRUPTIBLE,因此,接受一个信号就可以唤醒当前进程
   
   







      The kernel awakens processes in the wait queues, putting them in the TASK_RUNNING state, by means of one of the following macros:
 
      wake_up,
      wake_up_nr,
      wake_up_all,
      wake_up_interruptible,
      wake_up_interruptible_nr,
      wake_up_interruptible_all,
      wake_up_interruptible_sync,
      wake_up_locked.
     
      One can understand what each of these nine macros does from its name:

    *   All macros take into consideration sleeping processes in the TASK_INTERRUPTIBLE state; if the macro name does not include the string "interruptible," sleeping processes in the TASK_UNINTERRUPTIBLE state also are considered.
        所有宏都考虑到了处于TASK_INTERRUPTIBLE状态的睡眠进程;如果宏的名字中不含字符串"interruptible",则还将考虑处于TASK_UNINTERRUPTIBLE状态的睡眠进程
    *   All macros wake all nonexclusive processes having the required state
    *   The macros whose name include the string "nr" wake a given number of exclusive processes having the required state; this number is a parameter of the macro. The macros whose names include the string "all" wake all exclusive processes having the required state. Finally, the macros whose names don't include "nr" or "all" wake exactly one exclusive process that has the required state.
        名字中含有“nr”字符串的宏唤醒给定数字的具有所需状态的互斥进程;名字中含有"all"字符串的宏唤醒具有所需状态的所有互斥进程。最后,名字中不含"nr"或"all"字符串的宏只唤醒具有所需状态一个互斥进程
       
    *   The macros whose names don't include the string "sync" check whether the priority of any of the woken processes is higher than that of the processes currently running in the systems and invoke schedule( ) if necessary. These checks are not made by the macro whose name includes the string "sync"; as a result, execution of a high priority process might be slightly delayed.
    *   The wake_up_locked macro is similar to wake_up, except that it is called when the spin lock in wait_queue_head_t is already held.
 


Linux等待队列 多路IO复用select采用等待队列机制让用户程序没有资源读/写时睡眠,有资源读/写时唤醒用户程序。 等待队列以双向循环链表为基础数据结构,与进程调度紧密结合,用于实现内核的异步事件通知机制,也可用于同步对系统资源的访问。 1. 数据结构 (1) 每个等待队列都有一个等待队列头,该结构定义如下: struct __wait_queue_head { spinlock_t lock; 阅读详情

相关推荐

5060TI显卡深度学习环境搭建:CUDA12.8与CUDNN9.8.0的完美搭配

本文详细介绍了在Win10系统下为5060TI显卡搭建深度学习环境的最佳实践,重点配置CUDA12.8与CUDNN9.8.0的完美组合。通过优化OpenCV编译选项和YOLOv5环境设置,显著提升模型训练和推理效率,帮助开发者充分发挥5060TI显卡的性能潜力。

weixin_29065791的博客 500

_正点原子Linux第五十二章Linux阻塞和非阻塞IO实验

1)资料下载:点击资料即可下载2)对正点原子Linux感兴趣的同学可以加群讨论:9354467413)关注正点原子公众号,获取最新资料更新第五十二章Linux阻塞和非阻塞IO实验 阻塞和非阻塞IO是Linux驱动开发里面很常见的两种设备访问模式,在编写驱动的时候一定要考虑到阻塞和非阻塞。本章我们就来学习一下阻塞和非阻塞IO,以及如何在驱动程序中处理阻塞与非阻塞,如何在驱动程序使用等待队列和poll...

weixin_39732316的博客 364

Ultrascale/Ultrascale+ FPGA GTH IP及结构详解(一)

FPGA GTH Transceiver

houzi6320的博客 1万+

【等待队列】

【等待队列】

Sugar_wolf的博客 788

不用API Key也能玩转Obsidian AI:本地大模型离线配置全攻略

本文详细介绍了如何在无需API Key的情况下,通过本地部署大型语言模型(LLM)为Obsidian注入AI能力。文章以Ollama为例,提供了从环境准备、模型选择到与Obsidian插件(如Text Generator)集成的完整配置攻略,旨在帮助用户打造一个安全、离线可用的智能笔记工作流,实现数据隐私与AI助力的完美结合。

weixin_29062963的博客 831

51单片机-点亮LED和蜂鸣器

ULN2003是高耐压、大电流达林顿陈列,由七个硅NPN达林顿管组成。ULN2003是大电流驱动陈列,多用于单片机、只能仪表、PLC、数字量输出卡等控制电路中。可直接驱动蜂鸣器、继电器等负载。void delay(u16 i) // i等于1大约10uswhile(i--);//取反,高低电平不断变化,产生脉冲delay(10);// 周期200us。

三日沐水 1135

等待队列队列机制

等待队列机制等待队列队列操作添加等待队列项移除队列项唤醒 等待队列 等待队列是一种基于资源状态的线程管理的机制,它可以使用在资源不满足的情况下处于休眠状态,让出CPU资源,而资源状态满足时唤醒线程,使其继续进行业务粗粒的能力。等待队列用于使线程等待某一特定事件发生而无需繁琐的轮询,进程在等待期间休眠,在某事件发生时由内核自动唤醒。她是以双循环链表为基础数据结构,与进程的休眠唤醒机制紧密相连,是实现异步事件通知、跨进程通信、同步资源访问等技术的底层技术支撑。 定义在include\linux\wait.h头文

攻城狮 1569

抖音最新风控体系研究

小编从技术角度对抖音的风控史和落地目标,跟大家聊一聊抖音,包括海外版抖音TikTok的现状与生态。 1、非常非常早的时期,抖音刚出来的时候,没有任何封禁,这个阶段,导入一个开发库,加上一个请求头,咵咵咵的数据随便拿。 2、2018-2019年,抖音加入了设备id和xg机制,在绝大多数的接口中,携带了设备id参数,同时在请求头中加入了xg,如今的apk市场,能追溯到的最早xg是0200打头的,而小编见过的最早的xg是0000打头的。这个时期的接口,这些参数都还只是预设,没有实际的用处,设备随便写,

qq517348412的博客 1万+

linux等待队列wait queue

linux内核的等待队列是在内核中运用非常广泛的数据结构,它是以双循环链表为基础的数据结构,与进程的休眠---唤醒机制紧密相连,可以用来同步对系统资源的访问、异步事件通知、跨进程通信等。 假设进程A想要获取某资源(读网卡数据),但是此时资源没有准备好(网卡还未接收到数据),这时内核必须切换到其他进程进行,直到资源准备好再唤醒该进程。 1、等待队列头 struct __wait_queue_head { spinlock_t lock; //...

夏天Alex 5420

ESP32S3学习——i2c,点亮0.96寸oled

ESP32S3学习——i2c,点亮0.96寸oled

HandBAI的博客 1万+

Altium Designer(AD24)导入DDB库文件(Protel 99SE)方法

Altium Designer和Protel均为Altium公司开发的软件。本文介绍Altium Designer 24软件导入DDB库文件(Protel 99SE)方法。

fydar的博客 2004

linux 等待队列 PK linux 等待队列头

Linux内核的等待队列是以双循环链表为基础数据结构,与进程调度机制紧密结合,能够用于实现核心的异步事件通知机制。 在这个链表中,有两种数据结构:等待队列头(wait_queue_head_t)和等待队列项(wait_queue_t)。等待队列头和等待队列项中都包含一个list_head类型的域作为"连接件"。它通过一个双链表和把等待tast的头,和等待的进程列表链接起来。从上图可以清晰看到。所

wxie的Linux人生 2714

开篇词:学习系统分析师的好处?核心考点有哪些?

系统分析师备考指南 系统分析师是IT行业的高级专业人才,具备系统分析与设计能力,能指导开发团队工作。学习系统分析师不仅能获得职业晋升的"黄金跳板",还能全面提升技术架构思维和业务理解能力。 核心价值: 职业发展:晋升管理岗位的硬性要求,突破职业天花板 能力提升:构建T型知识结构,培养架构思维和软技能 市场竞争力:成为"业务+技术"桥梁,获得国家级专业认证 考试内容: 计算机基础知识(硬件、操作系统、网络等) 系统分析专业知识(软件工程、架构设计等) 进阶知识(企业信息

数据知道的博客 5953

等待队列浅析

一、等待队列的由来 Linux 内核的等待队列是以双循环链表为基础数据结构,与进程调度机制紧密结合,能够用于实现核心的异步事件通知机制。它有两种数据结构:等待队列头(wait_queue_head_t)和等待队列项(wait_queue_t)。 等待队列头和等待队列项中都包含一个 list_head 类型的域作为”连接件”。它通过一个双链表和把等待 task的头,和等待的进程列表链接起来。 等待队列头就是一个等待队列的头部,每个访问设备的进程都是一个队列项,当设备不可用的时候就要将这些进程对应的等待

wwwlyj123321的博客 1634

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

Linux 内核等待队列

目录 1、等待队列结构和API 1.1、等待队列头 1.1.1、定义等待队列头 1.1.2、初始化等待队列头 1.1.3、定义等待队元素 1.1.4、添加/移除等待队列 1.1.5、等待事件并睡眠 1.1.6、唤醒队列 2、等待队列(头)用法 2.1、 睡眠 2.1.1、方式一 2.1.2、方式二 2.2、 唤醒 3、等待队列相关源码分析 3.1、睡眠 3.2、唤...

StephenZhou 2086

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

内核等待队列的介绍

目录 一.背景和意义 二.数据结构分析 三.等待队列的操作 3.1 典型应用 3.2 将等待队列项插入等待队列 3.3 将等待队列从等待队列中删除 3.4 将等待队列从等待队列中删除 一.背景和意义 在实际编程中,我们会经常碰到这种场景:进程P需要等待条件C的成立,才能继续执行某个动作。例如,当串口没有数据可以读取时,我们可以通过轮询的方式,等到有数据来的时候,串口程序再去...

weixin_39059738的博客 539

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

等待队列使用

等待队列的相关api和简化编程

laugh哥的博客 885

linux查看等待队列,Linux 进程等待队列

Linux内核的等待队列是以双循环链表为基础数据结构,与进程调度机制紧密结合,能够用于实现核心的异步事件通知机制。在这个链表中,有两种数据结构:等待队列头(wait_queue_head_t)和等待队列项(wait_queue_t)。等待队列头和等待队列项中都包含一个list_head类型的域作为"连接件"。它通过一个双链表和把等待tast的头,和等待的进程列表链接起来。从上图可以清晰看到。所以我...

weixin_29204205的博客 1161

Linux 进程等待队列

Linux内核的等待队列是以双循环链表为基础数据结构,与进程调度机制紧密结合,能够用于实现核心的异步事件通知机制。在这个链表中,有两种数据结构:等待队列头(wait_queue_head_t)和等待队列项(wait_queue_t)。等待队列头和等待队列项中都包含一个list_head类型的域作为"连接件"。它通过一个双链表和把等待tast的头,和等待的进程列表链接起来。从上图可以清晰看到。所以我

张勤一 2572

linux等待队列

一.头文件 #include 二.结构体 1.等待队列头 struct __wait_queue_head { spinlock_t lock; struct list_head task_list; }; typedef struct __wait_queue_head wait_queue_head_t; 2.等待队列 struct __wait_queue {

paomadi的专栏 3517

Linux 等待队列

1. Linux内核中等待队列简介   Linux 内核等待队列可以用于许多用途,如中断、进程同步、以及定时。等待队列实现了在事件上的条件等待:希望等待特定事件的进程把自己放进合适的队列, 并且放弃控制权限。因此等待队列表示一组睡眠的进程,当某一事件为真时,有内核唤醒。 2. Linux 内核等待队列中重要数据结构 2.1 等待队列头 struct __wait_queue...

doj47065的博客 301

等待队列 wait_queue

等待队列

沐 白 713
上一篇: 交互式进程和批处理进程
下一篇: 更新时间片 -- scheduler_tick()
linux__kernel
博客等级 码龄20年 20粉丝 64原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值
⌂ 换网址