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.
執行完再離開
1.Guarantee the queued work completed before continuing.
執行完再離開
2.Wait on all work pending for the worker
thread
thread
確保work execute over
![](https://imageproxy.pixnet.cc/imgproxy?url=https://pic.pimg.tw/sheratea/1453605863-850191753_n.png)
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
文章標籤
全站熱搜