4.1 Provide three programming examples in which multithreading provides better performance than a single-threaded solution.
a. 一個為每個請求創建獨立執行緒的網頁伺服器。。
b. 一個平行化的應用程式,例如矩陣相乘,其中矩陣的不同部分可以同時進行計算。。
c. 一個互動式圖形介面程式,例如除錯器,其中一個執行緒用於監控使用者輸入,另一個執行緒代表正在執行的應用程式,第三個執行緒負責監控效能。
4.2 Using Amdahl's Law, calculate the speedup gain of an application that has a 60 percent parallel component for (a) two processing cores and (b) four processing cores.
a. 使用兩個處理核心,我們獲得了1.42倍的加速。
b. 使用四個處理核心,我們獲得了1.82倍的加速。
4.3 Does the multithreaded web server described in Section 4.1 exhibit task or data parallelism?
資料平行處理。每個執行緒執行相同的任務,但處理不同的資料。
4.4 What are two differences between user-level threads and kernel-level threads? Under what circumstances is one type better than the other?
a. 使用者層級執行緒對核心是不可見的,而核心層級執行緒則是核心所知的。
b. 在使用多對一或多對多模型映射的系統中,使用者執行緒由執行緒函式庫調度,而核心執行緒由核心調度。
c. 核心執行緒不必與某個行程(process)相關聯,而每個使用者執行緒都屬於某個行程。核心執行緒通常比使用者執行緒維護成本更高,因為它們必須使用核心資料結構來表示。
4.5 Describe the actions taken by a kernel to context-switch between kernel-level threads.
在核心執行緒之間進行上下文切換通常需要保存被切換出的執行緒的 CPU 暫存器值,並恢復被調度的新執行緒的 CPU 暫存器值。
4.6 What resources are used when a thread is created? How do they differ from those used when a process is created?
由於執行緒比行程更小,創建執行緒通常比創建行程使用更少的資源。創建一個行程需要分配一個行程控制表(PCB),這是一個相當大的資料結構。PCB 包括記憶體映射、開啟的檔案清單和環境變數。其中,分配和管理記憶體映射通常是最耗時的操作。創建使用者執行緒或核心執行緒只需分配一個小型資料結構,用於保存暫存器集合、堆疊和優先級。
4.7 Assume that an operating system maps user-level threads to the kernel using the many-to-many model and that the mapping is done through LWPs. Furthermore, the system allows developers to create real-time threads for use in real-time systems. Is it necessary to bind a real-time thread to an LWP? Explain.
是的,計時對即時應用程式至關重要。如果一個執行緒被標記為即時執行緒但未綁定到輕量級行程(LWP),該執行緒可能需要等待被附加到 LWP 後才能運行。考慮以下情況:一個即時執行緒正在運行(已附加到 LWP),然後因執行 I/O、高優先級即時執行緒搶佔、等待互斥鎖等原因阻塞。在即時執行緒阻塞期間,其附加的 LWP 被分配給其他執行緒。當該即時執行緒再次被調度運行時,必須先等待重新附加到 LWP。如果將 LWP 綁定到即時執行緒,可以確保該執行緒在被調度後能以最小的延遲立即運行。
4.8 Under what circumstances does a multithreaded solution using multiple kernel threads provide better performance than a single-threaded solution on a single-processor system?
當核心執行緒發生頁面錯誤時,可以切換到另一個核心執行緒,利用交替的時間進行有意義的工作。然而,單執行緒進程在發生頁面錯誤時無法執行有意義的工作。因此,在程式可能經常發生頁面錯誤或需要等待其他系統事件的情況下,即使在單處理器系統上,多執行緒解決方案也能表現得更好。
4.9 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.10 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
• (a) 1.53 (b) 1.60
• (a) 1.19 (b) 1.32
• (a) 3.07 (b) 4.70
4.11 Determine if the following problems exhibit task or data parallelism:
• Using a separate thread to generate a thumbnail for each photo in a collection
• Transposing a matrix in parallel
• A networked application where one thread reads from the network and another writes to the network
• The fork-join array summation application described in Section 4.5.2
• The Grand Central Dispatch system
• Data parallelism
• Data parallelism
• Task parallelism
• Data parallelism
• Task parallelism
4.12 Consider the following code segment:
pid t pid;
pid = fork();
if (pid == 0) { /* child process */
fork();
thread create( . . .);
}
fork();
a. How many unique processes are created?
b. How many unique threads are created?
有六個行程和兩個執行緒。
4.13 The program shown in Figure 4.23 uses the Pthreads API. What would be the output from the program at LINE C and LINE P?
#include <pthread.h>
#include <stdio.h>
int value = 0;
void *runner(void *param); /* the thread */
int main(int argc, char *argv[])
{
pid t pid;
pthread t tid;
pthread attr t attr;
pid = fork();
if (pid == 0) { /* child process */
pthread attr init(&attr);
pthread create(&tid,&attr,runner,NULL);
pthread join(tid,NULL);
printf("CHILD: value = %d",value); /* LINE C */
}
else if (pid > 0) { /* parent process */
wait(NULL);
printf("PARENT: value = %d",value); /* LINE P */
}
}
void *runner(void *param) {
value = 5;
pthread exit(0);
}
Output at LINE C is 5. Output at LINE P is 0
4.14 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.
當核心執行緒的數量少於處理器數量時,部分處理器將保持閒置,因為排程器僅將核心執行緒映射到處理器,而不會將使用者層級執行緒映射到處理器。當核心執行緒的數量正好等於處理器數量時,有可能所有處理器將同時被利用。然而,當一個核心執行緒在核心內部阻塞(例如因頁面錯誤或呼叫系統呼叫時),對應的處理器將保持閒置。當核心執行緒數量多於處理器數量時,一個被阻塞的核心執行緒可以被交換出去,讓另一個準備執行的核心執行緒佔用處理器,從而提高多處理器系統的利用率。