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。本文详细介绍了工作队列的概念、实现以及实际应用,并提供了多个示例代码。通过这些知识和示例,相信你能够在内核开发中更好地使用工作队列来管理延迟任务。

相关推荐
Dovis(誓平步青云)10 分钟前
几个方案来回选不定?做一个随时切换的候选推荐页
android·java·服务器·开发语言·javascript·数据库·智能化
一条破秋裤17 分钟前
Linux 线程同步:读写锁
java·linux·jvm
JieDavid39 分钟前
奇智创达知识产权管理系统期限监控模块实操,告别人工期限疏漏,实现管理闭环!
大数据·运维·人工智能·经验分享·重构
工作10年+,存储芯片行业1 小时前
Linux NVMe 中断排查与性能优化:IRQ 优化(原理+排查)
linux·运维·性能优化·ssd·nvme·pcie·中断
最强小杰1 小时前
claude-opus-5.5 API 接入教程:anthropic-version 头、tools 定义、流式事件格式三个常见问题全部梳理,收藏备用
java·服务器·网络·ai
宵时待雨1 小时前
linux笔记归纳25:多路转接epoll
linux·服务器·网络·c++
geats人山人海2 小时前
linux 1.目录结构
linux·运维·服务器
wuminyu2 小时前
JEP491中synchronized关键字引发的平台线程钉住解决方法简介
java·linux·c语言·jvm·c++
应用市场2 小时前
嵌入式Linux从裸板到产品(一):全景地图与交叉编译环境,从四个上板翻车现场说
linux·运维·服务器
CIMPro孪大师2 小时前
CIMPro×DPE | 不止是爆炸动画,AI原生数字样机如何让设备培训与运维更高效
运维·ai-native