--- tags: linux2024 --- # [2024q1](http://wiki.csie.ncku.edu.tw/linux/schedule) 第 13 週測驗題 :::info 目的: 檢驗學員對 [Atomics 操作](https://hackmd.io/@sysprog/concurrency-atomics), [Ring buffer](https://hackmd.io/@sysprog/concurrency-ringbuffer), futex, mmap 的認知 ::: ==[作答表單: 測驗 1-2](https://docs.google.com/forms/d/e/1FAIpQLSdemKSCyhqZQ_AsQ54n4N0L8KWZxi0mbwgpZL5Qkm0O_PEnQw/viewform)== (針對 Linux 核心「設計/實作」課程) ### 測驗 `1` spms 是個使用 C11 撰寫的單一發布者多訂閱者 (Pub/Sub模型,參見 [Build a one-to-many Pub/Sub system](https://cloud.google.com/pubsub/docs/building-pubsub-messaging-system)) 環狀緩衝區,適用於共享記憶體。發布者在不知道訂閱者的情況下,將訊息寫入環狀緩衝區,當訂閱者讀取一條訊息時,該訊息不會從環狀緩衝區中移除,其他訂閱者仍然可讀取。若訂閱者的讀取速度不夠快,它將錯過一些訊息。 > [系統設計入門:Pub/Sub Pattern](https://medium.com/jimmy-wang/%E7%B3%BB%E7%B5%B1%E8%A8%AD%E8%A8%88%E5%85%A5%E9%96%80-pub-sub-pattern-ec391aca22aa)  測試方式: * 在一個終端機執行: `./test pub` 作為發布者 * 在另一個終端機執行: `./test sub` 作為訂閱者 [原始程式碼](https://gist.github.com/jserv/250d7e601e652cdbfce25304a75092d6) 編譯方式 ```shell $ gcc -Wall -O2 -o test test.c spms.c -lrt -lpthread ``` 參考輸出: ``` Msg: This is a key message Msg: Msg: Thu May 16 18:49:11 2024 Msg: Msg: Thu May 16 18:49:12 2024 Msg: Msg: Thu May 16 18:49:13 2024 Msg: Msg: Thu May 16 18:49:14 2024 Msg: Msg: Thu May 16 18:49:15 2024 Msg: Msg: Thu May 16 18:49:20 2024 Msg: Msg: Thu May 16 18:49:21 2024 Msg: Msg: Thu May 16 18:49:22 2024 ``` 目前的 ring buffer 是 blocking,意味著當緩衝區是空的時候,讀取端會等到有資料為止。 作答規範: * AAAA, BBBB 是 FUTEX 的操作名稱,搭配 [並行程式設計: 實作輕量級的 Mutex Lock](https://hackmd.io/@sysprog/concurrency-mutex) * CCCC, DDDD 是表示式
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.