Linux内核create_singlethread_workqueue与create_workqueue学习

区别

使用create_singlethread_workqueue创建工作队列即使对于多CPU系统,内核也只负责在一个cpu上创建一个worker_thread内核线程;而使用create_workqueue创建工作队列对于多CPU系统,内核将会在每个CPU上创建一个worker_thread内核线程,使得线程处理的事务能够并行化.

 代码

worker_thread.c

#include <linux/module.h>
#include <linux/init.h>
#include <linux/string.h>
#include <linux/list.h>
#include <linux/sysfs.h>
#include <linux/ctype.h>
#include <linux/workqueue.h>
#include <linux/delay.h>

 //工作以队列结构组织成工作队列(workqueue),其数据结构为workqueue_struct,
static struct workqueue_struct *test_wq = NULL;   

//把推后执行的任务叫做工作(work),描述它的数据结构为work_struct
static struct work_struct   work;

/*
 *定义工作队列调用函数
 */
void work_func(struct work_struct *work){

        while(1) {
                
                printk(KERN_ERR "-----%s-----\n",__func__);  //printk可以在多处理器上同时被调用
        }
}


static int __init test_init(void){
    
        /*创建工作队列workqueue_struct,该函数会为cpu创建内核线程*/
        test_wq = create_singlethread_workqueue("test_wq");   
        
        /*初始化工作work_struct,指定工作函数*/
        INIT_WORK(&work,work_func);

        /*将工作加入到工作队列中,最终唤醒内核线程*/
        queue_work(test_wq, &work);

        while(1) {

                mdelay(1000);
                printk(KERN_ERR "-----%s-----\n",__func__);
        }

        return 0;
}


static void __exit test_exit(void){
    
}


module_init(test_init);
module_exit(test_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("zhao");

Makefile:

MODULE_NAME :=worker_thread
obj-m :=$(MODULE_NAME).o
 
KERNELDIR = /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
 
all:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules

clean:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) clean

项目附带学习实验,详细理论后面有时间补充总结!

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Linux 中,可以使用 `create_singlethread_workqueue()` 函数来创建一个单线程工作队列。 该函数的原型如下: ```c struct workqueue_struct *create_singlethread_workqueue(const char *name); ``` 其中,`name` 参数是工作队列的名称。该函数返回一个指向 `workqueue_struct` 结构体的指针,该结构体表示创建的工作队列。 下面是一个简单的示例,展示了如何使用 `create_singlethread_workqueue()` 函数创建一个单线程工作队列: ```c #include <linux/module.h> #include <linux/kernel.h> #include <linux/workqueue.h> static struct workqueue_struct *workqueue; static void work_handler(struct work_struct *work) { printk(KERN_INFO "Hello, world!\n"); } static DECLARE_WORK(work, work_handler); static int __init my_module_init(void) { workqueue = create_singlethread_workqueue("my_workqueue"); if (!workqueue) { printk(KERN_ERR "Failed to create workqueue\n"); return -ENOMEM; } schedule_work(&work); return 0; } static void __exit my_module_exit(void) { flush_work(&work); destroy_workqueue(workqueue); } module_init(my_module_init); module_exit(my_module_exit); MODULE_LICENSE("GPL"); ``` 在这个示例中,我们首先定义了一个工作处理函数 `work_handler`,它将打印一条消息。然后,我们使用 `DECLARE_WORK` 宏定义了一个工作项 `work`,并将其与 `work_handler` 函数关联起来。 在 `my_module_init` 函数中,我们调用 `create_singlethread_workqueue` 函数来创建一个名为 `my_workqueue` 的工作队列。然后,我们调用 `schedule_work` 函数将 `work` 工作项添加到工作队列中。 在 `my_module_exit` 函数中,我们使用 `flush_work` 函数等待工作项完成,并使用 `destroy_workqueue` 函数销毁工作队列。 需要注意的是,工作队列中的工作项是在内核线程中执行的,而不是在进程上下文中执行的。因此,在工作队列中执行的函数应该是内核级别的函数,而不是用户空间的函数。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值