目的: 檢驗學員對並行程式設計的認知
作答表單: 測驗 1 (針對 Linux 核心「設計」課程)
作答表單: 測驗 2 (針對 Linux 核心「實作」課程)
1
為了檢驗學員對〈建立相容於 POSIX Thread 的實作〉的認知,以下嘗試使用 GCC Built-in Functions for Memory Model Aware Atomic Operations 和 futex 來實作 multiple-producer/multiple-consumer (MPMC):
futex 全名為 fast user-space mutex locking,是 Linux 核心一種機制,主要提供使用者層級中有效與多執行緒的同步方式,並降低 Linux 核心的介入。可參考 Basics of Futexes。futex 主要有 wait 和 wake 等二個操作,其定義如下:
編譯方式:
參考執行輸出如下:
原始程式碼可見 mpmc.c (部分遮蔽)。請補完程式碼,使其運作符合預期。作答規範:
ZAAA
和 ZCCC
為整數ZBBB
為表示式,使用 lab0-c 的程式碼風格ZDDD
為巨集名稱(
和 )
)延伸問題:
提示: 加上編譯參數
-fsanitize=thread
2
以下程式碼嘗試用 POSIX Thread 重新實作 Linux 核心設計: RCU 同步機制,提供下列操作介面:
rcu_read_lock
rcu_read_unlock
synchronize_rcu
rcu_assign_pointer
rculist.h
是重寫 Linux 核心同名的標頭檔,搭配閱讀 A Tour Through RCU's Requirements:
RCU's grace-period guarantee allows updaters to wait for the completion of all pre-existing RCU read-side critical sections. An RCU read-side critical section begins with the marker
rcu_read_lock()
and ends with the markerrcu_read_unlock()
. These markers may be nested, and RCU treats a nested set as one big RCU read-side critical section.
__CHECKER__
的標示是針對 Sparse 這套程式碼靜態分析器。
程式碼可見: thread-rcu (部分遮蔽)
參考執行輸出:
請補完程式碼,使其運作符合預期。作答規範:
AAAA
為表示式,使用 lab0-c 的程式碼風格BBBB
為整數,用最精簡的形式書寫CCCC
為函式名稱,使用 GCC Built-in Functions for Memory Model Aware Atomic Operations延伸問題:
提示: 加上編譯參數
-fsanitize=thread
搭配閱讀 Userspace RCU