日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > linux >内容正文

linux

工作队列 order linux,linux 工作队列(workqueue)

發(fā)布時間:2023/12/10 linux 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 工作队列 order linux,linux 工作队列(workqueue) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在處理內(nèi)核相關(guān)工作中, 我們經(jīng)常看到工作隊(duì)列(workqueue)的身影. 本文描述何為 linux workqueue.

本文基于 2.6.32 的內(nèi)核, 此時的工作隊(duì)列還不是 cmwq.

為什么使用 workqueue

在內(nèi)核代碼中, 經(jīng)常希望延緩部分工作到將來某個時間執(zhí)行, 這樣做的原因很多, 比如

在持有鎖時做大量(或者說費(fèi)時的)工作不合適.

希望將工作聚集以獲取批處理的性能.

調(diào)用了一個可能導(dǎo)致睡眠的函數(shù)使得在此時執(zhí)行新調(diào)度非常不合適.

...

內(nèi)核中提供了許多機(jī)制來提供延遲執(zhí)行, 使用最多則是 workqueue.

如中斷的下半部處理可延遲中斷上下文中的部分工作;

定時器可指定延遲一定時間后執(zhí)行某工作;

workqueue 則允許在進(jìn)程上下文環(huán)境下延遲執(zhí)行;

內(nèi)核中曾短暫出現(xiàn)過的慢工作機(jī)制 (slow work mechanism);

異步函數(shù)調(diào)用(asynchronous function calls);

各種私有實(shí)現(xiàn)的線程池;

...

術(shù)語

workqueue: 所有工作項(xiàng)(需要被執(zhí)行的工作)被排列于該隊(duì)列.

worker thread: 是一個用于執(zhí)行 workqueue 中各個工作項(xiàng)的內(nèi)核線程, 當(dāng) workqueue 中沒有工作項(xiàng)時, 該線程將變?yōu)?idle 狀態(tài).

single threaded(ST): worker thread 的表現(xiàn)形式之一, 在系統(tǒng)范圍內(nèi), 只有一個 worker thread 為 workqueue 服務(wù).

multi threaded(MT): worker thread 的表現(xiàn)形式之一, 在多 CPU 系統(tǒng)上每個 CPU 上都有一個 worker thread 為 workqueue 服務(wù).

使用步驟

創(chuàng)建 workqueue(如果使用內(nèi)核默認(rèn)的 workqueue, 此步驟略過).

創(chuàng)建工作項(xiàng) work_struct.

向 workqueue 提交工作項(xiàng).

工作項(xiàng)

數(shù)據(jù)結(jié)構(gòu)(略有調(diào)整):

struct work_struct {

atomic_long_t data;

struct list_head entry;

work_func_t func;

struct lockdep_map lockdep_map;

};

struct delayed_work {

struct work_struct work;

struct timer_list timer;

};

靜態(tài)地創(chuàng)建工作項(xiàng):

DECLARE_WORK(n, f)

DECLARE_DELAYED_WORK(n, f)

動態(tài)地創(chuàng)建工作項(xiàng):

INIT_WORK(struct work_struct work, work_func_t func);

PREPARE_WORK(struct work_struct work, work_func_t func);

INIT_DELAYED_WORK(struct delayed_work work, work_func_t func);

PREPARE_DELAYED_WORK(struct delayed_work work, work_func_t func);

內(nèi)核默認(rèn)的 workqueue

查閱源碼可知, 內(nèi)核默認(rèn)的全局 workqueue 應(yīng)為:

// 定義

static struct workqueue_struct *keventd_wq __read_mostly;

// 初始化

...

keventd_wq = create_workqueue("events"); // MT worker thread 模式.

...

// 確認(rèn)對應(yīng)的內(nèi)核線程數(shù)目, 應(yīng)等于 CPU 核數(shù)

$ lscpu

Architecture: x86_64

CPU(s): 3

Thread(s) per core: 1

$ ps aux | grep "events"

root 9 0.0 0.0 0 0 ? S Feb09 1:15 [events/0]

root 10 0.0 0.0 0 0 ? S Feb09 0:59 [events/1]

root 11 0.0 0.0 0 0 ? S Feb09 0:59 [events/2]

工作項(xiàng)加入 keventd_wq 由下面兩個函數(shù)之一完成:

int schedule_work(struct work_struct *work)

{

return queue_work(keventd_wq, work);

}

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

{

return queue_delayed_work(keventd_wq, dwork, delay);

}

用戶自定義的 workqueue

創(chuàng)建 workqueue:

create_singlethread_workqueue(name) // 僅對應(yīng)一個內(nèi)核線程

create_workqueue(name) // 對應(yīng)多個內(nèi)核線程, 同上文.

向 workqueue 中提交工作項(xiàng):

int queue_work(workqueue_t *queue, work_t *work);

int queue_delayed_work(workqueue_t *queue, work_t *work, unsigned long delay);

取消 workqueue 中掛起的工作項(xiàng)以及釋放 workqueue 此處略過.

工作隊(duì)列的優(yōu)點(diǎn)

使用簡單.

執(zhí)行在進(jìn)程上下文, 從而可以睡眠, 被調(diào)度和搶占.

在多核環(huán)境下使用也非常友好.

example

#include

#include

#include

static struct workqueue_struct *queue = NULL;

static struct work_struct work;

static void work_handler(struct work_struct *data)

{

printk(KERN_ALERT "work handler for work_item in queue helloworkqueue\n");

// workqueue 中的每個工作完成之后就被移除 workqueue.

// 下面的語句會造成"死機(jī)", 原因可能是該 workqueue 占據(jù)了所有的 CPU 時間.

// queue_work(queue, &work);

}

static int __init test_init(void)

{

queue = create_singlethread_workqueue("helloworkqueue");

if (!queue)

{

goto err;

}

INIT_WORK(&work, work_handler);

queue_work(queue, &work);

return 0;

err:

return -1;

}

static void __exit test_exit(void)

{

destroy_workqueue(queue);

}

MODULE_LICENSE("GPL");

module_init(test_init);

module_exit(test_exit);

brought to you by .

總結(jié)

以上是生活随笔為你收集整理的工作队列 order linux,linux 工作队列(workqueue)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。