linux2021
目的: 檢驗學員對 Atomics 操作, Lock-Free Programming, 事件驅動伺服器:原理和實例, The Linux Kernel Module Programming Guide 的認知
1
給定 vpoll
核心模組可產生/合成 (synthesize) select / poll / epoll 的事件,一旦 vpoll
掛載進 Linux 核心後,將可藉由 ioctl
接受來自使用者層級的命令要求。以下是參考的測試程式碼: (檔名: user.c
)
參考執行輸出:
最後一行
GOT event
訊息不完整,留給學員推測
ZZZ
為 16 進位數值
vpoll
原始程式碼可參見: gist (部分程式碼隱蔽)
MMM
和 NNN
為數值,WWW
為 Linux 核心 API 函式名稱,後者可參見 memory-barriers.txt,以下摘錄:
wake_up_process()
always executes a general memory barrier. The barrier again occurs before the task state is accessed. In particular, if the wake_up() in the previous snippet were replaced by a call towake_up_process()
then one of
the two loads would be guaranteed to see 1.The available waker functions include:
complete()
;wake_up()
;wake_up_all()
;wake_up_bit()
;wake_up_interruptible()
;wake_up_interruptible_all()
;wake_up_interruptible_nr()
;wake_up_interruptible_poll()
;wake_up_interruptible_sync()
;wake_up_interruptible_sync_poll()
;wake_up_locked()
;wake_up_locked_poll()
;wake_up_nr()
;wake_up_poll()
;wake_up_process()
;In terms of memory ordering, these functions all provide the same guarantees of a
wake_up()
(or stronger).
搭配對照 include/linux/wait.h 和 include/uapi/linux/eventpoll.h
請補完程式碼,使得執行符合預期。
作答區
MMM = ?
NNN = ?
WWW = ?
ZZZ = ?
延伸問題:
vpoll
核心模組,實作效能評比的特別模式,從而分析 epoll
效能
2
考慮 lfring
是個 lock-free ring buffer 實作,並支援 multiple-producer/multiple-consumer (MPMC) 的情境。測試程式的參考輸出:
執行過程不會觸發任何 assert
失敗。
lfring
目前只支援 x86-64 架構,可在 Linux 和 macOS 執行,程式碼可見 gist (部分程式碼隱蔽)
請補完程式碼,使得執行符合預期。
延伸閱讀:
作答區
DDD = ?
KKK = ?
TTT = ?
HHH = ?
延伸問題:
git log include/linux/kfifo.h lib/kfifo.c
並觀察修改記錄
spin_unlock_irqrestore
的使用lfring
移植到 Linux 核心,並提供對應的測試及效能評比程式