1 - Provide two programming examples in which multithreading does not provide better performance than a single-threaded solution.
一種是以串行操作為主的程序,例如對數字圖像進行處理的各個步驟之間通常是無法並行的;
另一種是並行任務頻率很大,需要頻繁切換、同步、等待的程序,這樣的程序在切換、同步、等待上的開銷會帶來更大的性能損失。
2 - Under what circumstances does a multithreaded solution using multiple kernel threads provide better performance than a single-threaded solution on a single-processor system?
對於一些處理任務單元之間不存在數據相互關聯性的程序,如對數字圖像進行最小值濾波,每一個濾波器所用的數據都是獨立的N×N小塊,此時可以多線程並行化處理,效率高於串行處理。
另外,考慮一種線程阻塞(如缺頁錯誤或者系統調用)頻繁發生的狀況,單線程程序只能原地等待,而多線程程序的其他無關的線程可以繼續執行。
3 - Which of the following components of program state are shared across threads in a multithreaded process?
a. Register values
b. Heap memory
c. Global variables
d. Stack memory
堆內存和全局變量是各個線程之間共享的。每個線程有自己的寄存器值和棧。
4 - Can a multithreaded solution using multiple user-level threads achieve better performance on a multiprocessor system than on a single-processor system? Explain.
對於多處理器系統和單處理器系統,決定核心利用率的是內核線程數量而不是用戶級線程數量。以多對一模型為例,即使擁有再多的用戶級線程,也只能同時運用一個內核。因此,多用戶級線程在多處理器系統上並不能獲得比單處理器更強的性能。
5 - Is it possible to have concurrency but not parallelism? Explain.
並發而不並行是可能的。並行只有在多核、多處理器系統上才可能真正實現,特點是多個任務或者線程同時在執行;並發在單核、單處理器系統上也可以實現,例如使用時間切片技術,整體上多個任務或者線程是一起執行的,但在特定瞬間只有一個任務或者線程執行。
6 - Using Amdahl’s Law, calculate the speedup gain for the following applications:
• 40 percent parallel with (a) eight processing cores and (b) sixteen processing cores
• 67 percent parallel with (a) two processing cores and (b) four processing cores
• 90 percent parallel with (a) four processing cores and (b) eight processing cores
7 - Consider a multicore system and a multithreaded program written using the many-to-many threading model. Let the number of user-level threads in the program be greater than the number of processing cores in the system. Discuss the performance implications of the following scenarios.
a. The number of kernel threads allocated to the program is less than the number of processing cores.
b. The number of kernel threads allocated to the program is equal to the number of processing cores.
c. The number of kernel threads allocated to the program is greater than the number of processing cores but less than the number of user-level threads.
在多對多模型中,用戶級線程被映射到內核線程,內核線程被映射到處理器核心。
當內核線程數量少於處理器核心數時,某些處理器核心會空閑,不能最大化利用機能;當內核線程數量等於處理器核心數時,理論上沒有處理器核心空閑,可以最大化利用機能,除非內核線程因故發生阻塞(如系統調用);當內核線程數量大於處理器核心數時,內核線程需要輪流占用處理器核心,當某個內核線程阻塞時,可以交換為另一個內核線程占用處理器核心,可見這樣更可能最大化利用機能。