108 年 國立陽明交通大學資訊工程學系碩士班《計算機系統》
第 1 題4 分
Which one(s) of the following statements about processes and threads are correct?
(A) All the data in one thread are shared with other threads in the same process.
(B) Kernel threads in the same process share one program counter.
(C) When multiple user-level threads are mapped to a single kernel thread through a thread library, these user-level threads cannot take advantage of the multi-core structure.
(D) Race condition can occur in a process in which multiple user threads are associated with a kernel thread.
登入後即可作答並保存紀錄。
核心觀念
執行緒是程序內的執行單位。每個執行緒有自己的執行狀態,包括程式計數器、暫存器與堆疊;同一程序中的執行緒則共享程式碼、全域資料、堆積區與開啟的檔案等程序資源。
使用者層級執行緒由執行緒函式庫管理;核心層級執行緒則由作業系統核心排程。若多個使用者層級執行緒映射到單一核心層級執行緒,這種映射稱為「多對一」。
解題方法
逐項區分「執行緒私有狀態」與「程序共享資源」,再判斷多對一映射對平行執行與競爭情況的影響:
- 執行緒的程式計數器、暫存器與堆疊是各自獨立的。
- 同一程序中的執行緒共享程序的記憶體空間與其他程序資源。
- 多對一映射無法讓多個使用者執行緒同時在不同核心上執行;但使用者執行緒仍可能交錯存取共享資料。
選項分析
第 2 題4 分
Which one(s) of the following statements about modern operating systems are correct?
(A) When a process writes a character on screen, this operation is accomplished with one or more system calls.
(B) In a time-sharing system, a process will not leave the running state unless it terminates or it is preempted through a timer interrupt.
(C) Applying the microkernel structure can reduce the communication overhead between kernel and user space.
(D) One process can communicate with another process through shared memory.
登入後即可作答並保存紀錄。
核心觀念
本題考查作業系統的系統呼叫、行程狀態轉換、微核心架構與行程間通訊(IPC)。
- 系統呼叫:使用者程式要求作業系統核心提供服務的介面,例如檔案輸出與裝置存取。
- 行程狀態:行程可能在執行(running)、就緒(ready)或等待(waiting)狀態間轉換。行程離開執行狀態的原因不限於結束或計時器中斷。
- 微核心:將核心功能縮小,許多服務移至使用者空間;核心與服務之間通常需要透過訊息傳遞,會增加通訊成本。
- 共享記憶體:多個行程共同存取同一段記憶體,是行程間通訊方式之一。
解題方法
逐一比對選項與作業系統的基本機制:確認畫面輸出是否涉及系統服務、行程是否只有兩種離開執行狀態的情況、微核心是否降低通訊成本,以及共享記憶體能否用於行程間通訊。
選項分析
(A) 正確。
行程要將字元輸出至螢幕,通常會透過輸出函式要求作業系統處理,例如對終端或裝置呼叫 write。系統呼叫使處理器從使用者模式切換至核心模式,由核心或裝置驅動程式完成後續輸出。
第 3 題4 分
Assume that a system issues multiple readers and writers processes with the following modified readers-writers pseudo codes:
Shared semaphore , initialized to .
Shared Integer initialized to .
A writer process:
- wait();
- writing the content
- signal();
A reader process:
- wait();
- ;
- if (), wait();
- signal();
- reading the content
- wait();
- ;
- if (), signal();
- signal().
Which of the following statements are correct?
(A) When and , at most one process can access (read or write) the content at any time.
(B) When and , at most one reader process is waiting for at any time.
(C) When and , at most two reader processes are waiting for at any time.
(D) None of the above statement is correct.
登入後即可作答並保存紀錄。
核心觀念
mutex 保護共享變數 readcount。讀者執行 wait(mutex) 後,會增加 readcount,再依條件決定是否執行 wait(wrt)。
關鍵在於:讀者呼叫 wait(wrt) 時,尚未執行 signal(mutex)。若 wrt 已被占用,該讀者會阻塞,並且仍持有 mutex。因此,其他讀者無法進入讀者程式碼,也就無法同時排隊等待 wrt。
解題方法
判斷各選項時,分別追蹤兩件事:
- **能否同時讀寫內容:**檢查哪些讀者會略過
wait(wrt),直接進入讀取。 - **能否有多位讀者等待
wrt:**檢查讀者等待wrt時是否仍持有mutex。
選項分析
(A) 錯誤。
令 、。第一位讀者進入時,readcount 變成 ,符合 readcount ≤ Na,因此取得 wrt 後開始讀取。
此時第二位讀者也能取得 mutex,使 readcount 變成 。由於 ,第二位讀者會略過 wait(wrt),直接讀取內容。
第 4 題4 分
If a system is monitored by the banker’s algorithm and is currently in a safe state, which of the following changes can be made safely (without introducing the possibility of deadlock)?
(A) Increase the amount of available resources.
(B) Increase the number of resource types.
(C) Increase the Max for one process.
(D) Release the allocated but no longer used resources of one process.
登入後即可作答並保存紀錄。
核心觀念
銀行家演算法以「安全狀態」判斷目前是否存在一種程序完成順序,使每個程序都能取得剩餘所需資源、完成執行並釋放資源。令:
安全性檢查會反覆尋找滿足 的程序;找到後,將該程序完成並釋放的資源加入 。若所有程序都能完成,狀態安全。
解題方法
題目已知系統目前安全,逐項判斷改變是否會減少可供安全性檢查使用的資源,或增加程序尚需資源。增加可用資源、釋放已配置資源都不會讓原本存在的安全完成順序失效;增加最大需求或新增資源種類則沒有這項保證。
選項分析
第 5 題4 分
Given the following code with fork():
#include <stdio.h>
#include <unistd.h>
void main()
{
pid_t pid;
int a;
int i;
a = 5;
for (i = 0; i < 2; i++) {
pid = fork();
if (pid < 0) {
return;
} else if (pid == 0) {
a = 2;
printf("%d\n", a);
} else {
a--;
printf("%d\n", a);
}
}
}
Which of the following statements are correct?
(A) The sum of all output numbers is larger than .
(B) At least one of the output numbers is smaller than zero.
(C) Besides the primary process, at most two additional processes are created.
(D) None of the above statement is correct.
登入後即可作答並保存紀錄。
核心觀念
fork() 會建立子行程,並讓父、子行程各自從 fork() 之後繼續執行。父行程中 fork() 的回傳值大於 ;子行程中的回傳值為 。兩個行程各自持有變數 a、i 的副本,因此其中一方修改變數,不會改變另一方的值。
解題方法
逐輪追蹤行程數與各行程的 a 值。每個行程在每輪都會執行一次 fork(),父行程將自己的 a 減 並輸出;子行程將自己的 a 設為 並輸出。
以下依考題慣例,假設每次 printf 都各輸出一次;實際執行時,若標準輸出採全緩衝且 fork() 前尚未清空緩衝區,輸出內容可能因緩衝區被複製而重複。
| 輪次 | 執行該輪前的行程及 a | 父行程輸出 | 子行程輸出 |
|---|---|---|---|
| 第 1 輪 | 主行程:a = 5 | ||
| 第 2 輪 | 第一輪父行程:a = 4 | ||
| 第 2 輪 | 第一輪子行程:a = 2 |
因此,所有輸出數字為 ,總和為
第 6 題4 分
Which one(s) of the following statements regarding virtual memory are true?
(A) Logical memory space can be much larger than physical memory space.
(B) Swapped-out pages are stored in a disk partition or a pre-allocated system file.
(C) When a page is replaced, the page must be written back to the swap space regardless of whether it has been modified or not.
(D) When a page fault occurs, the running process is trapped into the operating system kernel.
登入後即可作答並保存紀錄。
核心觀念
虛擬記憶體(virtual memory)讓行程使用的邏輯位址空間大於實際可用的實體記憶體,並以磁碟作為部分頁面的後備儲存空間。頁面不在主記憶體時,存取該頁會引發缺頁例外(page fault),由作業系統核心處理。
頁面被換出時,是否要寫回磁碟,取決於它是否曾被修改:乾淨頁面可由原本的檔案或其他後備來源重新載入;髒頁面則須先寫回,才能保留修改內容。
解題方法
逐項判斷虛擬記憶體的基本特性:位址空間能否超過實體記憶體、換出頁面的存放位置、頁面替換時的寫回條件,以及缺頁時的控制流程。
選項分析
(A) 正確。 邏輯記憶體空間可以遠大於實體記憶體空間。作業系統將虛擬位址映射至實體頁框,並可把暫時未使用的頁面放在磁碟上,因此不必將行程的全部位址空間同時載入主記憶體。
第 7 題4 分
Consider three typical disk scheduling algorithms: First-Come First-Serve (FCFS), Short-Seek-Time First (SSTF), and SCAN (the elevator algorithm). Which one(s) of the following statements are true?
(A) FCFS never starves a disk I/O request.
(B) The variation of disk I/O latencies under SSTF can be very high.
(C) SCAN is optimal in terms of total seek distance.
(D) Disk scheduling significantly improves disk I/O performance on conventional hard drives, but not on RAM disks.
登入後即可作答並保存紀錄。
核心觀念
磁碟排程決定多筆 I/O 請求的服務順序。傳統硬碟的存取時間包含尋道時間與旋轉延遲,因此排程順序會影響磁頭移動距離與等待時間。
- FCFS(先到先服務):依請求抵達順序處理。
- SSTF(最短尋道時間優先):每次選擇距離目前磁頭位置最近的請求。
- SCAN(電梯演算法):磁頭沿固定方向移動,依序服務沿途請求,到端點後再反向移動。
判斷時要分清楚三件事:是否會飢餓、等待時間是否穩定,以及是否能使總尋道距離最短。
解題方法
逐項檢查各演算法的性質:
- FCFS 不依距離改變請求次序,先到的請求不會被後到的請求插隊。
- SSTF 偏好鄰近請求,遠端請求可能長期等候,造成等待時間差異很大。
- SCAN 以固定方向掃描改善服務公平性,但其目標不是保證全域最小總尋道距離。
- 排程對傳統硬碟有助於減少機械移動;RAM 磁碟沒有磁頭尋道與旋轉延遲,排程帶來的效益有限。
選項分析
第 8 題4 分
Consider the following directory listing information, which is produced using an ls -l command in a Linux console. The fields of each line stand for, in order: access permission, hard link count, owner name, owner group, file size in bytes, time stamp, and file name. Which one(s) of the following statements are true?
-rw-rw-r-- 1 andy staff 31219 Sep 3 08:30 intro.ps
drwx------ 5 andy staff 512 Jul 8 09:33 private/
drwxrwxr-x 2 andy staff 512 Jul 8 09:35 doc/
drwxrwx--- 2 andy student 512 Aug 3 14:13 student-proj/
-rw-r--r-- 1 andy staff 9423 Feb 24 20:03 program.c
-rwxr-xr-x 1 andy staff 20471 Feb 24 20:03 program
drwx--x--x 4 andy faculty 512 Jul 31 10:31 lib/
drwx------ 3 andy staff 1024 Aug 29 06:52 mail/
drwxrwxrwx 3 andy staff 512 Jul 8 09:35 test/
lrwxrwxrwx 1 andy staff 9 Dec 21 16:51 t -> program.c
(A) There are sub-directories in the directory “lib”.
(B) The file “t” is a hard link.
(C) The file “program” can be executed by users in the group “staff”.
(D) The amount of storage space allocated to file “intro.ps” is exactly bytes.
登入後即可作答並保存紀錄。
核心觀念
ls -l 權限欄第一個字元表示檔案類型:- 是一般檔案、d 是目錄、l 是符號連結。後續九個字元分別表示擁有者、群組及其他使用者的讀取(r)、寫入(w)、執行(x)權限。
目錄列出的硬連結數通常等於 該目錄內的子目錄數:兩個基本連結分別來自目錄本身的 . 與其父目錄中的目錄項目;每個子目錄的 .. 會再增加一個連結。
解題方法
逐項對照目錄型態、硬連結數、群組權限及檔案大小欄位。特別注意:一般檔案列出的大小是檔案長度,不能直接視為實際配置的磁碟空間。
選項分析
(A) 錯誤。 lib/ 的硬連結數是 。依目錄硬連結數的計算方式,子目錄數為:
因此 lib 有 個子目錄,不是 個。
第 9 題4 分
Regarding page table designs, which one(s) of the following statements are true?
(A) In inverted page tables, a table entry contains a page number and a process ID.
(B) In hash-based page tables, if a hash bucket is associated with a linked list for collision handling, each element of the linked list contains a frame number plus a page number.
(C) In multilevel page tables, an inner page table entry contains a frame number.
(D) With multilevel page tables, the more levels a page table has, the higher the TLB miss penalty will be.
登入後即可作答並保存紀錄。
核心觀念
本題比較四種頁表設計,以及 TLB 未命中時的頁表查找成本。
- 倒置頁表:全系統通常只設一張表,每個實體頁框對應一個表項。表項記錄目前使用該頁框的程序識別碼與虛擬頁號,另可包含保護位元等資訊。
- 雜湊式頁表:以虛擬頁號(通常連同程序識別碼)計算雜湊值,找到對應的桶;若發生碰撞,沿著鏈結串列逐項比對。表項記錄虛擬頁號及其對應的實體頁框號。
- 多層頁表:外層頁表項指向下一層頁表;最內層頁表項才記錄虛擬頁面所對應的實體頁框號。
- TLB 未命中:處理器必須查詢記憶體中的頁表。若頁表有 層,硬體頁表遍歷通常需依序讀取各層表項,因此層數增加會提高頁表遍歷成本。
解題方法
逐項檢查各敘述描述的表項內容,以及多層頁表的查找路徑。特別要區分「倒置頁表以實體頁框為索引」與「一般頁表以虛擬頁號查找」;多層頁表則要分清外層表項是指向下一層頁表,最內層表項才是指向實體頁框。
選項分析
第 10 題4 分
Consider three typical file allocation schemes: contiguous allocation, linked allocation (link-list allocation), indexed allocation, and extent allocation. Which one(s) of the following statements are true?
(A) Contiguous allocation offers the best read and write performance for large files.
(B) Files are prone to file fragmentation with the linked allocation scheme.
(C) Random file access with indexed allocation in general is faster than with linked allocation.
(D) File defragmentation is not necessary for extent allocation.
登入後即可作答並保存紀錄。
核心觀念
本題比較四種檔案配置方式的磁碟區塊排列與存取特性:
- 連續配置:檔案占用一段連續磁碟區塊,適合連續讀寫,也能直接定位檔案內任一區塊。
- 鏈結配置:每個區塊指向下一個區塊,檔案區塊可分散在磁碟各處。它不會產生「外部碎裂」,但檔案本身可能不連續,讀取時也容易增加磁頭移動。
- 索引配置:以索引區塊記錄檔案各資料區塊的位置,支援直接存取;隨機存取通常比沿鏈結逐區塊尋找快。
- 延伸區段配置:檔案由一個或多個連續區段組成。它兼有連續配置的區段區域性與彈性,但檔案仍可能散布在多個區段。
解題方法
依序判斷每個選項描述的效能或碎裂性質。判斷碎裂時,要區分「外部碎裂」與「檔案區塊不連續」:鏈結配置沒有外部碎裂,卻可能讓同一檔案的區塊散落在磁碟不同位置。
選項分析
第 11 題4 分
Consider CPU performance. Which of the following statements are true?
(A) If you want to decrease the response time of a task, you have to promote your PC by increasing the number of CPUs of your PC. Assume the old CPU and new CPUs in your PC are of the same type and have the same specification.
(B) If we run two programs on two CPUs of different ISAs to perform the same task, the CPU with smaller CPI must have less CPU time than the other CPU.
(C) The CPU time of performing a program on a CPU is determined by the instruction count, the CPI, and the clock rate of the CPU.
(D) Using Millions of Instructions Per Second (MIPS) as a metric to measure CPU performance is not fair since MIPS does not consider the differences in the complexity among instructions.
登入後即可作答並保存紀錄。
核心觀念
CPU 執行時間由下式決定:
其中:
- Instruction Count(指令數):程式執行時所需的指令總數。
- CPI(Cycles Per Instruction):平均每條指令需要的時脈週期數。
- Clock Rate(時脈頻率):每秒可執行的時脈週期數;頻率越高,單一週期時間越短。
比較處理器效能時,不能只看其中一項,必須考慮完成相同工作所需的指令數、平均 CPI 與時脈頻率。
解題方法
逐一檢查選項是否符合 CPU 執行時間公式,並區分單一工作的回應時間與多項工作的整體吞吐量。判斷效能指標時,也要確認它是否能公平比較不同指令集架構(ISA)的處理器。
選項分析
(A) 錯誤。 增加 CPU 數量不保證單一工作的回應時間會縮短。只有當工作可有效平行化,且程式能利用多個 CPU 同時執行時,增加 CPU 才可能降低回應時間。若工作只能由單一 CPU 執行,增加其他 CPU 通常不會讓該工作的執行時間變短。增加 CPU 數量較直接的效果是提高多項工作同時執行的能力,改善整體吞吐量;
第 12 題4 分
Consider the design principles of MIPS CPU. Which of the following statements are true?
(A) The add instruction has a fixed number of operands, i.e., , and does not have more operands because simplicity favors regularity, which also enables higher performance at lower cost.
(B) MIPS has a small group of registers rather than a large and complex group of registers to form a register file, which shows the principle of favoring regularity by simplicity.
(C) The immediate operand in immediate arithmetic instructions follows the principle of making the common case fast.
(D) Several types of instruction formats in MIPS reveal the principle of the demands of compromises for good design.
登入後即可作答並保存紀錄。
核心觀念
本題考查 MIPS 的四項設計原則:
- 簡單即規則:採用規則一致的指令格式與操作方式,能簡化硬體設計。
- 越小越快:資源規模較小,通常較容易快速存取;暫存器數量也受此原則影響。
- 加速常見情況:常見操作應設計得有效率,例如讓算術指令能直接使用立即數。
- 良好設計需要折衷:不同需求常彼此牽制,設計須在規則性、彈性與硬體成本間取捨。
解題方法
逐項辨認選項描述的設計做法,再核對它引用的原則是否恰當。特別要區分「簡單即規則」與「越小越快」:指令格式一致屬於前者;控制暫存器等資源的規模則主要屬於後者。
選項分析
(A) 正確。
MIPS 的 add 指令固定使用三個運算元,例如 add rd, rs, rt,分別指定目的暫存器與兩個來源暫存器。
第 13 題4 分
Consider computer arithmetic and CPU design. Which of the following statements are true?
(A) For floating-point numbers, the sticky bit is used to promote the accuracy of rounding a floating-point number, where increasing the number of used sticky bits can improve the accuracy.
(B) The merit of the non-restoring division algorithm (NRDA) over the restoring division algorithm (RDA) is that the NRDA requires one less addition than the RDA if the subtraction of the divisor register from the remainder register (dividend) yields a negative number.
(C) The addition of two integers represented by 2’s complement of different signs does not produce an overflow.
(D) The following codes perform the overflow check for the addition of two unsigned integers stored in $t1 and $t2:
addu $t0, $t1, $t2; nor $t3, $t1, $zero; sltu $t3, $t3, $t2; bne $t3, $zero, Overflow
登入後即可作答並保存紀錄。
核心觀念
本題考查浮點數捨入、非恢復式除法、二補數溢位,以及 MIPS 對無號加法溢位的判斷。
- Sticky bit(黏滯位):把捨去部分中所有位元做 OR;只要其中任一位是 1,sticky bit 就是 1。它協助判斷捨入時是否有非零尾數被捨去。
- Restoring division(恢復式除法):試減除數後若餘數為負,便加回除數以恢復原餘數。
- Non-restoring division(非恢復式除法):試減後若餘數為負,先保留負值;下一輪依餘數正負決定加或減除數。
- 二補數加法溢位:兩個同號數相加,結果符號若改變便可能溢位;異號數相加則不會發生有號溢位。
- 無號加法溢位:對 位元而言,兩數相加超過 就會產生進位。
解題方法
逐項檢查敘述所用的定義。除法選項聚焦於「試減結果為負時是否需要立即恢復」;二補數選項利用正負號判斷;MIPS 程式則將條件改寫成兩個無號整數之和是否超過最大值。
選項分析
(A) 錯誤。 Sticky bit 是把所有被捨去位元濃縮成一個位元,表示被捨去部分是否非零。增加 sticky bit 的數量不會提升捨入精度;精度取決於保留的有效位元及捨入規則。Guard bit、round bit 與 sticky bit 是不同用途的資訊。
(B) 正確。 恢復式除法在試減除數後若得到負餘數,必須再加回除數,執行一次恢復加法。
第 14 題4 分
The left field of the table below lists a code sequence of instructions for the strcpy function. This code sequence is incomplete. Choose a right update from (A) or (B) to make this code sequence complete. The right field of the table lists the MIPS instruction format. Based on the incomplete code sequence on the table (not the updated code sequence), compute the values of ADR1 and ADR2. Which statements of (C) and (D) are correct?
| Instruction | Instruction address | Instruction format |
|---|---|---|
strcpy: (1) add $s0, $zero, $zero | ||
L1: (2) add $t1, $s0, $a1 | ||
(3) lbu $t2, 0($t1) | ||
(4) add $t3, $s0, $a0 | ||
(5) sb $t2, 0($t3) | ||
(6) beq $t2, $zero, L2 | ADR1 | |
(7) addi $s0, $s0, 1 | Immediate field: | |
(8) j L1 | ADR2 | |
(9) L2: lw $s0, 0($sp) | ||
(10) jal PRT_MSG | ADR3 | |
(11) jr $ra | Format field values: , , , |
(A) Add the following instructions before instruction (1): addi $sp, $sp, -8; sw $s0, 0($sp); sw $ra, 4($sp). Add the following instructions before instruction (11): lw $ra, 4($sp); addi $sp, $sp, 8.
(B) Add the following instructions before instruction (1): addi $sp, $sp, -4; sw $s0, 0($sp). Add the following instructions before instruction (11): addi $sp, $sp, 4.
(C) ADR1 .
(D) ADR2 .
登入後即可作答並保存紀錄。
核心觀念
本題考查 MIPS 的函式呼叫慣例、分支指令的位移量,以及跳躍指令的目標欄位。
-
$s0是被呼叫函式應保留的暫存器;函式若要使用,須先存入堆疊,返回前再還原。 -
jal會把返回位址寫入$ra。若函式還要用jr $ra返回呼叫端,就必須在呼叫其他函式前保存原本的$ra,並在返回前還原。 -
beq使用 I 型格式,其立即數是相對於下一條指令的字數位移: -
j使用 J 型格式,目標欄位存放目標位址除以 的結果;完整跳躍位址由指令中的目標欄位及PC+4的高位元組合而成。
解題方法
這段 strcpy 程式以 $s0 作為字元索引,逐字元從來源字串載入到目的字串,遇到空字元後跳至 L2。因為程式會使用 $s0,也會呼叫 PRT_MSG,因此函式序言與結尾須保存並還原 $s0、$ra。
選項 (A) 配置 位元組堆疊空間,分別保存 $s0 與 $ra;結尾還原 $ra、釋放堆疊空間,符合函式呼叫需求。選項 (B) 沒有保存 $ra,而 jal PRT_MSG 會改寫 $ra,因此最後的 jr $ra 無法使用原本的呼叫端返回位址。
第 15 題4 分
Consider the addition of two normalized binary floating-point numbers, (input 1) and (input 2). The significant bit of a normalized binary floating-point number is hidden and exponent bias is not used. The accuracy of a binary floating-point number is to the fifth binary place, i.e., each fraction register is -bit long. The hardware design adopts the scheme of two additional bits, i.e., guard bit and round bit, to improve accuracy. Which of the following statements are true?
🖼️【此處有附圖,見下方】
(A) ; ; .
(B) (implying a right shift by two bits); .
(C) no operation; ; no operation.
(D) triggers rounding hardware to produce a carry-in of to the fifth binary place; the final addition result is .
登入後即可作答並保存紀錄。
核心觀念
浮點數相加時,先比較指數,將指數較小的有效數右移,使兩數指數相同,再相加。結果若仍為正規化形式,指數不需調整;最後利用 guard bit 與 round bit 決定是否進位至保留的五位小數。
解題方法
圖中資料路徑包含指數比較、較小有效數右移、大小有效數選擇、加法、正規化與捨入電路;控制訊號 至 分別控制資料選擇及後續運算。本題的輸入 2 指數較大,兩數指數相差 ,因此將輸入 1 的有效數右移兩位後相加。
保留五位小數並附上 guard bit、round bit,輸入 1 的有效數可寫為:
右移兩位並與輸入 2 對齊後,兩數為:
相加得:
保留五位小數為 ,後兩位 guard bit 與 round bit 都是 。因此捨入時將保留部分的最低位加 :
第 16 題4 分
A sequential CPU can be implemented in (i) single-(machine-)cycle-per-instruction, (ii) multiple-cycles-per-instruction, and (iii) pipelined fashions. For any particular machine program:
(A) In (i), since every instruction must be completed in only one machine cycle, and since for any particular type of hardware circuits, if any instruction may possibly need to use it for any number of times, say, , then copies of it have to be in physical existence (meaning the hardware cost is at its maximum), hence the execution speed is the fastest among the three.
(B) In (ii), all instructions are allowed to repetitively reuse any piece of hardware for any number of times; hence the required function units in this design can be the minimum.
(C) In (iii), the CPU needs to execute many individual instructions simultaneously, with each instruction being in their respectively different pipe stages. Hence at the end of each pipe stage, a pipestage latch must be provided to keep the processing results of that pipestage. While pipestage latches in the (ii) design are not necessary (except for those storing the final outcomes of that instruction), since only one instruction will be executed in those cycles at any instance of time.
(D) Both (ii) and (iii) design fashions have the same shortcoming of having to slow the machine clock down to allow for the slowest pipestage to be able to complete, making other faster stages wait without further productivity, hence they are very rarely adopted in many successful commercial products.
登入後即可作答並保存紀錄。
核心觀念
本題比較三種 CPU 實作方式的時脈週期、每指令所需週期數,以及硬體資源重複使用方式:
- 單週期(single-cycle):每條指令都在一個時脈週期內完成。時脈週期必須足以容納最慢指令的整條資料路徑。
- 多週期(multi-cycle):一條指令分成多個時脈週期完成。不同指令可共用功能單元,並在不同週期重複使用。
- 管線化(pipelined):將指令拆成多個階段,讓不同指令同時處於不同階段。階段間需要管線暫存器保存中間結果。
衡量 CPU 執行時間的基本公式為:
因此,不能只比較硬體複製數或時脈頻率來判斷哪種設計最快;還要同時考慮 CPI 與時脈週期。
解題方法
逐項檢查敘述是否符合各設計的執行方式:
- 單週期把整條指令路徑塞進一個週期,週期通常較長。
- 多週期將工作分散到多個週期,能讓不同週期共用功能單元。
- 管線化在階段之間保存中間結果;多週期也需要暫存器保存跨週期的中間結果。
- 多週期與管線化的週期時間受單一階段的延遲限制,但這不代表它們很少用於商用處理器。
選項分析
(A) 錯誤
單週期設計要求每條指令在一個週期內完成,所以時脈週期要涵蓋最長的指令路徑。這使週期時間通常較長。
即使一個週期完成一條指令,這也不代表執行速度最快。單週期的 CPI 為 ,但其時脈週期可能遠長於多週期或管線化設計。
第 17 題4 分
We talk about the famous pipeline hazards in pipelined processor design, in alphabetical order:
(A) For Control hazards, no matter how we restrict the branch condition complexity, how early we calculate the target (instruction) address and/or resolve the branch condition, how hard we predict the branch outcome and/or target address, pipeline stalls due to this kind of hazards are still unavoidable.
(B) For Data hazards, we mean the pipeline stalls or errors which may be caused by immature (not-yet-completed) read-after-write, arithmetic overflow, divide-by-zero, etc.
(C) For Memory hazards, we mean the pipeline stalls or errors which may be caused by data inconsistencies among different memory hierarchies, cache misses, page faults, errors occurring in data storage/transmission, etc.
(D) For Structural hazards, it can be dealt with by properly scheduling the uses of the contended hardware circuit and stalling the awaiting instructions, such that, for example, no matter how many instructions in the pipeline want to perform an addition at whenever for whatever number of times, only one hardware adder will suffice.
登入後即可作答並保存紀錄。
核心觀念
管線危障(pipeline hazards)是指指令在管線中重疊執行時,因控制流程、資料相依或硬體資源競爭,使指令無法依預定時序前進。常見三類是:
- 控制危障(control hazard):分支或跳躍指令改變控制流程,後續應取用的指令尚未確定。
- 資料危障(data hazard):指令之間有資料相依,所需資料尚未產生或寫回。常見相依包括讀後寫(RAW)、寫後讀(WAR)、寫後寫(WAW)。
- 結構危障(structural hazard):多個指令同時要求使用同一個硬體資源,資源無法同時服務所有請求。
題目中的「記憶體危障(memory hazard)」不是上述三種常見基本分類之一;在特定脈絡下,它通常指載入與儲存指令間的記憶體相依或存取順序衝突。
解題方法
逐一檢查選項是否符合危障的定義。特別注意:某事件會造成停滯,不代表它本身就是某一類管線危障;而危障也不必然只能以停滯解決。排程、預測、轉送或增加硬體資源,都可能降低或消除停滯。
選項分析
(A) 錯誤。 控制危障不代表管線停滯必然無法避免。若分支結果能在取指前得知,或分支預測正確,處理器可持續取指而不必停滯;推測執行也能隱藏部分分支延遲。
第 18 題4 分
In a sequence of instructions in a user program, let instructions be numbered , where instruction is later than instruction in execution order. Assume that instruction incurs an integer arithmetic overflow. Which of the following can be a proper rationale?
(A) The processor completes instructions up to , triggers exception handling to calculate the true result of instruction and place the result properly; then the exception returns and resumes user program execution starting from instruction .
(B) The processor completes instructions up to , triggers exception handling to signal to the user that such an error occurred; then the exception halts the user program.
(C) The processor ignores this overflow and simply continues the user program execution.
(D) Such an arithmetic overflow should not occur if this user program is generated by a proper and commercially authorized compiler and assembler, since such compilers and assemblers are designed to produce correct and high quality code.
登入後即可作答並保存紀錄。
核心觀念
本題考查整數溢位的處理方式,以及例外處理後如何恢復執行。整數運算超出可表示範圍時,處理器與系統軟體的反應取決於指令集架構(ISA)和作業系統的設計,並非所有系統都採用同一種策略。
精確例外(precise exception)要求:例外發生時,例外前的指令已完成,發生例外的指令及其後指令尚未完成。例外處理常式可依處理方式修正狀態,再從適當的指令位置恢復;也可以通知使用者並終止程式。
解題方法
依序判斷各選項描述的處理方式是否能由合理的處理器或系統設計實現:
- 若處理常式代替處理器完成溢位指令,恢復位置可設為 。
- 若系統將溢位視為致命錯誤,可通知使用者並終止程式。
- 若架構不因溢位觸發例外,處理器可依該架構定義的整數運算規則繼續執行。
- 編譯器與組譯器品質無法保證程式執行時永不發生溢位。
選項分析
第 19 題4 分
Upon receiving a read/write request, a cache controller will partition the received memory address (assuming a byte-addressable address, since memories are byte-addressable by default) into three fields: tag, index, and byte-offset.
(A) The size of a direct-mapped cache can be determined by knowing only the size of the tag field.
(B) The size of a set-associative cache cannot be determined based on the sizes of the three fields.
(C) For any reasonable cache, any of these three fields should not contain zero bit.
(D) If the same amount of memory is to be used for the fully-associative, set-associative, or direct-mapped caches, the tag field of the fully-associative cache is the largest, and that of the direct-mapped cache is the smallest.
登入後即可作答並保存紀錄。
核心觀念
位址總長度為 位元,位址欄位分成標籤(tag)、索引(index)與位元組位移(byte-offset):
其中 、、 分別是三個欄位的位元數。位元組位移決定每個區塊的大小:
索引決定快取組數:
若每組有 路,則快取資料容量為:
直接映射快取只有一個路,因此 ;全相聯快取只有一組,因此 。
解題方法
逐項檢查選項:先由位址總長度與標籤位元數推回索引與位移的總位元數,再確認容量公式是否還需要未提供的參數;比較不同映射方式時,則固定資料容量與區塊大小,檢查索引位元數如何影響標籤位元數。
選項分析
- (A) 正確。 直接映射快取的 ,容量為 bytes。因為 ,所以:
第 20 題4 分
The designs of cache memory and virtual memory are based on the same principle. While the implementations of these memories are very different in some ways:
(A) The principle mentioned above is Amdahl’s Law.
(B) The counterpart of a cache block (or line) in virtual memory design is called a page (or page frame).
(C) During a cache miss or a page fault, the CPU will typically stand still and wait until the miss or fault is handled.
(D) Just like in cache designs, both write-through and write-back policies are commonly used in a typical virtual memory system.
登入後即可作答並保存紀錄。
核心觀念
快取記憶體與虛擬記憶體都利用區域性原理:程式近期使用過的資料,或與其相鄰的資料,短時間內再次被存取的機率較高。快取主要利用時間與空間區域性;虛擬記憶體則常以需求分頁載入近期需要的頁面。
兩者都有「較小、較快的儲存層」與「較大、較慢的儲存層」之間的對應關係:
- 快取以區塊(block 或 cache line)為搬移單位。
- 虛擬記憶體以頁(page)為搬移單位;頁載入實體記憶體後,所在位置稱為頁框(page frame)。
解題方法
先找出兩種記憶體設計共用的原理,再檢查各選項是否正確描述區塊對應、未命中處理,以及寫入策略。注意「CPU 是否等待」與「典型虛擬記憶體的寫入政策」都不能直接照搬快取的描述。
選項分析
(A) 錯誤。
共同原理是區域性原理,不是 Amdahl 定律。Amdahl 定律用來分析系統中加速部分對整體效能的影響,與快取、虛擬記憶體分層設計所依據的存取區域性不同。
Consider the following set of processes performed on a single-core processor. The processes arrive at the following arrival times, with the length of the CPU burst given in the table:
| Process | Arrival time | CPU burst time |
|---|---|---|
Please calculate the average turnaround time under round-robin scheduling with time quantum . Assume ().
第 21 題5 分
What is ?
(A)
(B)
(C)
(D)
登入後即可作答並保存紀錄。
核心觀念
Round-robin(循環先來先服務)排程會依就緒佇列順序輪流分配 CPU,每個行程每次最多執行一個時間量子。本題時間量子為 ;若行程在時間量子用完前完成,就立即釋出 CPU。
周轉時間定義為:
平均周轉時間 為所有行程周轉時間的平均值。
解題方法
依照就緒佇列順序排出執行時間軸:
| 時間區間 | 執行行程 | 執行後狀態 |
|---|---|---|
| 剩餘 | ||
| 完成 | ||
| 完成 | ||
| 完成 | ||
| 剩餘 | ||
| 完成 |
在 執行的 期間, 到達; 執行期間,、 依序到達。 在時間 用完量子後,排隊順序為 ,因此接著由 執行。
各行程周轉時間如下:
| 行程 | 到達時間 | 完成時間 | 周轉時間 |
|---|---|---|---|
第 22 題5 分
What is ?
(A)
(B)
(C)
(D)
登入後即可作答並保存紀錄。
核心觀念
Round-robin(循環式)排程會依就緒佇列順序輪流分配 CPU,每次最多執行一個時間量子。本題時間量子為 ;若程序在時間量子用完時尚未完成,就把它放回就緒佇列尾端。
周轉時間定義為:
平均周轉時間 為所有程序周轉時間的算術平均。
解題方法
依照程序到達時間與就緒佇列順序,逐段排出 CPU 執行時間:
| 時間區間 | 執行程序 | 執行後狀態 |
|---|---|---|
| – | 剩餘 ,回到佇列尾端 | |
| – | 完成 | |
| – | 完成 | |
| – | 完成 | |
| – | 剩餘 ,回到佇列尾端 | |
| – | 完成 |
各程序的周轉時間如下:
第 23 題5 分
What is ?
(A)
(B)
(C)
(D)
登入後即可作答並保存紀錄。
核心觀念
Round-robin(輪轉)排程讓就緒佇列中的行程依序執行,每次最多執行一個時間量子(本題為 )。量子用完仍未完成的行程排回佇列尾端;在它被搶下之前就已到達的行程,排在它前面。
解題方法
逐段追蹤就緒佇列:
| 時間區間 | 執行行程 | 事件與佇列(執行後) |
|---|---|---|
| – | 於 到達; 剩 ,排在 之後 → 佇列 | |
| – | ()、()到達; 完成 → 佇列 | |
| – | 完成 | |
| – | 完成 | |
| – | 剩 ,佇列中沒有其他行程 | |
| – | 完成 |
| 行程 | 到達 | 完成 | 周轉時間 |
|---|---|---|---|
第 24 題5 分
What is ?
(A)
(B)
(C)
(D)
登入後即可作答並保存紀錄。
核心觀念
Round-robin(RR,循環排程)依就緒佇列順序輪流分配 CPU,每次最多執行一個時間量子 。若行程在時間量子用完前完成,就立即釋放 CPU;若尚未完成,則回到就緒佇列尾端。
行程的周轉時間為:
平均周轉時間 是所有行程周轉時間的算術平均。
解題方法
依 RR 規則追蹤執行順序;執行中的行程若未完成,新抵達的行程會依抵達時間加入就緒佇列。
| 時間區間 | 執行行程 | 說明 |
|---|---|---|
| – | 執行 單位,剩餘 ; 已於時間 抵達 | |
| – | 執行 單位並完成;期間 、 依序抵達 | |
| – | 執行剩餘 單位並完成 | |
| – | 執行 單位並完成 | |
| – | 執行 單位,剩餘 | |
| – | 執行剩餘 單位並完成 |
Consider the following system parameters of a microprocessor: a logical address is of bits, and a physical address is of bits. Let the page size be KB.
第 25 題5 分
What is the largest possible number of frames? ( stands for the -th power of . For example, .)
(A)
(B)
(C)
(D) None of the above.
登入後即可作答並保存紀錄。
核心觀念
實體記憶體會切分成固定大小的頁框(frame),每個頁框的大小等於頁面大小(page size)。因此,頁框數量為:
實體位址為 位元,表示可定址的實體位址空間共有 個位元組。頁面大小為 個位元組。
解題方法
將實體位址空間大小除以頁面大小:
邏輯位址為 位元,影響的是虛擬位址空間與虛擬頁數;計算實體記憶體最多能有多少頁框,應使用實體位址位元數。
選項分析
第 26 題5 分
Consider a simple 1-level page table design. How many entries are there in a page table?
(A)
(B)
(C)
(D) None of the above.
登入後即可作答並保存紀錄。
核心觀念
單層頁表中,每個虛擬頁面對應一個頁表項目,因此頁表項目數等於邏輯位址空間可分成的頁數:
解題方法
邏輯位址為 位元,所以邏輯位址空間共有 個位址。頁面大小為 位元組,因此每個頁面包含 個位址。
因此,完整的單層頁表需要 個項目。
選項分析
- (A) :錯誤。 是頁面大小的位元組數,也就是頁內位移所需的位元數;它不是頁表項目數。
- **(B) :錯誤。
第 27 題5 分
At least how many bits are necessary in a page-table entry?
(A)
(B)
(C)
(D) None of the above.
登入後即可作答並保存紀錄。
核心觀念
頁表項目(page-table entry, PTE)至少要能指出該虛擬頁面對應的實體頁框(page frame)。實體位址可分成:
頁內位移的位元數由頁面大小決定;其餘實體位址位元用來表示頁框編號。
解題方法
頁面大小為 bytes,因此頁內位移需要 位元。
實體位址共 位元,扣除頁內位移後,頁框編號需要:
The following two C and MIPS assembly programs perform the same task.
Program 1:
clear1(int array[], int size) {
int i;
for (i = 0; i < size; i += 1)
array[i] = 0;
}
move $t0, ARY1
loop1: sll $t1, $t0, 2
add $t2, $a0, $t1
sw $zero, 0($t2)
addi $t0, $t0, ARY2
slt $t3, $t0, ARY3
bne $t3, $zero, loop1
Program 2:
clear2(int *array, int size) {
int *p;
for (p = &array[0]; p < &array[size]; p = p + 1)
*p = 0;
}
move $t0, PTR1
sll $t1, $a1, 2
add $t2, $a0, $t1
loop2: sw $zero, 0($t0)
addi $t0, $t0, PTR2
slt $t3, $t0, PTR3
bne $t3, $zero, loop2
第 28 題5 分
The ARY1 and PTR1 should be:
(A) ARY1 = $zero; PTR1 = $a0
(B) ARY1 = $a0; PTR1 = $zero
(C) ARY1 = $a0; PTR1 = $a0
(D) ARY1 = $zero; PTR1 = $zero
登入後即可作答並保存紀錄。
核心觀念
本題考查 C 陣列索引與指標運算的對應,以及 MIPS 中的位址計算。
在 32 位元 MIPS 中,int 佔 4 位元組:
- 陣列索引
array[i]的位址為array起始位址加上 。 - 指標
p加 1,位址增加 4 位元組。 - 函式的第一、第二個整數參數分別放在
$a0、$a1。
解題方法
Program 1:以索引走訪陣列
$t0 儲存索引 i。每輪先將索引乘以 4,再加上陣列起始位址 $a0,取得 array[i] 的位址:
sll $t1, $t0, 2 # $t1 = i * 4
add $t2, $a0, $t1 # $t2 = &array[i]
sw $zero, 0($t2) # array[i] = 0
i 從 0 開始,因此 ARY1 應是 $zero。每輪索引增加 1,再與 size 比較,所以 ARY2 為 1、ARY3 為 $a1。
Program 2:以指標走訪陣列
$t0 儲存指標 p。迴圈要從 &array[0] 開始,因此 PTR1 應是 $a0。結束位址由 $a0 + 4 \times size 算出;每輪指標增加 4 位元組,再與結束位址比較:
第 29 題5 分
The ARY2 and PTR2 should be:
(A) ARY2 ; PTR2
(B) ARY2 ; PTR2
(C) ARY2 ; PTR2
(D) ARY2 ; PTR2
登入後即可作答並保存紀錄。
核心觀念
本題考查 C 語言陣列索引與指標運算如何對應到 MIPS 位址計算。假設 int 為 4 bytes:
array[i]的位址是陣列起始位址加上 。int *p每次增加 1,位址前進sizeof(int),也就是 4 bytes。
解題方法
Program 1:以索引走訪
$t0 保存索引 i。指令
sll $t1, $t0, 2
將索引左移 2 位,等同乘以 ,因此 $t1 是第 i 個整數的位元組位移量。接著以 $a0 + $t1 算出 array[i] 的位址並清零。
迴圈每次要讓索引增加 1,因此:
addi $t0, $t0, ARY2
中的 ARY2 必須是 1。
Program 2:以指標走訪
$t0 保存指標 p。每次清零目前指向的整數後,指標必須前進一個 int,即 4 bytes:
addi $t0,
第 30 題5 分
The ARY3 and PTR3 should be:
(A) ARY3 = $t2; PTR3 = $a1
(B) ARY3 = $a1; PTR3 = $a1
(C) ARY3 = $t2; PTR3 = $t2
(D) ARY3 = $a1; PTR3 = $t2
登入後即可作答並保存紀錄。
核心觀念
這題考的是 C 語言陣列索引與指標的位址計算,以及 MIPS 中用 slt 判斷迴圈是否繼續。
假設 int 佔 4 位元組:
array[i]的位址是$a0 + 4i。p + 1會讓指標位址增加 4 位元組。&array[size]是陣列尾端的位址,迴圈以它作為不包含在範圍內的終點。
解題方法
Program 1:以整數索引比較
每輪先用 $t0 作為索引,計算元素位址並清零:
sll $t1, $t0, 2 # $t1 = i * 4
add $t2, $a0, $t1 # $t2 = &array[i]
sw $zero, 0($t2)
接著索引加 1,再判斷 i < size:
addi $t0, $t0, 1
slt $t3, $t0, $a1
因此 ARY3 必須是 $a1,也就是 size。
Program 2:以指標比較
$t2 先被設定為陣列尾端位址:
sll $t1, $a1, 2 # $t1 = size * 4
add $t2, $a0, $t1 # $t2 = &array[size]
每輪清零目前 $t0 所指向的元素,接著指標前進一個 int:
Consider cache placement and replacement policies. Assume that a code sequence of instructions, , where is a power of , is to be executed repeatedly for an infinite number of times. Assume further that all instructions are bits long, and the cache lines (or blocks) are also bits long. Hint: To help derive the answers in the following questions, you may let , a small number.
第 31 題5 分
If a direct-mapped cache of size -cache lines is used, the resulting cache hit rate is about:
(A)
(B)
(C)
(D)
登入後即可作答並保存紀錄。
核心觀念
直接對映快取中,每個記憶體區塊只能放在一個固定的快取行。快取有 行時,位址區塊編號相差 的指令會對映到同一行。
題目中每條指令長 位元,快取區塊也長 位元,因此每個區塊恰好放一條指令。指令 到 依序存放,故 與 的區塊編號相差 ,會對映到同一條快取行。
解題方法
用 觀察存取情形:指令序列為 。其中 與 對映到同一行,兩者交替存取時會互相置換,因此每次存取都未命中;、、 各自使用不同的快取行,在快取暖機後都會命中。
一般化後,每次重複執行共有 次指令存取:
- 與 對映至同一行,且在序列中交替出現,兩者都未命中。
- 到 共 條指令,各自對映至不同快取行;暖機後每次都命中。
第 32 題5 分
If a fully associative cache of the same size, with the commonly used LRU (least-recently-used) replacement, is used, the resulting cache hit rate is about:
(A)
(B)
(C)
(D)
登入後即可作答並保存紀錄。
核心觀念
每條快取區塊與每個指令都恰為 位元,因此每條指令剛好佔一個區塊。依題目「同樣大小」的設定,快取可容納 個區塊;程式循環執行時,會依序存取 個不同區塊:
全相聯快取可將任一記憶體區塊放入任一快取列,但 LRU 會淘汰最久未被使用的區塊。當循環存取的工作集合有 個區塊、快取容量只有 個區塊時,區塊再次被存取前,已有另外 個不同區塊被存取,原區塊已被 LRU 淘汰。
解題方法
以題目提示令 ,便有 5 個區塊、4 個快取列。第一次循環存取 到 時,五次皆為失誤;存入 時, 已被淘汰。下一次存取從 開始,又會淘汰下一個即將再次使用的區塊,於是後續存取也持續失誤。
第 33 題5 分
If a fully associative cache of the same size, with the less famous MRU (most-recently-used) replacement, is used, the resulting cache hit rate is about:
(A)
(B)
(C)
(D)
登入後即可作答並保存紀錄。
核心觀念
題目考的是快取替換策略對循環存取的影響。每條快取線可放一條指令,因此快取若可容納 條指令,題目中的循環會依序存取 條不同指令:
MRU(最近使用)策略在快取未滿時直接填入;快取已滿且發生缺失時,則淘汰「最近被使用」的快取區塊。題目假設程式無限次重複執行,因此初次填滿快取的有限次存取不影響長期命中率。
解題方法
因為快取只有 條線,無法同時容納循環中的 條指令。以 為例,快取可放 4 條指令,存取串流為 循環。快取填滿後,連續幾輪的缺失次數會呈現如下模式:
| 循環輪次 | 缺失次數 |
|---|---|
| 第 1 輪 | 1 |
| 第 2 輪 | 1 |
| 第 3 輪 | 1 |
| 第 4 輪 | 2 |
四輪共存取 次,發生 5 次缺失,因此命中率為 。
一般化來看,對 條快取線與 條循環指令,MRU 淘汰會使缺失在各輪間移動;每 輪總共發生 次缺失。