工作队列 例子

接着 上一篇  “工作队列 的变化”

写了个例子,代码如下:

 

#include <linux/workqueue.h> #include <linux/module.h> // kmalloc ... #include <linux/sysctl.h> int submit_work(void (*func)(struct work_struct *work), void* data); struct workqueue_struct* wq = NULL; typedef struct { void* data_for_work; struct work_struct my_work; } mydrv_t; void func(struct work_struct *work) { mydrv_t* mydrv = container_of(work, mydrv_t, my_work); printk("In func: %d received./n", (int)(mydrv->data_for_work)); } /* Driver initialization */ static int __init mydrv_init(void) { int data = 1; printk("Hello, small world!/n"); wq = create_singlethread_workqueue("g_queue"); submit_work(func, (void*) data); submit_work(func, (void*)(data + 1)); return 0; } /* Work submission. The first argument is the work function, and the second argument is the argument to the work function */ int submit_work(void (*func)(struct work_struct *work), void * data) { mydrv_t* test; test = (mydrv_t*)kmalloc(sizeof(mydrv_t), GFP_KERNEL); memset(test, 0, sizeof(mydrv_t)); /* Init the work structure */ INIT_WORK(&(test->my_work), func); test->data_for_work = data; /* Enqueue Work */ queue_work(wq, &(test->my_work)); return 0; } static void mydrv_exit(void) { printk("cruel world, /n bye ,my dear!/n"); destroy_workqueue(wq); } module_init(mydrv_init); module_exit(mydrv_exit); //EXPORT_SYMBOL_GPL(queue_work); MODULE_LICENSE("GPL");

 

编译执行后发现:

#ps -ef

...

root      5933     2  0 00:59 ?        00:00:00 [Josephdrv]   <------<<<< 这里这个线程在模块移除后,仍然不退出,不知道什么原因
root      7572     2  0 01:50 ?        00:00:00 [g_queue]
...

 

 

你可能感兴趣的:(工作,struct,function,Module,initialization,structure)