• 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 .....
    ");
    22 }
    23 
    24 static void do_work2(struct work_struct *arg)
    25 {
    26     printk(KERN_INFO "do_work2 .....
    ");
    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
    ",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
    ");
    58        wake_up_process(thread);
    59        
    60    }else
    61    {
    62        printk(KERN_ERR "thread create err
    ");
    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 
    ");
    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 .....
    ");
    }
    
    static void do_work2(struct work_struct *arg)
    {
        printk(KERN_INFO "do_work2 .....
    ");
    }
    
    
    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
    ",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
    ");
           wake_up_process(thread);
           
       }else
       {
           printk(KERN_ERR "thread create err
    ");
       }
    
        return 0;
    }
    
    
    
    static void __exit test_kobj_exit(void)
    {
        printk(KERN_INFO "test_kobj_exit 
    ");
        destroy_workqueue(workqueue);
        return;
    }
    
    module_init(test_kobj_init);
    module_exit(test_kobj_exit);
    
    MODULE_AUTHOR("LoyenWang");
    MODULE_LICENSE("GPL");

  • 相关阅读:
    [剑指Offer] 10.矩形覆盖
    [剑指Offer] 9.变态跳台阶
    [剑指Offer] 8.跳台阶
    [剑指Offer] 7.斐波那契数列
    ArtifactTransferException: Failure to transfer org.apache.openejb:javaee-api:jar:5.0-1
    -Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HO 解决办法
    java中判断list是否为空的用法
    PL/SQL快速选中一行并执行
    substring的用法
    Oracle---------sql 中取值两列中值最大的一列
  • 原文地址:https://www.cnblogs.com/coversky/p/15256587.html
Copyright © 2020-2023  润新知