我想使用create_workqueue()创建一个新的工作队列
我正在读的一本关于内核开发的书的作者说:“这个函数创建所有的工作线程(系统中的每个处理器一个),并为它们处理工作做好准备。”
我的代码显示在这个问题的末尾,创建了一个内核,并在两个队列上调度工作。一个是默认工作队列,另一个使用自定义工作队列。它们应该由不同的工作线程来处理。
但是,我在结果(如下所示)中看到,这两个进程都由同一个进程(PID 42501)处理,该进程是我的虚拟机中的线程kworker2。
运行结果:


#include <linux/workqueue.h>
#include "kn_common.h"
#include <linux/sched.h>
MODULE_LICENSE("Dual BSD/GPL");
static void my_work_func(struct work_struct *work){
long ID;
printk(KERN_ALERT"=============\n");
print_current_time(0);
ID = current->pid;
printk(KERN_ALERT"my workqueue function is called!.... pid = %ld\n", ID);
printk(KERN_ALERT"=============\n");
}
static void my_work_custom_func(struct work_struct *work){
long ID;
printk(KERN_ALERT"=============\n");
print_current_time(0);
ID = current->pid;
printk(KERN_ALERT"my customize workqueue is called!... pid = %ld\n", ID);
printk(KERN_ALERT"=============\n");
}
DECLARE_WORK(mywork, my_work_func);
static int testworkqueue_init(void){
struct workqueue_struct *myworkqueue = create_workqueue("myworkqueue");
// init a work_struct dynamically use pointer
struct work_struct *mywork2;
mywork2 = kmalloc(sizeof(struct work_struct), GFP_KERNEL);
INIT_WORK(mywork2, my_work_custom_func);
flush_scheduled_work();
// schedule work
schedule_work(&mywork);
// flush customized workqueue
flush_workqueue(myworkqueue);
queue_work(myworkqueue, mywork2);
return 0;
}
static void testworkqueue_exit(void){
printk(KERN_ALERT"*************\n");
print_current_time(0);
printk(KERN_ALERT"testworkqueu exit\n");
printk(KERN_ALERT"*************\n");
}
module_init(testworkqueue_init);
module_exit(testworkqueue_exit);和Makefile
obj-m += myworkqueue.o
myworkqueue-objs := testworkqueue.o kn_common.o
CURRENT_PATH := $(shell pwd)
LINUX_KERNEL := $(shell uname -r)
# you may change this to your own kernel src path
LINUX_KERNEL_PATH := /lib/modules/$(LINUX_KERNEL)/build
all:
make -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules
rm -rf modules.order Module.symvers .*.cmd *.o *.mod.c .tmp_versions *.unsigned
clean:
rm -rf modules.order Module.symvers .*.cmd *.o *.mod.c *.ko .tmp_versions *.unsigned发布于 2017-01-18 17:02:22
最初实现的多线程(MT)工作队列浪费了大量的资源,所提供的并发水平并不令人满意。
引入了新的设计来获得高级别的并发性。函数"create_*workqueue()“已弃用并计划删除。
有关工作队列的最新实现,请阅读this。
https://stackoverflow.com/questions/41710901
复制相似问题