112 年 國立陽明交通大學資訊工程學系碩士班《計算機系統》
第 1 題4 分
Which of the following statements on page replacement is (are) true?
(A) FIFO page replacement algorithm may suffer from Belády's anomaly.
(B) With LRU page replacement, adding page frames may increase the number of page faults.
(C) The LRU page replacement algorithm achieves the minimum number of page faults.
(D) Increasing the size of the TLB (translation lookaside buffer) reduces the number of page replacements.
登入後即可作答並保存紀錄。
核心觀念
頁面置換是在實體記憶體的頁框已滿、又發生缺頁時,決定淘汰哪一頁。不同置換演算法的缺頁次數,會受到演算法性質與頁框數量影響。
- Belády 異常:增加頁框數後,缺頁次數反而增加。FIFO 可能發生此現象。
- 堆疊演算法:使用 個頁框時記憶體中的頁面集合,必定包含使用 個頁框時的集合,因此增加頁框不會增加缺頁次數。LRU 與最佳置換演算法都具有此性質。
- 最佳置換(OPT):淘汰未來最晚才會再次使用的頁面,對已知的參考字串而言,缺頁次數最少。
- TLB:快取虛擬頁面到實體頁框的位址轉換;TLB 未命中會增加轉譯成本,但不等同於缺頁,也不直接決定頁面置換。
解題方法
逐項判斷演算法的性質,並分清楚三件事:頁框數量、缺頁次數與 TLB 命中率。頁框數增加是否可能造成更多缺頁,取決於置換演算法是否具有堆疊性質;而 TLB 容量影響的是位址轉譯快取,不是實體記憶體中的頁框置換。
選項分析
第 2 題4 分
Assume that we create a 64 MB shared memory for processes and on an Intel x86 machine. Which of the following statements is (are) true?
(A) The shared memory must be mapped to the same virtual address range in both and .
(B) If we set the shared memory pages in process as read-only, process will not be able to write to the shared memory.
(C) The shared memory can be swapped to a backing store (e.g., the disk).
(D) It is possible that less than 64 MB of physical memory space is actually allocated for the shared memory.
登入後即可作答並保存紀錄。
核心觀念
共享記憶體的關鍵,是不同處理程序的虛擬位址可以透過頁表映射到同一組實體頁框。各處理程序各自有頁表,因此虛擬位址與頁面權限可以分別設定;共享記憶體的大小也不代表所有頁面都必須同時占用實體記憶體。
解題方法
逐項檢查三件事:共享頁面是否必須使用相同的虛擬位址、讀寫權限是否由各處理程序分別設定,以及 64 MB 的共享區域是否必須全部常駐於實體記憶體。
選項分析
(A) 錯誤。
程序 與 的虛擬位址空間彼此獨立。作業系統可以讓兩者以不同的虛擬位址映射到相同的共享實體頁面,因此不必映射到相同的虛擬位址範圍。
第 3 題4 分
Which of the following statements is (are) true about direct memory access (DMA)?
(A) It allows application programs to access the main memory directly.
(B) A DMA transfer will slow down the execution of a CPU-intensive program.
(C) We can use device interrupts to synchronize DMA transfers.
(D) We can use polling to synchronize DMA transfers.
登入後即可作答並保存紀錄。
核心觀念
DMA(直接記憶體存取)讓 I/O 裝置在 DMA 控制器協助下,直接與主記憶體傳送資料,減少 CPU 逐字元或逐字組搬運資料的工作。CPU 通常負責設定來源、目的地與傳輸長度,並在傳輸完成後處理結果。
DMA 傳輸可透過兩種常見方式與 CPU 同步:
- 中斷:傳輸完成或發生錯誤時,裝置或 DMA 控制器通知 CPU。
- 輪詢:CPU 定期讀取狀態暫存器,確認傳輸是否完成。
解題方法
逐項判斷敘述是否符合 DMA 的用途、記憶體保護規則,以及傳輸同步方式。特別要留意選項 B 的「will slow down」是必然敘述;DMA 可能與 CPU 競爭記憶體匯流排,但不代表每次都會降低 CPU 密集型程式的執行速度。
選項分析
(A) 錯誤。
DMA 讓裝置能直接與主記憶體交換資料,不表示應用程式可以直接存取主記憶體。應用程式仍受作業系統與記憶體保護機制限制;通常由作業系統或裝置驅動程式設定 DMA 傳輸。
第 4 題4 分
To move file from to , which of the following step(s) are required (cannot be skipped)?
(A) Make an entry for in directory .
(B) Allocate data blocks on the disk for .
(C) Copy the file content of to the data blocks of .
(D) Erase the entry for in directory .
登入後即可作答並保存紀錄。
核心觀念
檔案系統通常把「檔案名稱」和「檔案內容」分開管理:目錄項目記錄檔名及其對應的 inode(或類似的檔案識別資料),inode 再指向檔案的資料區塊。因此,移動檔案時若來源與目的地位於同一個檔案系統,通常只需變更目錄項目;檔案本身的 inode 與資料區塊不必改變。
解題方法
比較移動前後的目錄對應關係:
- 移動前:目錄
x中有f,指向原檔案。 - 移動後:目錄
y中有f,指向同一個原檔案;目錄x中不再有f。
所以需要在目的目錄建立檔名對應,並移除來源目錄中的原對應。檔案內容仍留在原資料區塊,不必另行配置或複製。
選項分析
第 5 題4 分
Assume that a file system has no fragmentation to begin with (the disk blocks of each file are contiguous). If we want to have some files with non-contiguous data blocks on the file system, which of the following file operation(s) are essential?
(A) Changing a file name.
(B) Deleting a file.
(C) Creating a new file and writing some data to the file.
(D) Overwriting the data of an existing file.
登入後即可作答並保存紀錄。
核心觀念
檔案系統的「檔案碎裂」是指同一個檔案的資料區塊分散在磁碟不同位置,彼此不連續。題目已給定初始狀態下每個檔案的區塊都連續,因此要產生非連續配置,必須有操作讓新檔案取得資料區塊。
解題方法
逐一判斷各操作能否讓某個檔案的資料區塊變成非連續。關鍵在於分辨「改變目錄資訊」、「釋放區塊」與「配置檔案資料區塊」:只有配置資料區塊,才會直接形成非連續檔案。
選項分析
- (A) Changing a file name:錯誤。 更改檔名只改變檔案的名稱或目錄項目,不會重新配置資料區塊,也不會改變原本連續的區塊配置。
- (B) Deleting a file:錯誤。 刪除檔案會釋放它占用的區塊,可能在可用空間中形成空洞,但被刪除的檔案已不存在,其他既有檔案的區塊配置也不因此改變。刪除可以造成可用空間分散,卻不是產生非連續檔案的必要操作。
第 6 題4 分
Which statement(s) about processes are correct?
(A) When a process creates a child process, it can either continue its execution or wait for the child process to complete before continuing.
(B) A process may run out its stack memory by spawning too many processes as it runs.
(C) In a multithreaded multicore system, it is possible for two threads of the same process to run simultaneously on two different cores.
(D) A deep recursive function call can consume a lot of memory from the heap section of the process.
登入後即可作答並保存紀錄。
核心觀念
程序(process)建立子程序後,可以與子程序並行執行,也可以等待子程序結束後再繼續。多執行緒程序中的執行緒(thread)共享同一程序的位址空間,但仍可由不同處理器核心同時執行。
程序的記憶體通常分成程式碼、資料、堆積(heap)與堆疊(stack)等區域。函式呼叫會建立呼叫框架,遞迴呼叫會累積多層呼叫框架,通常使用堆疊空間。
解題方法
逐項判斷敘述是否符合程序與執行緒的基本定義,並留意題目是否把「程序可用的記憶體」和特定的「堆積區」混為一談。
選項分析
(A) 正確。 建立子程序後,父程序可以繼續執行,與子程序並行;也可以呼叫等待機制,等子程序完成後再繼續。
(B) 錯誤。 建立過多程序可能耗盡系統資源,例如程序表項目或可用記憶體,但這不等於父程序會因此用完自己的堆疊記憶體。建立子程序本身不必然使父程序的堆疊持續增長。
第 7 題4 分
Consider the following CPU scheduling result in a single-processor system. If all five processes complete within 18 ms, which statement(s) are correct?
🖼️【此處有附圖,見下方】
(A) This result is not possible with round-robin scheduling.
(B) This result is possible if non-preemptive SJF is used and processes do not arrive at the same time.
(C) This result is possible if preemptive SJF is used and processes do not arrive at the same time.
(D) If both preemptive SJF and non-preemptive SJF yield this result, the waiting time of must be zero.
登入後即可作答並保存紀錄。
核心觀念
本題考的是:同一張 CPU 排程圖,是否能由不同排程演算法產生。
- 輪轉排程(RR):每次最多執行一個時間量子;若行程在時間量子用完前完成,就直接執行下一個行程。
- 非搶先式 SJF:CPU 空出時,選擇已到達行程中 CPU 執行時間最短者;開始執行後不被搶先。
- 搶先式 SJF(SRTF):隨時選擇剩餘執行時間最短者;新行程到達時,必須比較它與目前行程的剩餘時間。
- 本題各行程皆只執行一個連續區段,因此等待時間為 其中 、、、 分別為到達時間、開始時間、完成時間與 CPU 執行時間。
解題方法
圖中的時間單位為毫秒,五個連續執行區段依序為: 在 –、 在 –、 在 –、 在 –、 在 –。因此,五個行程的 CPU 執行時間依序為 毫秒。
題目未指定到達時間。要證明某種排程「可以」產生此結果,只需構造一組符合條件的到達時間;要否定「必須」,則只需提出反例。採用以下設定:
| 行程 | CPU 執行時間 (毫秒) | 到達時間 (毫秒) |
|---|---|---|
| 2 | 0 | |
| 1 | 1.5 | |
| 5 | 2.5 | |
| 4 | 7 | |
| 6 | 11 |
這些到達時間是用來驗證選項的構造範例,且五個行程並非同時到達。總執行時間為
與圖中 CPU 從 到 毫秒連續執行的結果相符。
選項分析
(A) 錯誤:輪轉排程也能產生此結果。
使用上述到達時間,並設定時間量子為 毫秒。每個行程的 CPU 執行時間都不超過時間量子,因此每次取得 CPU 後,都能直接執行到完成。
各行程依序取得 CPU,產生
第 8 題4 分
Consider the following C code for creating two threads. Which statement(s) are correct?
(A) The output is always 20000.
(B) It is possible that the output is 19999.
(C) The output is fixed to 20000 if we change the order of the two calls to pthread_join.
(D) The first thread has a higher priority than the second one in thread scheduling.
// header files here
int count = 0;
void *mythread(void *arg) {
for (int i = 0; i < 10000; i++) {
// wait for some time
count += 1;
}
return(NULL);
}
int main() {
pthread_t p1, p2;
pthread_create(&p1, NULL, mythread, NULL);
pthread_create(&p2, NULL, mythread, NULL);
pthread_join(p2, NULL); pthread_join(p1, NULL);
printf("%d", count);
return 0;
}
登入後即可作答並保存紀錄。
核心觀念
這題考的是競爭條件(race condition)與非原子操作。兩個執行緒各自執行 10,000 次 count += 1,但這個運算不是不可分割的一步;它包含讀取 count、加一、寫回結果。若兩個執行緒交錯執行,可能發生「遺失更新」。
pthread_join 會等待指定的執行緒結束,確保主執行緒在兩個工作執行緒都結束後才印出結果;它不會替兩個工作執行緒之間的 count 更新加鎖。
解題方法
若更新完全沒有衝突,總增量為:
但若兩個執行緒同時讀到相同的 count,可能都算出相同的新值,接著後寫入的值覆蓋先寫入的值。例如:
- 執行緒一讀到
count = 100。 - 執行緒二也讀到
count = 100。 - 執行緒一寫入
101。 - 執行緒二也寫入
101。
兩次遞增只讓 count 增加 1,因此最終值可能是 19,999,而不是 20,000。
要保證每次更新都被計入,需使用互斥鎖保護遞增操作,或採用適當的原子操作。
選項分析
第 9 題4 分
Which statement(s) about condition variables are correct?
(A) A call to pthread_cond_wait() causes the calling thread to busy wait on a condition variable.
(B) pthread_cond_wait() includes a mutex lock as a parameter.
(C) The pthread_cond_signal() function signals one thread that is waiting on the condition variable.
(D) The thread that calls pthread_cond_signal() aborts its execution right after the return of the call, after which the CPU scheduler gives the execution right to the awakened thread.
登入後即可作答並保存紀錄。
核心觀念
條件變數用來讓執行緒在某個條件尚未成立時暫停,等其他執行緒通知後再繼續。它通常與互斥鎖搭配:等待執行緒先持有互斥鎖,再呼叫 pthread_cond_wait();此函式會原子地釋放互斥鎖並讓執行緒進入等待狀態。收到通知後,執行緒會重新取得互斥鎖,才從函式返回。
條件變數只負責通知,不會替程式保證共享資料的條件已成立。因此,等待條件通常要放在 while 迴圈中檢查。
解題方法
逐項核對 POSIX 條件變數的等待與通知行為:等待是否忙等、等待函式是否接收互斥鎖、通知會喚醒幾個等待者,以及呼叫通知的執行緒是否會立刻讓出處理器。
選項分析
(A) 錯誤。 pthread_cond_wait() 不會讓呼叫執行緒持續佔用處理器忙等,而是使其阻塞等待。通知到達後,執行緒才有機會繼續執行。
(B) 正確。 函式需傳入條件變數與互斥鎖,形式如下:
第 10 題4 分
Consider a system with resources and 4 threads: , , , and . Let and denote the maximum number of resources requested and the current number of resources held by thread , respectively, for all , . Which of the following statement(s) are correct?
| Thread | Maximum Needs | Current Needs |
|---|---|---|
(A) The system state is deadlocked if .
(B) The system state is deadlocked if .
(C) The system state is deadlocked if for all , .
(D) The system state is safe if for some , .
登入後即可作答並保存紀錄。
核心觀念
令目前可用資源數為
每個執行緒尚未取得的最大需求為 。題目考查的是:比較各執行緒的剩餘需求與可用資源,判斷是否有人能取得足夠資源完成執行。
要區分「死結」與「不安全狀態」: 是最大需求減去目前持有量,並不必然等於執行緒此刻正在等待的資源數。死結偵測通常還需要知道目前的請求;安全狀態判斷則要確認能否安排一個讓所有執行緒依序完成的順序。
解題方法
先算出可用資源 ,再逐一比較 和 。若四個執行緒的剩餘需求都大於目前可用資源,依本題預期的簡化判準,沒有任何執行緒能先取得所需資源並完成,因此對應到選項 C。
選項分析
- (A) 錯。 總剩餘需求大於可用資源,只表示資源不足以同時滿足所有執行緒的最大需求,不代表死結。若某個執行緒的剩餘需求不超過 ,它仍可能先完成並釋放資源。
- **(B) 錯。
第 11 題4 分
Two CPUs are implemented to execute exactly the same ISA. Assume that CPU A is a single-cycle implementation while CPU B is a five-stage pipelined implementation. The instruction and data memory devices are zero-wait-state memory for both CPUs. Which of the following statements are true when both CPUs run the same program C with finite execution time?
(A) The execution time of CPU B is shorter than that of CPU A.
(B) The critical path of CPU B can always be made shorter than that of CPU A.
(C) CPU B will need more circuit resources than CPU A does.
(D) The CPI of CPU A is higher than the CPI of CPU B.
登入後即可作答並保存紀錄。
核心觀念
執行時間可寫成:
其中 是指令數,CPI 是每條指令平均所需的時脈週期數, 是時脈週期。兩部 CPU 執行相同程式,因此指令數相同;但 CPI 與時脈週期長度可能不同。
單週期 CPU A 的每條指令都在一個週期內完成,故 CPI 為 ,而週期必須夠長,能容納最長的指令路徑。五階段管線 CPU B 將指令處理拆成多個階段,時脈週期由最慢階段的延遲決定;資料相依等情況則可能使管線停頓。
解題方法
逐項比較兩部 CPU 的執行時間、關鍵路徑、硬體資源與 CPI。特別要分清楚「單條指令的延遲」和「管線每週期完成一條指令的吞吐量」:管線提高吞吐量,不代表任何有限程式的總執行時間必定較短。
選項分析
第 12 題4 分
To remove hazards for a single-issue in-order execution pipelined processor, which of the following techniques may require help from the compilers (assuming that you only write programs using high-level languages)?
(A) Delayed branch.
(B) Dynamic branch prediction with branch history tables.
(C) Load-use hazard removal.
(D) Forwarding.
登入後即可作答並保存紀錄。
核心觀念
管線危障可分為控制危障與資料危障。處理方式有些由硬體直接完成,有些則可由編譯器重新安排指令,減少停滯週期。題目問的是:在只使用高階語言撰寫程式的前提下,哪些技術可能需要編譯器協助。
解題方法
逐一判斷各技術的主要工作由誰負責:
- 若需要安排指令順序,或在指令間填入適當工作,編譯器可能需要協助。
- 若由處理器硬體根據執行狀態動態判斷、預測或轉送資料,通常不需要編譯器介入。
選項分析
(A) Delayed branch(延遲分支):正確。
延遲分支會在分支指令後安排一個延遲槽,槽內指令在分支結果確定前仍會執行。編譯器可從前後指令中挑選不受分支結果影響的指令放入延遲槽,避免該週期閒置。高階語言程式由編譯器轉成機器指令,因此這種排程可能需要編譯器協助。
第 13 題4 分
Modern ISAs often contain a pair of instructions for multi-thread and multi-core synchronization. In MIPS, this pair of instructions includes a special load called a load linked, ll, and a special store called a store conditional, sc. Which of the following statements are true?
(A) The instruction ll reads and locks a memory cell such that no other threads can change its value until an sc instruction executed by the same thread changes its value.
(B) The execution of the sc instruction will always be successful if no context switch occurs after the ll instruction.
(C) The following code segment has a bug in protecting the memory pointed to by $s1:
try:
ll $t0, 0($s1)
addi $t1, $t0, 1
bnez $t1, skip
sc $t1, 0($a1)
beqz $t0, try
skip:
(D) The ll and sc instruction pairs perform shared resource protection more efficiently than the atomic swap instruction used in, for example, the ARM ISA, especially when there are a lot of concurrent processes.
登入後即可作答並保存紀錄。
核心觀念
ll(load linked)與 sc(store conditional)用來實作原子更新。ll 讀取記憶體時,處理器會記錄一個保留狀態;之後執行 sc 時,若保留狀態仍有效,才會把資料寫入指定位置,並回報是否成功。
ll 不會把記憶體鎖住。其他處理器或執行緒仍可寫入該位置;若相關寫入使保留狀態失效,sc 就會失敗。sc 的成功與否也不能只靠「中間沒有發生情境切換」判定。
解題方法
逐項檢查三件事:ll 是否真的阻止其他寫入、sc 的成功條件是否有保證,以及程式中的 ll 與 sc 是否操作同一個記憶體位址。最後比較競爭情況下,ll/sc 與原子交換的記憶體更新行為。
選項分析
(A) 錯誤。
ll 會建立保留狀態,但不會鎖住記憶體,也不會禁止其他執行緒修改該位置。若其他處理器寫入造成保留狀態失效,後續的 sc 會失敗,而不是阻止那次寫入。
(B) 錯誤。
沒有情境切換,不代表 sc 一定成功。另一個處理器仍可能在 ll 與 sc 之間寫入相關位置,使保留狀態失效;此外,保留狀態也可能因實作規則或其他事件而失效。
第 14 題4 分
The following figure shows a simple implementation of a MIPS processor. Which of the following statements are true?
🖼️【此處有附圖,見下方】
(A) The following code segment can be used to detect a stuck-at-0 fault of MemtoReg:
addi $t1, zero, 1
sw $t1, 0(zero)
lw $t0, 0(zero)
slti $t0, $t0, 1
(B) The CPU will not suffer from load-use hazards.
(C) The instruction memory does not have to be a synchronous memory (i.e., driven by a clock signal).
(D) The register file does not have to be a synchronous component.
登入後即可作答並保存紀錄。
核心觀念
這題考單周期 MIPS 資料路徑,以及各元件在一個時脈週期內的工作方式。圖中包含 PC、指令記憶體、暫存器檔、ALU、資料記憶體,以及控制 MemtoReg 的寫回多工器。單周期設計會在同一個週期內完成一條指令的取指、執行與寫回;暫存器檔則以時脈控制寫入,讀取可為組合邏輯。
解題方法
圖中 MemtoReg 多工器選擇寫回暫存器的資料來源:0 選 ALU 結果,1 選資料記憶體讀值。判斷 (A) 時,比較 lw 正常讀出與 MemtoReg 故障固定為 0 時的結果;判斷 (B) 時,確認單周期資料路徑是否會讓相鄰指令等待前一指令完成;判斷 (C)、(D) 時,分別檢查指令記憶體的讀取與暫存器檔的狀態更新是否需要時脈控制。
選項分析
(A) 正確。 addi 將 1 寫入 $t1,接著 sw 把 1 存至位址 0,lw 再讀取位址 0。
- 正常時,
lw經由MemtoReg選出記憶體讀值 1,故$t0=1。最後的slti $t0, $t0, 1計算 ,結果為 0。
第 15 題4 分
Assume that we have a 5-stage processor that has neither data forwarding nor branch prediction. It has separate instruction and data memory. Bubbles will be inserted when there are hazards. The processor always resolves the branch target at stage 2 (the decode stage). Which of the following statements are true when the following code segment is executed?
addi $t1, zero, zero
loop:
addi t1, 1
lw t1)
subi s0, 2
sw t1)
bne t3, loop
(A) The bubble(s) inserted for the subi instruction can be removed by forwarding.
(B) If is 100, the CPI will be larger than 2.5 for the execution of the code segment.
(C) Excluding the adders for the PC, the processor needs at least two more adders. Note that a subtractor is counted as an adder.
(D) Adding a static branch predictor increases the CPI for this code segment when is 100.
登入後即可作答並保存紀錄。
核心觀念
五級管線依序為 IF、ID、EX、MEM、WB。沒有資料轉送時,消費者必須等到生產者寫回暫存器後才能讀取;在常見的「同一週期先寫回、後讀取」假設下,緊鄰的 RAW 相依需要插入 2 個氣泡。
本題還要計算分支控制代價:分支在 ID 階段解析,採「向後分支預測為 taken」的靜態策略時,迴圈大多數分支都能預測正確。
解題方法
時,初始指令將 設為 0;每次迴圈將 加 1,執行到 時離開。因此迴圈執行 100 次,動態指令數為
初始 addi 與迴圈第一條 addi 相依,插入 2 個氣泡。每次迴圈另有三組緊鄰的資料相依:
addi→lw: 用於位址計算。lw→subi: 用於減法。subi→sw: 用於存入資料。
每組插入 2 個氣泡,因此資料相依氣泡共 個。100 次分支中有 99 次 taken;靜態預測向後分支為 taken 時,只有最後一次離開迴圈的分支預測錯誤。即使以每次分支都付出 1 個控制氣泡估算,總週期數也約為
第 16 題4 分
Which of the following statements about cache are true?
(A) A virtually-addressed cache access is typically faster than that of a physically-addressed cache.
(B) High associativity in a cache can reduce compulsory misses.
(C) Cache performance is of less importance in faster processors because the processor speed compensates for the high memory access time.
(D) Larger blocks reduce the miss rate by taking advantage of spatial locality.
登入後即可作答並保存紀錄。
核心觀念
快取(cache)利用時間區域性與空間區域性,降低處理器平均記憶體存取時間。判斷快取敘述時,要分辨快取位址方式、未命中類型,以及快取區塊大小與相連度的影響。
三種常見未命中為:
- 強制未命中:第一次存取某個區塊時發生。
- 容量未命中:快取容量不足,無法容納工作集合。
- 衝突未命中:多個記憶體區塊映射到相同快取位置,彼此競爭。
解題方法
逐項檢查敘述是否符合快取的基本設計原理:位址轉換是否增加存取延遲、相連度能影響哪些未命中、處理器加快後快取的重要性如何變化,以及較大區塊如何利用空間區域性。
選項分析
(A) 正確。
虛擬位址快取可直接使用虛擬位址進行快取查找,不必先等待虛擬位址轉成實體位址,因此存取通常比實體位址快取更快。
第 17 題4 分
The following table shows the instruction type breakdown of a given application executed in a CPU. Assume the CPU has a 2 GHz clock rate. Which of the following statements are correct?
| FP instr. | INT instr. | L/S instr. | Branch instr. | |
|---|---|---|---|---|
| Number of instr. | ||||
| CPI | 1 | 1 | 4 | 2 |
(A) The execution time of this program is 1024 ms.
(B) We can make the program run two times faster by only improving the CPI of L/S instructions.
(C) We can make the program run two times faster by only improving the CPI of INT instructions.
(D) If we improve the CPU such that CPI of INT and FP instructions is reduced by 40% and the CPI of L/S and branch is reduced by 30%, the execution time of running this program will become 342 ms.
登入後即可作答並保存紀錄。
核心觀念
CPU 執行時間由指令數、每類指令的 CPI,以及時脈週期共同決定:
其中,指令數乘上 CPI 得到該類指令所需的時脈週期數。若時脈頻率以 GHz 表示、週期數以十億為單位,計算結果的時間單位為秒。
解題方法
先計算各類指令所需的時脈週期數,再加總:
| 指令類型 | 指令數 | CPI | 時脈週期數 |
|---|---|---|---|
| 浮點指令 | 1 | ||
| 整數指令 | 1 | ||
| 載入/儲存指令 | 4 | ||
| 分支指令 | 2 |
總時脈週期數為:
時脈頻率為 Hz,因此原始執行時間為:
選項分析
(A) 錯誤。 執行時間為 ,不是 。 是總時脈週期數,還需要除以 Hz 才能得到時間。
(B) 正確。 若只改善載入/儲存指令的 CPI,要使總執行時間減半,總週期數須由 降至
第 18 題4 分
Given a processor with two caches: an I-cache and a D-cache, and the miss penalty is 200 cycles for all misses. Assume the I-cache miss rate is 2%, D-cache miss rate is 4%, and the percentage of all lw and sw instructions is 50%. If the processor has a CPI of 2 without any memory stalls, which of the following statements are true when memory stall is considered? (Note: each statement is independent.)
(A) The CPI will become 10 if memory stall is considered.
(B) If we double the clock rate (but the memory access time to handle a cache miss does not change), the CPI will become 18.
(C) If we double the clock rate (but the memory access time to handle a cache miss does not change), the machine will have a speedup of .
(D) If an L2 cache is added only for I-cache and its miss penalty is 20 clock cycles, assume global instruction miss rate to main memory became 0.5% after the L2 cache is added. The CPI will become 3.4.
登入後即可作答並保存紀錄。
核心觀念
平均每條指令的記憶體停滯週期數,可依各類存取事件分別計算:
每條指令都要取指,因此 I-cache miss rate 直接乘上每次 miss penalty。D-cache 只在 lw、sw 時存取,所以須先乘上這類指令占比。
解題方法
沒有記憶體停滯時,CPI 為 。以原始時脈計算,I-cache 每條指令造成的平均停滯為
lw、sw 占全部指令的 ,其中 發生 D-cache miss,因此 D-cache 每條指令造成的平均停滯為
所以考慮記憶體停滯後:
選項分析
-
(A) 正確。 原時脈下的 CPI 為 ,與敘述相符。
-
(B) 正確。 時脈加倍後,若記憶體處理 miss 的實際時間不變,每次 miss 換算成的時脈週期數也加倍,從 變成 。因此:
第 19 題4 分
The system has a direct-mapped cache with total data size of 8192 bytes and block size of 16 bytes. Assume that the cache starts out empty. For the following piece of code, which of the following statements are correct? (Note: Assume that int is of 32-bits (4 bytes); variables and are both in registers and the array is in memory.)
int A[256];
int x = 0;
for (int i = 0; i < 256; i++) {
x += A[i];
}
(A) There will be 256 cache misses in total.
(B) All the misses are compulsory misses.
(C) The cache hit-rate is 0.25.
(D) If we double the block size, the miss rate will be halved.
登入後即可作答並保存紀錄。
核心觀念
直接對映快取中,每個快取區塊可容納連續的一段主記憶體資料。連續讀取陣列時,同一區塊內後續的資料存取會命中。
本題每個 int 為 4 bytes,快取區塊為 16 bytes,因此每個區塊可放:
以下依一般題目假設,陣列起始位置與區塊邊界對齊。
解題方法
程式依序讀取 A[0] 到 A[255],共存取 256 個整數。每連續 4 個整數位於同一個快取區塊:首次讀取該區塊時發生一次未命中,之後讀取同區塊的另外 3 個整數皆命中。
因此,陣列共占:
快取共有 個區塊,陣列所需的 64 個區塊可同時放入快取,不會因容量不足而遭到替換。總未命中數為 64,命中數為 ,所以未命中率為 ,命中率為 。
第 20 題4 分
The following table shows two processors and their read/write operations on two different variables and (initially ). After the operations, which of the following statements are correct?
| Processor 1 | Processor 2 |
|---|---|
a++; b++; | a=2; b=b+2; |
(A) The value of can be 1, 2, or 3 if the system ensures cache coherence.
(B) The value of can be 1, 2, or 3 if the system does not ensure cache coherence.
(C) The value of can be 1, 2, or 3 if the system ensures cache coherence.
(D) The value of can be 1, 2, or 3 if the system does not ensure cache coherence.
登入後即可作答並保存紀錄。
核心觀念
這題考的是**快取一致性(cache coherence)與讀取-修改-寫入(read-modify-write)**的差別。
a++、b++ 和 b=b+2 都可以拆成讀取、計算、寫回三個步驟。例如:
兩個處理器執行時,這些步驟可能交錯。快取一致性主要規範同一記憶體位置的寫入如何被觀察;它本身不保證上述讀取-修改-寫入是不可分割的原子操作。
解題方法
分別追蹤變數 、。初值皆為 。若一個處理器先讀到舊值,另一個處理器隨後寫入,而前者最後才寫回,就可能覆蓋對方的更新。
以 為例,令 P1 的 a++ 拆為「讀取 、加 、寫回」:
- 最後得到 :P1 先讀到 ;P2 寫入 ;P1 依照先前讀到的 寫回 。
- 最後得到 :P1 先讀到 並寫回 ;P2 再寫入 。
- 最後得到 :P2 先寫入 ;P1 讀到 ,再加 寫回 。
這些執行次序都可以符合同一變數的寫入順序,因此即使有快取一致性,只要 a++ 不是原子操作, 仍可能是 、 或 。
對 ,P1 的 b++ 與 P2 的 b=b+2 也都是讀取-修改-寫入:
Assume that we have four disks (disk0–3), and each disk has 1 TB of space. We use the notation diskX[i] to denote the sector on diskX. Now, we form a RAID5 array with all four disks.
第 21 題
Which of the following numbers is closest to the maximum capacity (the maximum amount of data you can store on the array) of the RAID5 array?
(A) 1 TB
(B) 2 TB
(C) 3 TB
(D) 4 TB
登入後即可作答並保存紀錄。
核心觀念
RAID 5 使用分散式同位元(parity)提供容錯能力。每個同位元區塊分散存放在不同磁碟上;在任一磁碟故障時,可利用其餘磁碟上的資料與同位元重建內容。
對 顆容量相同、每顆容量為 的磁碟,RAID 5 的可用資料容量為:
也就是總容量中,約有一顆磁碟的容量用於同位元資訊。
解題方法
題目有 顆磁碟,每顆容量為 。因此 RAID 5 可用來存放資料的容量為:
同位元資料不是固定放在某一顆磁碟上,而是分散在四顆磁碟中;整體而言,相當於保留一顆磁碟的容量作為冗餘。
第 22 題
What is the maximum number of disk failures that the RAID5 array can tolerate?
(A) 1
(B) 2
(C) 3
(D) 4
登入後即可作答並保存紀錄。
核心觀念
RAID 5 將資料與同位元分散在所有磁碟上。每組資料區塊會利用 XOR 運算產生同位元;若一顆磁碟故障,可用其餘磁碟上的資料與同位元重建遺失內容。
解題方法
RAID 5 的同位元資訊可在單一磁碟故障時提供重建所需資訊,因此最多可容忍 顆磁碟故障。四顆磁碟的數量不會讓 RAID 5 多出第二份同位元;若同時故障兩顆磁碟,剩餘資訊不足以唯一重建遺失資料。
選項分析
第 23 題
In the worst case, how many disks will be involved in writing a single byte to the RAID5 array?
(A) 1
(B) 2
(C) 3
(D) 4
登入後即可作答並保存紀錄。
核心觀念
RAID 5 以 XOR 奇偶資訊保護資料。四顆磁碟的一個 stripe 包含三個資料區塊與一個 parity 區塊;parity 區塊的位置會在不同 stripe 間輪替。若三個資料區塊為 ,則:
更新資料時,對應的 parity 也必須更新,才能維持 RAID 5 的冗餘資訊。
解題方法
題目問的是「最壞情況」涉及幾顆磁碟。單一 byte 的更新屬於小範圍寫入;在最壞情況下,系統可讀取 stripe 的三個資料區塊,重新計算 parity,再寫回被修改的資料區塊與 parity 區塊。
三個資料區塊分布在三顆磁碟上,parity 區塊位於另一顆磁碟。因此,這個過程會涉及全部四顆磁碟。
Consider three processes , , and using message-passing for communications. Their behaviors are illustrated below. Let BS = blocking send, NS = nonblocking send, BR = blocking receive, and NR = nonblocking receive.
| Process P | Process Q | Process R |
|---|---|---|
…<br>send(Q, msg1)<br>receive(R, msg2) | …<br>send(R, msg3)<br>receive(P, msg4) | …<br>send(P, msg5)<br>receive(Q, msg6) |
第 24 題
Which type of naming is used?
(A) direct
(B) indirect
(C) insufficient evidence to judge
(D) none of the above
登入後即可作答並保存紀錄。
核心觀念
訊息傳遞的命名方式,取決於程序如何指定訊息的收件者或寄件者:
- 直接命名(direct naming):送訊息時明確指定接收程序,例如
send(Q, msg1);接收時也可明確指定寄件程序。 - 間接命名(indirect naming):透過信箱或通訊埠傳遞訊息,例如
send(mailbox1, msg1),程序不直接指定彼此。
解題方法
觀察題目中的傳送與接收操作。程序 使用 send(Q, msg1),明確指定程序 為收件者;receive(R, msg2) 也明確指定從程序 接收訊息。因此,這是以程序名稱直接指定通訊對象的直接命名。
第 25 題
Which data structure is needed to support this type of communication?
(A) mailbox
(B) port
(C) semaphore
(D) none above
登入後即可作答並保存紀錄。
核心觀念
訊息傳遞可分為直接通訊與間接通訊。直接通訊時,傳送與接收操作直接指定對方程序,例如 send(Q, msg1) 指定接收者為程序 ,receive(R, msg2) 指定訊息來源為程序 。
間接通訊則透過信箱或連接埠等中介端點傳遞訊息。阻塞與非阻塞會影響程序是否等待操作完成,但不會改變通訊是直接或間接的判斷。
解題方法
觀察操作中的程序名稱:send(Q, ...) 與 receive(R, ...) 都直接寫出對方程序,表示這是直接訊息傳遞。因此,這種通訊不需要以信箱或連接埠作為中介,也不需要信號量來支援訊息傳遞。
選項分析
第 26 題
Which condition will cause deadlock in this example?
(A) BS
(B) BR
(C) both BS and BR
(D) none above
登入後即可作答並保存紀錄。
核心觀念
阻塞傳送(BS)會讓傳送端等待,直到接收端接收訊息;非阻塞傳送(NS)則會立即返回,讓程序繼續執行。阻塞接收(BR)會等待訊息到達,非阻塞接收(NR)則不會等待。
判斷死結時,關鍵是看程序是否形成「互相等待」的循環:若每個程序都卡在目前的操作,且無法繼續執行到解除等待的接收操作,就會發生死結。
解題方法
若三個程序都採用 BS,各程序的第一個動作都是傳送:
- 傳送
msg1給 ,等待 接收。 - 傳送
msg3給 ,等待 接收。 - 傳送
msg5給 ,等待 接收。
然而,三個程序都停在傳送處,尚未執行後續的接收。因此, 等 、 等 、 等 ,形成循環等待,造成死結。
第 27 題
If deadlock is not possible, which condition will ensure each process to proceed only after receiving its message?
(A) BS
(B) BR
(C) NS
(D) NR
登入後即可作答並保存紀錄。
核心觀念
阻塞接收(blocking receive,BR)會讓呼叫端停在 receive,直到指定來源的訊息到達;非阻塞接收(nonblocking receive,NR)則立即返回,即使訊息尚未到達。因此,要確保每個程序都必須等收到訊息後才能繼續執行,應使用 BR。
解題方法
觀察三個程序的順序:每個程序都先送出訊息,再接收另一個程序的訊息。若接收操作是阻塞的,程序執行到 receive 時便會等待,直到收到指定訊息才會繼續。題目已排除死結,因此每個程序最後都能收到訊息並繼續執行。
選項分析
We want to translate a C function my_call() into MIPS assembly language. The function my_call() calls another function func() which is declared as int func(int s, int t);. The translated assembly code for the function my_call() is as follows:
my_call:
addi $sp, $sp, -12
sw $ra, 8($sp)
sw $s1, 4($sp)
sw $s0, 0($sp)
move $s1, $a2
move $s0, $a3
jal func
move $a0, $v0
add $a1, $s0, $s1
jal func
lw $ra, 8($sp)
lw $s1, 4($sp)
lw $s0, 0($sp)
addi $sp, $sp, 12
jr $ra
第 28 題
How many parameters does the function my_call() have?
(A) 1
(B) 2
(C) 3
(D) 4
登入後即可作答並保存紀錄。
核心觀念
依 MIPS 呼叫慣例,函式前四個整數參數分別放在 $a0、$a1、$a2、$a3。呼叫其他函式時,$a 暫存器可能被改寫;若要跨函式呼叫保留資料,需先將資料保存到適當的暫存器或記憶體。
解題方法
觀察 my_call() 開頭保存的兩個參數暫存器:
move $s1, $a2
move $s0, $a3
這表示進入 my_call() 時,第三個參數位於 $a2,第四個參數位於 $a3。接著,程式使用 $s0、$s1 保存這兩個值,讓它們能在第一次呼叫 func() 後繼續使用。
$a0 和 $a1 則是前兩個參數的位置。第一次呼叫 func() 後,程式將回傳值放進 $a0,並以 $s0 + $s1 設定 `
第 29 題
Which of the following C code could be the body of my_call()?
(A) { return func(func(a,b), c+d); }
(B) { return func(a,b) + func(c,d); }
(C) { return func(a+b, func(a+b,c)); }
(D) { return func(a, func(c+d,b)); }
登入後即可作答並保存紀錄。
核心觀念
MIPS 函式呼叫通常以 $a0、$a1 傳入前兩個整數引數,並以 $v0 傳回整數結果。呼叫其他函式後,若後續還需要原本放在暫存器中的資料,就必須先保存它們。
本題的 my_call() 有兩次 func() 呼叫。判讀時要追蹤每次呼叫前 $a0、$a1 的值,以及第一次呼叫後 $v0 的用途。
解題方法
假設 my_call() 的引數依序為 a、b、c、d,進入函式時分別放在 $a0、$a1、$a2、$a3。
- 函式先在堆疊上保存
$ra、$s1、$s0。呼叫其他函式會改寫$ra,因此返回位址必須先保存;$s0、$s1則用來保留後續仍要使用的值。 move $s1, $a2與move $s0, $a3把c、d保存到$s1、$s0。- 第一次呼叫
func時,$a0、$a1仍是a、b,因此計算的是func(a,b)。結果放在$v0。 - 接著
move $a0, $v0將第一次呼叫結果作為第二次呼叫的第一個引數。 add $a1, $s0, $s1將保存的d、c相加,作為第二個引數,即 。
第 30 題
The tail-call optimization is a technique that, when a caller simply returns the value that it gets from the callee, can avoid allocating a procedure frame for the last function call. How many instructions can be saved if the tail-call optimization is used to optimize the assembly code?
(A) 1
(B) 2
(C) 3
(D) 4
登入後即可作答並保存紀錄。
核心觀念
尾呼叫最佳化適用於函式的最後一個動作是呼叫另一個函式,且直接把被呼叫函式的結果傳回。此時,呼叫者可先恢復自己的堆疊框架,再直接跳到被呼叫函式,讓它沿用原呼叫者的返回位址。
解題方法
my_call() 的第二次 jal func 是最後一次函式呼叫;呼叫完成後,程式只恢復 $ra、$s1、$s0 與 $sp,再以 jr $ra 返回。因此可把第二次呼叫改成尾呼叫:在跳到 func 前先完成函式框架還原,並將 jal func 改為 j func。
lw $ra, 8($sp)
lw $s1, 4($sp)
lw $s0, 0($sp)
addi $sp,
Consider a two-way set associative cache with four-word blocks and LRU replacement. Assume the cache has a total data size of 32 words and a 24-bit byte address is used.
第 31 題
What is the size (in bits) of the tag field in one block used in this cache?
(A) 23
(B) 22
(C) 19
(D) 18
登入後即可作答並保存紀錄。
核心觀念
快取位址可分成三個欄位:標籤(tag)、集合索引(set index)與區塊位移(block offset)。因此:
題目使用位元組位址,因此區塊位移除了選取區塊內的字(word),還要選取字內的位元組。依本題採用的標準假設,一個 word 為 4 bytes。
解題方法
快取資料容量為 32 words,每個 block 有 4 words,所以快取共有:
兩路組相聯表示每個 set 放 2 個 blocks,因此 set 數為:
選取 4 個 sets 需要 個索引位元。
第 32 題
What is the total size (in bits) of this cache, including data, tag, and valid fields?
(A) 1024
(B) 1100
(C) 1176
(D) 1184
登入後即可作答並保存紀錄。
核心觀念
快取總位元數包含資料欄位、標籤欄位(tag)與有效位元(valid)。組相聯快取的位址分成:
本題每個 word 為 4 bytes,因此 4-word 區塊大小為 bytes。
解題方法
資料容量為 32 words,每個區塊有 4 words,所以快取共有:
每組有 2 個區塊,因此組數為:
區塊大小為 bytes,位移欄位需要:
4 組需要 2 個組索引位元。24-bit 位址中的標籤位元數為:
每個區塊的資料欄位有 bits;再加上 18-bit 標籤與 1 個有效位元,每個區塊共 bits。
第 33 題
Assume that the cache is initially empty. Given a series of memory references: 24, 8, 25, 10, and 41 (which are word addresses), how many cache misses will there be for the references?
(A) 1
(B) 2
(C) 3
(D) 4
登入後即可作答並保存紀錄。
核心觀念
二路組相連快取中,每個組有兩個快取區塊;記憶體區塊編號為 時,其所在組別為:
每個區塊有 4 個字,因此字址 對應的記憶體區塊編號為 。若該區塊不在快取中,便發生一次未命中;若目標組已滿,則依 LRU 規則淘汰最久未使用的區塊。
解題方法
快取總容量為 32 字,每區塊 4 字,因此共有 個區塊。快取為二路組相連,故共有 組。
逐一將字址換算成區塊編號與組別:
| 字址 | 區塊編號 | 組別 | 結果 |
|---|---|---|---|
| 24 | 6 | 未命中,載入區塊 6 | |
| 8 | 2 | 未命中,載入區塊 2 | |
| 25 | 6 | 命中區塊 6 | |
| 10 | 2 | 命中區塊 2 |
第 34 題
For your answer to the above question, how many cache misses are compulsory misses?
(A) 1
(B) 2
(C) 3
(D) 4
登入後即可作答並保存紀錄。
核心觀念
強制失誤(compulsory miss,也稱 cold miss)是某個記憶體區塊第一次被存取時發生的快取失誤。因此,整段存取序列中的強制失誤數,等於被存取過的不同記憶體區塊數;後續再次存取同一區塊,即使因替換而不在快取中,也不算強制失誤。
題目給定的快取容量、組相連度與 LRU 規則,會影響其他類型的失誤,但不會改變強制失誤的定義。
解題方法
逐一查看原題的記憶體存取序列,將每個位址換算成其所在的區塊,再統計不同區塊的數量。若區塊大小為 4 個 word,位址所屬區塊可用「位址除以每個區塊的 byte 數並取整」判定;同一區塊內的不同位址只計一次。