Linux create_workque()例程

Linux中的Workqueue机制就是为了简化内核线程的创建。通过调用workqueue的接口就能创建内核线程。

使用create_singlethread_workqueue创建工作队列即使对于多CPU系统,内核也只负责在一个cpu上创建一个worker_thread内核线程;而使用create_workqueue创建工作队列对于多CPU系统,内核将会在每个CPU上创建一个worker_thread内核线程,使得线程处理的事务能够并行化。
test.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可以在多处理器上同时被调用
                printk(KERN_INFO "-----%s-----\n",__func__);  
//        }
}
 
static int __init test_init(void){
	int i;
 
        /*创建工作队列workqueue_struct,该函数会为cpu创建内核线程*/
        test_wq = create_workqueue("test_wq");   
 
        /*初始化工作work_struct,指定工作函数*/
        INIT_WORK(&work,work_func);
 
        /*将工作加入到工作队列中,最终唤醒内核线程*/
        queue_work(test_wq, &work);
 
//        while(1){
//                mdelay();
//                printk(KERN_ERR "-----%s-----\n",__func__);
//        }
        for(i=0;i<3;i++)
        {
	printk(KERN_INFO "file --> %s function --> %s  line --> %d\n",__FILE__,__func__,__LINE__);
        }
        return 0;
}
 
static void __exit test_exit(void){
	
	printk(KERN_INFO "file --> %s function --> %s  line --> %d\n",__FILE__,__func__,__LINE__);
 
}

Linux 内核printk()信息打印等级

#define KERN_EMERG "<0>" /* system is unusable*/
#define KERN_ALERT "<1>" /* action must be taken immediately*/
#define KERN_CRIT "<2>" /* critical conditions*/
#define KERN_ERR "<3>" /* error conditions*/
#define KERN_WARNING "<4>" /* warning conditions*/
#define KERN_NOTICE "<5>" /* normal but significant condition*/
#define KERN_INFO "<6>" /* informational*/
#define KERN_DEBUG "<7>" /* debug-level messages*/

Makefile

CONFIG_MODULE_SIG=n

ifeq ($(KERNELRELEASE),)

ROOTS_DIR = /root/
#内核源码路径,不同环境可能会不一样,内核源码一定要先编译
KERNEL_DIR = /lib/modules/$(shell uname -r)/build
CUR_DIR = $(shell pwd)

all: 
	make -C $(KERNEL_DIR) M=$(CUR_DIR) modules
clean :
	make -C $(KERNEL_DIR) M=$(CUR_DIR) clean
install:
	insmod chr_drv.ko
uninstall:
	rmmod chr_drv
		
else
#用于指定到底编译的是哪个代码--hello.c
obj-m += test.o
#obj-m += math.o
endif

实际效果:

[qax@localhost test]$ dmesg -T | tail -6
[Tue Oct 13 23:44:09 2020] file --> /home/curits/Desktop/test/test.c function --> test_init  line --> 44
[Tue Oct 13 23:44:09 2020] file --> /home/curtis/Desktop/test/test.c function --> test_init  line --> 44
[Tue Oct 13 23:44:09 2020] file --> /home/curtis/Desktop/test/test.c function --> test_init  line --> 44
[Tue Oct 13 23:44:09 2020] file --> /home/curtis/Desktop/test/test.c function --> test_init  line --> 44
[Tue Oct 13 23:44:09 2020] -----work_func-----
[Tue Oct 13 23:44:16 2020] file --> /home/curtis/Desktop/test/test.c function --> test_exit  line --> 51

从实验结果中可以看出,是先执行的test_init里的printk()打印程序,后执行workqueue的程序

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值