close

Work Queue

run in process context


Create a Work Queue Interface

#include <linux/workqueue.h>


Work Queue Management

1.Guarantee the queued work completed before continuing.
執行完再離開
2.Wait on all work pending for the worker
thread
確保work execute over





step 1:#vi work_queue.c

#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/module.h>

#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
// To do: include workqueue.h
  #include <linux/workqueue.h>

#else
// To do: include tqueue.h
  #include <tqueue.h>

#endif

#include <linux/interrupt.h>

struct my_data{
        int len;
        char *buf;
        unsigned long jiffies;
#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,20)
        struct work_struct mytq_task;
#endif
} mytq_data;

#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,20)
void mytq_fun (struct work_struct *work)
#else
void mytq_fun (void *ptr)
#endif
{
  // To do: implement your defered codes

    struct my_data *data=container_of(work,struct my_data,mytq_task);
       
    if(in_interrupt())
             printk("in interrupt context\n");
        else
                printk("in process context\n");
        
        printk("in mytq_fun,jiffies=%ld,\ len=%d\n",jiffies,data->len);
        printk("the mwssage is :%s\n",data->buf);

}

int init_module(void)
{
        int len = 100;
        char *buf = "Hello from mytq_fun";
        INIT_WORK(&mytq_data.mytq_task,mytq_fun);
        mytq_data.len = len ;
        mytq_data.buf = buf ;
        mytq_data.jiffies = jiffies ;
        //schedule_work(&mytq_data.mytq_task,mytq_);      
          printk ("queued task at jiffies = %ld\n", jiffies );

#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,20)
        // Initialize a work queue
#elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0)
        // To do: schedule a work
        schedule work(&mytq data.mytq task);
#else
        // To do : queue a task
#endif

  return 0;
}

void cleanup_module(void)
{
        printk ("I cleaned up, jiffies = %ld\n", jiffies );
}


step 2:#make

step 3:#insmod ./work_queue.ko

step 4:tail /var/log/kern.log

                                          
arrow
arrow
    文章標籤
    Linux device drive
    全站熱搜
    創作者介紹
    創作者 布拉怡 的頭像
    布拉怡

    nini的部落格

    布拉怡 發表在 痞客邦 留言(1) 人氣()