Linux 工作队列(Workqueue):概念与实现

目录

在Linux内核中,工作队列(Workqueue)是一种用于延迟任务执行的机制。它允许内核代码在可中断上下文中执行任务,这对于需要在进程上下文中运行或者需要延迟执行的任务非常有用。在本文中,我们将详细介绍工作队列的概念、实现以及实际应用。

一、工作队列的概念

1.1 什么是工作队列

工作队列(Workqueue)是Linux内核提供的一种机制,用于将任务推迟到将来某个时间点在进程上下文中执行。与软中断和任务队列不同,工作队列允许任务在进程上下文中运行,这意味着它们可以睡眠和使用所有的内核API。

1.2 为什么使用工作队列

使用工作队列的主要原因是一些任务需要在中断上下文之外执行。中断处理程序通常需要尽可能快地完成,以避免延迟其他中断的处理。然而,有些任务需要较长的时间来完成,或者需要使用可能会睡眠的内核API。在这种情况下,可以使用工作队列将这些任务推迟到稍后在进程上下文中执行。

二、工作队列的实现

2.1 定义和初始化工作队列

在Linux内核中,工作队列由struct workqueue_struct表示,工作项由struct work_struct表示。你可以使用create_workqueue函数创建一个新的工作队列,并使用INIT_WORK宏初始化一个工作项。

c 复制代码
struct workqueue_struct *my_wq;
struct work_struct my_work;

void my_work_function(struct work_struct *work) {
    printk(KERN_INFO "Workqueue: Task executed\n");
}

static int __init my_module_init(void) {
    my_wq = create_workqueue("my_workqueue");
    if (my_wq) {
        INIT_WORK(&my_work, my_work_function);
        queue_work(my_wq, &my_work);
    }
    return 0;
}

static void __exit my_module_exit(void) {
    flush_workqueue(my_wq);
    destroy_workqueue(my_wq);
}

module_init(my_module_init);
module_exit(my_module_exit);
2.2 工作队列API

以下是一些常用的工作队列API:

  • create_workqueue:创建一个新的工作队列。
  • destroy_workqueue:销毁工作队列。
  • INIT_WORK:初始化一个工作项。
  • queue_work:将工作项添加到工作队列中。
  • flush_workqueue:等待工作队列中的所有工作项完成。
  • cancel_work_sync:取消一个工作项的执行。

三、工作队列的应用

3.1 延迟执行任务

工作队列非常适合需要延迟执行的任务。例如,在驱动程序中,可以使用工作队列来处理需要延迟执行的任务,如处理硬件中断或定期检查设备状态。

示例:延迟执行任务

c 复制代码
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/workqueue.h>
#include <linux/delay.h>

static struct workqueue_struct *my_wq;
static struct delayed_work my_delayed_work;

void my_delayed_work_function(struct work_struct *work) {
    printk(KERN_INFO "Delayed Workqueue: Task executed after delay\n");
}

static int __init my_module_init(void) {
    my_wq = create_workqueue("my_delayed_workqueue");
    if (my_wq) {
        INIT_DELAYED_WORK(&my_delayed_work, my_delayed_work_function);
        queue_delayed_work(my_wq, &my_delayed_work, msecs_to_jiffies(5000)); // 延迟5秒执行
    }
    return 0;
}

static void __exit my_module_exit(void) {
    cancel_delayed_work_sync(&my_delayed_work);
    destroy_workqueue(my_wq);
}

module_init(my_module_init);
module_exit(my_module_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("OpenAI GPT-4");
MODULE_DESCRIPTION("A simple example of using workqueue with delayed execution in Linux kernel.");
3.2 处理复杂的中断任务

在中断处理程序中,我们通常尽量减少执行时间,以免阻塞其他中断。对于需要较长时间处理的任务,可以使用工作队列来延迟执行。

示例:在中断处理程序中使用工作队列

c 复制代码
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/workqueue.h>

static struct workqueue_struct *my_wq;
static struct work_struct my_work;

irqreturn_t my_interrupt_handler(int irq, void *dev_id) {
    queue_work(my_wq, &my_work);
    return IRQ_HANDLED;
}

void my_work_function(struct work_struct *work) {
    printk(KERN_INFO "Workqueue: Handling interrupt task\n");
}

static int __init my_module_init(void) {
    int irq = 1; // 假设使用 IRQ 1
    my_wq = create_workqueue("my_interrupt_workqueue");
    if (my_wq) {
        INIT_WORK(&my_work, my_work_function);
        request_irq(irq, my_interrupt_handler, IRQF_SHARED, "my_interrupt_handler", NULL);
    }
    return 0;
}

static void __exit my_module_exit(void) {
    int irq = 1; // 假设使用 IRQ 1
    free_irq(irq, NULL);
    flush_workqueue(my_wq);
    destroy_workqueue(my_wq);
}

module_init(my_module_init);
module_exit(my_module_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("OpenAI GPT-4");
MODULE_DESCRIPTION("A simple example of using workqueue in interrupt handler in Linux kernel.");

四、工作队列的类型

Linux内核提供了两种类型的工作队列:

4.1 普通工作队列

普通工作队列使用默认的系统工作队列来执行任务。示例中的create_workqueue就是创建一个普通工作队列。

4.2 高优先级工作队列

在某些情况下,需要更高优先级的工作队列来执行关键任务。Linux内核提供了高优先级工作队列,可以通过alloc_workqueue函数创建。

示例:创建高优先级工作队列

c 复制代码
static struct workqueue_struct *my_highpri_wq;

static int __init my_module_init(void) {
    my_highpri_wq = alloc_workqueue("my_highpri_workqueue", WQ_HIGHPRI, 0);
    if (my_highpri_wq) {
        INIT_WORK(&my_work, my_work_function);
        queue_work(my_highpri_wq, &my_work);
    }
    return 0;
}

static void __exit my_module_exit(void) {
    flush_workqueue(my_highpri_wq);
    destroy_workqueue(my_highpri_wq);
}

五、总结

工作队列(Workqueue)是Linux内核中用于延迟执行任务的一种机制。它允许任务在进程上下文中运行,使得任务可以睡眠并使用所有的内核API。本文详细介绍了工作队列的概念、实现以及实际应用,并提供了多个示例代码。通过这些知识和示例,相信你能够在内核开发中更好地使用工作队列来管理延迟任务。

相关推荐
chlk12319 小时前
Linux文件权限完全图解:读懂 ls -l 和 chmod 755 背后的秘密
linux·操作系统
舒一笑19 小时前
Ubuntu系统安装CodeX出现问题
linux·后端
改一下配置文件20 小时前
Ubuntu24.04安装NVIDIA驱动完整指南(含Secure Boot解决方案)
linux
碳基沙盒21 小时前
OpenClaw 多 Agent 配置实战指南
运维
深紫色的三北六号1 天前
Linux 服务器磁盘扩容与目录迁移:rsync + bind mount 实现服务无感迁移(无需修改配置)
linux·扩容·服务迁移
SudosuBash1 天前
[CS:APP 3e] 关于对 第 12 章 读/写者的一点思考和题解 (作业 12.19,12.20,12.21)
linux·并发·操作系统(os)
哈基咪怎么可能是AI2 天前
为什么我就想要「线性历史 + Signed Commits」GitHub 却把我当猴耍 🤬🎙️
linux·github
十日十行3 天前
Linux和window共享文件夹
linux
Sinclair3 天前
简单几步,安卓手机秒变服务器,安装 CMS 程序
android·服务器
木心月转码ing3 天前
WSL+Cpp开发环境配置
linux