--- tags: linux2023 --- # [2023q1](http://wiki.csie.ncku.edu.tw/linux/schedule) 第 15 週測驗題 :::info 目的: 檢驗學員對[並行和多執行緒程式設計](https://hackmd.io/@sysprog/concurrency)和《Demystifying the Linux CPU Scheduler》的認知 ::: ==[作答表單](https://docs.google.com/forms/d/e/1FAIpQLScoEjK-c9nFDcieMA3bKiu86Ktv9gMRUvJ9sA1tVBwrlWlCCw/viewform)== ### 測驗 `1` 考慮我們想要實作一個 lock-free 的 single-producer/single-consumer (SPSC) 並行程式,底層使用 [ring buffer](https://en.wikipedia.org/wiki/Circular_buffer),且避免 [false sharing](https://en.wikipedia.org/wiki/False_sharing)。程式碼可見: [main.c](https://gist.github.com/jserv/03adfccf69ffc756a7504f9caf7058e9) (部分遮蔽) 編譯方式: ```shell gcc -Wall -O2 -I. -o main main.c -lpthread ``` 測試程式的參考輸出: (其中 `4` 指定 4 個處理器核) ```shell $ ./main consumer 1: ---2---- producer 0: ---1---- consumer 2: ---4---- Consumer created... consumer 3: ---6---- Consumer created... Consumer created... producer 7 cycles/op consumer: 21 cycles/op consumer: 21 cycles/op consumer: 21 cycles/op Done! ``` 其中利用到針對多核處理器的 [spinlock: significant optimizations](https://en.wikipedia.org/wiki/Spinlock#Significant_optimizations) 技巧,對照 [cserv](https://github.com/sysprog21/cserv) 專案的 [src/util/spinlock.h](https://github.com/sysprog21/cserv/blob/master/src/util/spinlock.h)。 已知執行過程不會遇到任何 assert 錯誤,請補完程式碼,使其執行符合預期。作答規範: * `AAAA` 和 `BBBB` 皆為表示式,不該有括號 (及 `(` 與 `)`) * 以最精簡的形式作答
×
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
.