contributed by < fewletter >
給定的 kecho 已使用 CMWQ,請陳述其優勢和用法
Concurrency Managed Workqueue (cmwq)
從 CMWQ 文件中的段落可以先知道幾個名詞
When such an asynchronous execution context is needed, a work item describing which function to execute is put on a queue. An independent thread serves as the asynchronous execution context. The queue is called workqueue and the thread is called worker.
為何需要使用 CMWQ ?
CMWQ 的優勢
khttpd
以下實作參考 作業說明: CMWQ (Concurrency Managed Workqueue) ,首先在掛載模組時建立 CMWQ ,並且在卸載模組時結束 CMWQ 。
建立 CMWQ
結束 CMWQ
接著參考 kecho 當中建立 workqueue 的模式在 khttpd
重新建立一次。
khttpd_server
利用 list_head
指向 workqueue 的 work。khttpd
是為了將 socket
中的資訊傳給 worker
,讓 worker
能夠從指定的函式中執行任務,list_head
是讓所有的 worker 能夠以佇列的形式存在並且在釋放記憶體空間時能夠從頭到尾遍歷後釋放。daemon
,並且設定狀態為執行中 .is_stopped = false
。create_work()
中將所有的 work 串接在 &daemon.worker_head
後面,同時啟動 worker 要執行的函式。free_work()
將遍歷整個佇列並將記憶體釋放。修改 http_server_daemon()
,並以結構體 khttpd
的成員改寫,主要修改的地方有
khttpd_wq
中修改 http_server_worker()
,主要修改的地方有
container_of()
和結構體 khttpd
的關係找出此 work 下, worker 的記憶體位置改好程式碼後,利用 make check 來觀察測試結果
引入 CMWQ 之前
引入 CMWQ 之後
參考自 作業說明:實作 directory listing 功能,為了能夠在網頁中顯示檔案目錄,首先第一步是要先讀取檔案目錄並且需要了解將資料傳給網頁的語法。
filp_open
, filp_close
, iterate_dir
,跟結構體 struct dir_context
,而從 linux/fs.h
中的註解可以看到 struct dir_context
允許將目錄讀到核心。tracedir
的功能就在提供讀取檔案目錄到核心並且利用 html 的語法將目錄名稱傳傳到 client 中。
if (request->method != HTTP_GET)
filp_open
打開資料夾,並且透過 iterate_dir
遍歷整個資料夾目錄tracedir
會將資料夾目錄名稱透過 http_server_send
傳給網頁filp_close
關閉資料夾其所產生的 html 檔案如下,可以看到其與 handle_directory
中所傳遞的字串階相同
最後透過載入模組並且連線到網頁瀏覽器便可得下面結果