linux 工作队列

理解工作队列 ,理解工作

 

调度的两种方式: 

1: 单独调度 work_struct  

  API:  schedule_work()

2:    调度执行一个workqueue_struct 里面的某个任务。

  API:     queue_work()

 

demo 如下: schedule_work

 1 #include <linux/kernel.h>
 2 #include <linux/module.h>
 3 #include <linux/slab.h>
 4 #include <linux/kobject.h>
 5 #include <linux/list.h>
 6 #include <linux/kthread.h>
 7 #include <asm/ptrace.h>
 8 #include <linux/sched.h>
 9 #include <linux/delay.h>
10 #include <linux/interrupt.h>
11 int data  = 10;
12 
13 static struct workqueue_struct *workqueue;
14 static struct work_struct       work1;
15 static struct work_struct       work2;
16 
17 
18 
19 static void do_work1(struct work_struct *arg)
20 {
21     printk(KERN_INFO "do_work1 .....\r\n");
22 }
23 
24 static void do_work2(struct work_struct *arg)
25 {
26     printk(KERN_INFO "do_work2 .....\r\n");
27 }
28 
29 
30 int threadfn(void *data)
31 {
32     static int count =  0 ;
33     int args = *(int *)data;
34     printk(KERN_INFO "enter thead_fn");
35     while(1)
36     {
37         msleep(2*1000);
38         printk(KERN_INFO "threadfn data: %d, count: %d\r\n",args , ++count);
39         schedule_work(&work1);
40         schedule_work(&work2);
41         
42     }
43 } 
44 
45 
46 static int __init test_kobj_init(void)
47 {
48 
49     
50    workqueue = create_workqueue("wanghb_queue");
51    INIT_WORK(&work1,do_work1);
52    INIT_WORK(&work2,do_work2);
53 
54    struct task_struct *  thread =  kthread_create( threadfn,(void * )&data,"mythread");
55    if(thread != NULL)
56    {
57        printk(KERN_INFO "thread create success\r\n");
58        wake_up_process(thread);
59        
60    }else
61    {
62        printk(KERN_ERR "thread create err\r\n");
63    }
64 
65     return 0;
66 }
67 
68 
69 
70 static void __exit test_kobj_exit(void)
71 {
72     printk(KERN_INFO "test_kobj_exit \r\n");
73     return;
74 }
75 
76 module_init(test_kobj_init);
77 module_exit(test_kobj_exit);
78 
79 MODULE_AUTHOR("LoyenWang");
80 MODULE_LICENSE("GPL");

log 如下: 

 

 

demo: queue_work

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/kobject.h>
#include <linux/list.h>
#include <linux/kthread.h>
#include <asm/ptrace.h>
#include <linux/sched.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
int data  = 10;

static struct workqueue_struct *workqueue;
static struct work_struct       work1;
static struct work_struct       work2;



static void do_work1(struct work_struct *arg)
{
    printk(KERN_INFO "do_work1 .....\r\n");
}

static void do_work2(struct work_struct *arg)
{
    printk(KERN_INFO "do_work2 .....\r\n");
}


int threadfn(void *data)
{
    static int count =  0 ;
    int args = *(int *)data;
    printk(KERN_INFO "enter thead_fn");
    while(1)
    {
        msleep(2*1000);
        printk(KERN_INFO "threadfn data: %d, count: %d\r\n",args , ++count);
        queue_work(workqueue,&work1);
        queue_work(workqueue,&work2);
        
    }
} 


static int __init test_kobj_init(void)
{

    
   workqueue = create_workqueue("wanghb_queue");
   INIT_WORK(&work1,do_work1);
   INIT_WORK(&work2,do_work2);

   struct task_struct *  thread =  kthread_create( threadfn,(void * )&data,"mythread");
   if(thread != NULL)
   {
       printk(KERN_INFO "thread create success\r\n");
       wake_up_process(thread);
       
   }else
   {
       printk(KERN_ERR "thread create err\r\n");
   }

    return 0;
}



static void __exit test_kobj_exit(void)
{
    printk(KERN_INFO "test_kobj_exit \r\n");
    destroy_workqueue(workqueue);
    return;
}

module_init(test_kobj_init);
module_exit(test_kobj_exit);

MODULE_AUTHOR("LoyenWang");
MODULE_LICENSE("GPL");

 

posted @ 2021-09-11 19:44  颜小雀  阅读(410)  评论(0编辑  收藏  举报