1. 程式人生 > >Linux中的工作佇列(work queue)

Linux中的工作佇列(work queue)

工作佇列(work queue)是Linux kernel中將工作推後執行的一種機制。這種機制和BH(bottom half)或Tasklets不同之處在於工作佇列是把推後的工作交由一個核心執行緒去執行,因此工作佇列的優勢就在於它允許重新排程甚至睡眠。

linux 2.6.20以後,工作佇列機制和之前的版本有一點不同,在網上找了一點資料,也相應的看了一些code,現在自己總結一下:

原文參考:

http://wiki.365linux.cn/index.php?doc-view-39

http://blog.csdn.net/lanmanck/archive/2009/11/05/4770030.aspx

work queue的標頭檔案: /kernel/include/linux/workqueue.h

work queue的資料結構:

struct work_struct {
    atomic_long_t data;
#define WORK_STRUCT_PENDING 0                  /* T if work item pending execution */
#define WORK_STRUCT_FLAG_MASK                 (3UL)
#define WORK_STRUCT_WQ_DATA_MASK         (~WORK_STRUCT_FLAG_MASK)
    struct list_head entry;
    work_func_t func;
#ifdef CONFIG_LOCKDEP
    struct lockdep_map lockdep_map;
#endif
};

其中 work_func_t 的定義如下:

typedef void (*work_func_t)(struct work_struct *work);

work queue 主要的 API:

INIT_WORK(struct work_struct *work, work_func_t func)

INIT_DELAYED_WORK(struct delayed_work *work, work_func_t func)

int schedule_work(struct work_struct *work)

void flush_scheduled_work(void)

int schedule_delayed_work(struct delayed_work *work, unsigned long delay)

int cancel_delayed_work(struct delayed_work *work)

struct workqueue_struct *create_workqueue(const char *name)

int queue_work(struct workqueue_struct *wq, struct work_struct *work)

int queue_delayed_work(struct workqueue_struct *wq, struct delayed_work *work, unsigned long delay)

void flush_workqueue(struct workqueue_struct *wq)

void destroy_workqueue(struct workqueue_struct *wq)

work queue 的使用例項:

struct my_work_t {

    char *name;

    struct work_struct work;                 //作為自己資料結構的成員,然後用container_of獲得my_work_t的指標

};


void my_func(struct work_struct *work)

{

    struct my_work_t *my_work = container_of (work, struct my_work_t, work);

    printk(KERN_INFO “Hello world, my name is %s!/n”, my_work->name);

}


struct workqueue_struct *my_wq = create_workqueue(“my wq”);      //建立自己的work queue

struct my_work_t my_work;


my_work.name = “Jack”;


INIT_WORK(&(my_work.work), my_func);                //初始化自己的work queue

queue_work(my_wq, &(my_work.work));

destroy_workqueue(my_wq);                     //銷燬自己的work queue