114 年 國立臺灣大學資訊工程學系碩士班《計算機結構與作業系統》

📄 試題原卷 免費註冊後即可對照原始考卷 PDF免費註冊

第 1 題5 分

Which ONE of the following statements is correct for the critical-section problem? Assume there are several concurrent processes, each of which has one critical section. Memory variables shared among these processes are protected by the critical section from concurrent updates.
a. Memory barriers can prevent concurrent updates by enforcing mutual exclusion to the shared memory.
b. Atomic instructions can prevent concurrent updates by completing the load and store instructions to the shared memory.
c. Semaphores can allow more than one process to update the shared memory.
d. Mutex locks acquire the lock before updating the shared memory and release the lock after updating the shared memory when the locks are mandatory to assure mutual exclusion.
e. None of all.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題旨在考驗作業系統(Operating Systems)中臨界區段問題(Critical-Section Problem)​與同步機制(Synchronization Primitives)​的核心觀念與運作機制。

解決臨界區段問題必須同時滿足以下三個條件:

  1. 互斥(Mutual Exclusion):若有行程在臨界區段內執行,則其他行程皆不得進入其臨界區段。
  2. 進展(Progress):若沒有行程在臨界區段內執行,且有行程想要進入,則只有不在剩餘區段(Remainder Section)執行的行程可以參與決定誰下一個進入臨界區段,且此決定不可無限期延遲。
  3. 有限等待(Bounded Waiting):行程提出進入臨界區段的請求後,在該請求被允許之前,其他行程進入臨界區段的次數必須有上限。

為達成互斥並保護共享記憶體變數免於併發更新(Concurrent Updates)導致的競態條件(Race Condition),常見的硬體與軟體同步工具包括:

  • 記憶體屏障(Memory Barriers / Fences):強制記憶體操作的執行順序,避免編譯器或 CPU 亂序執行(Out-of-Order Execution),確保記憶體可見性與一致性。
  • 原子指令(Atomic Instructions):硬體提供的不可中斷操作(如 TestAndSet、CompareAndSwap),作為建立高階同步工具的基礎構造塊(Building Blocks)。
  • 號誌(Semaphores):提供 wait()(或 PP)與 signal()(或 VV)操作。分為二元號誌(Binary Semaphore)與計數號誌(Counting Semaphore)。
  • 互斥鎖(Mutex Locks):專門用於實現互斥鎖定的高階同步機制,擁有者(Owner)在進入臨界區段前取得鎖(Acquire),離開後釋放鎖(Release)。

解題方法

本題為單選題,解題切入點為比對各選項所述同步工具的功能定義與運作邏輯,確認何者能正確描述保護共享記憶體併發更新的互斥機制。

在標準的臨界區段程式結構中:

do {
    // 1. 進入區段 (Entry Section):取得存取權/鎖
    /* acquire lock / wait(mutex) */

    // 2. 臨界區段 (Critical Section):更新共享記憶體
    /* Update shared memory variables */

    // 3. 退出區段 (Exit Section):釋放存取權/鎖
    /* release lock / signal(mutex) */

    // 4. 剩餘區段 (Remainder Section)
    /* Other operations */
} while (true);

當使用鎖定機制保障互斥性時,行程必須在進入臨界區段更新共享記憶體前取得鎖定,並於更新完成離開時釋放鎖定。


選項分析

  • a. 錯誤。
    記憶體屏障(Memory Barriers)的作用是防止指令重排(Instruction Reordering),確保屏障前的記憶體讀寫操作必在屏障後的操作之前完成(Memory Ordering),以保證記憶體可見性。記憶體屏障本身無法提供互斥(Mutual Exclusion)功能,無法單獨防止多個行程同時存取與更新共享記憶體。

  • b. 錯誤。
    原子指令(如 CompareAndSwap、TestAndSet)保證該指令本身(Read-Modify-Write)在執行時為不可中斷的單一硬體操作,常用於實作高階鎖(如 Spinlock)。然而,原子指令本身並非藉由「完成 shared memory 的載入與儲存指令」來防止整個臨界區段的併發更新;若臨界區段包含多條敘述,單靠原子指令無法自動保護整個區段的互斥,仍需配合迴圈與鎖定協定(Locking Protocol)。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 2 題5 分

Which ONE of the following statements is correct for the redundant array of inexpensive disks (RAID)?
a. Reliability of the storage system is increased by the parallelism of the multiple disks.
b. RAID 5 uses the block-interleaved distributed parity check to increase the reliability.
c. If the MTBF of a single drive is 100,000 hours and the mean time to repair is 10 hours, the mean time to data loss of a mirrored drive (two disks) system is 5,700 years.
d. RAID 10 refers to a combination of RAID levels that are stripped first and mirrored later.
e. None of the above.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題旨在考驗對獨立磁碟冗餘陣列(RAID, Redundant Array of Inexpensive Disks)​的定義、各等級(RAID Levels)架構特性、嵌套式 RAID 組合順序,以及儲存系統可靠度(Reliability)與平均資料遺失時間(MTTDL, Mean Time to Data Loss)​的數學推導。

  1. RAID 等級定義:

    • RAID 0:區塊拆分條帶化(Striping),提升效能但無冗餘(No Redundancy)。
    • RAID 1:磁碟鏡像(Mirroring),提供完全備份。
    • RAID 4:區塊級條帶化 + 獨立奇偶校驗碼磁碟(Block-interleaved dedicated parity)。
    • RAID 5:區塊級分散式奇偶校驗碼(Block-interleaved distributed parity),將 Parity 區塊均勻分散於陣列中各磁碟,解決 RAID 4 的 Parity 磁碟寫入瓶頸。
    • RAID 01 vs. RAID 10:嵌套式 RAID 架構。RAID 01 (RAID 0+1) 為先條帶化再鏡像(Striped first, mirrored later);RAID 10 (RAID 1+0) 為先鏡像再條帶化(Mirrored first, striped later)。
  2. 可靠度與 MTTDL 計算:

    • 平行處理(Parallelism)若無容錯機制,會隨磁碟數量增加而降低系統可靠度。
    • 在雙磁碟鏡像系統(RAID 1)中,平均資料遺失時間 MTTDLMTTDL 的公式為:
      MTTDL=MTTFsingle22×MTTRMTTDL = \frac{MTTF_{\text{single}}^2}{2 \times MTTR}
      其中 MTTFsingleMTTF_{\text{single}} 為單一磁碟的平均無故障時間(Mean Time To Failure),MTTRMTTR 為平均修復時間(Mean Time To Repair)。

解題方法

本題為標準的觀念辨析與數值驗算題,解題切入點如下:

  1. 檢視 RAID 各等級之標準語義定義,比對 RAID 5 的區塊層級與 parity 配置方式。
  2. 代入 RAID 1 的 MTTDLMTTDL 算式,進行精確的時間單位換算與數值驗算。
  3. 區分 RAID 10 與 RAID 01 在子陣列組合順序上的結構差異。
  4. 釐清「平行處理(Parallelism)」與「冗餘(Redundancy)」對系統故障率的根本影響。

選項分析

  • a. 錯誤。
    單純多磁碟平行處理(Parallelism,如 RAID 0 的條帶化)僅能提升讀寫效能(Performance),反而會因為參與的硬碟數量增加而降低系統整體的可靠度(MTTFsystem=MTTFsingle/NMTTF_{\text{system}} = MTTF_{\text{single}} / N)。系統可靠度的提升來自於「冗餘(Redundancy)」(如鏡像或偶數檢查碼),而非「平行處理」。

  • b. 正確。
    RAID 5 的標準定義即為區塊級分散式奇偶校驗碼(Block-interleaved distributed parity check)。它將數據與對應的 Parity 區塊交錯分散在陣列內的所有磁碟上,既提供了單碟故障的容錯能力(提升可靠度),又避免了 RAID 4 中專用奇偶校驗碟導致的寫入效能瓶頸。

  • c. 錯誤。
    根據雙磁碟鏡像系統(RAID 1)的 MTTDLMTTDL 推導公式:
    MTTDL=MTTFsingle22×MTTRMTTDL = \frac{MTTF_{\text{single}}^2}{2 \times MTTR}
    已知單碟 MTTFsingle=100,000 hoursMTTF_{\text{single}} = 100,000 \text{ hours},修復時間 MTTR=10 hoursMTTR = 10 \text{ hours},代入數據:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 3 題3 分

Which ONE of the following statements is correct? Please select the correct description for the output of the following program.

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int value = 10;
int main()
{
    pid_t pid;
    pid = fork();

    if (pid == 0) {
        value += 10;
        printf("A = %d\n",value);
        return 0;
    }
    else if (pid > 0) {
        wait(NULL);
        print("B = %d\n",value);
        return 0;
    }
}

(A) A+B2=120A+B^2=120
(B) A=BA=B
(C) 2A+3B=502A+3B=50
(D) B=20B=20
(E) None of the above.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

  1. fork() 系統呼叫與位址空間隔離(Process Address Space Isolation):

    • 呼叫 fork() 時,作業系統會建立一個全新的子行程(Child Process)。子行程會複製父行程(Parent Process)的虛擬位址空間,包含程式碼區段(Code Segment)、資料區段(Data Segment,存放全域變數與靜態變數)、堆積(Heap)與堆疊(Stack)。
    • 雖然現代作業系統普遍採用寫入時複製(Copy-on-Write, COW)技術優化實體記憶體配置,但在語意上,父子行程擁有各自獨立的記憶體位址空間。子行程對全域變數進行的任何數值修改,僅限於子行程自身的資料區段,絕不會影響父行程對應變數的數值。
  2. wait() 系統呼叫與行程同步(Process Synchronization):

    • wait(NULL) 系統呼叫會將呼叫者(父行程)掛起(Block),直至其子行程執行完畢並終止(Terminate)。
    • 此機制確保了執行順序:子行程的輸出必定早於父行程恢復執行後的輸出;同時亦能回收子行程的終止狀態與行程表資源,避免子行程成為殭屍行程(Zombie Process)。
  3. 印刷筆誤說明(Typographical Note):

    • 程式碼父行程分支中之 print("B = %d\n",value); 顯為出題印刷筆誤(缺少字母 f,應為標準函式庫之 printf),依題意輸出標籤應為 B = 10。

程式追蹤與推導

本題執行流程可拆解為以下步驟:

  1. 初始狀態:

    • 全域變數 int value = 10; 位於 Data Segment,初始值為 1010。
  2. 建立子行程(fork()):

    • 執行 pid = fork(); 後,父子行程分流執行。子行程獲得的傳回值為 pid == 0,父行程獲得的傳回值為子行程之 PID(pid > 0)。
  3. 子行程執行(pid == 0 分支):

    • 子行程擁有獨立的 value(初始值為 1010)。
    • 執行 value += 10;,子行程記憶體中的 value 更新為:
      10+10=2010 + 10 = 20
    • 執行 printf("A = %d\n", value);,向標準輸出印出 A = 20,對應標籤數值 A=20A = 20。
    • 執行 return 0;,子行程正常結束。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 4 題7 分

  1. (7 Points) Given the following descriptions, which ONE of the following statements is correct? Please calculate the values of variables P, L, X, N, Y, A, and B to select the correct statement.

Consider a paging system with a page size of 2KB. The amount of physical memory is 64KB. Let P denote the number of bits required in the physical address. Let L denote the number of bits required in the logical address.

Conventionally, a single-level page table is stored in the memory. If the page table is implemented in the conventional way, a page table in the memory contains 1024 entries. The memory access time is 180 nanoseconds (ns). Assume that the hit ratio of the conventional page table is 60% and the average page fault service time is 30,000 microseconds (μs). Therefore, the effective memory access time for the conventional approach with a single-level page table is X.

Alex would like to consider an "inverted page table" to improve the translation from a logical address to a physical address. After careful calculation, he found that the inverted page table requires N entries. Furthermore, Alex considers the translation look-aside buffer (TLB) hardware to implement the idea of the inverted page table. Searching TLB takes 20 nanoseconds (ns). Assume that the hit ratio of the inverted page table is 90%. The effective memory access time for Alex's approach with a TLB-implemented inverted page table is Y. So, Alex's approach improves the effective memory access time, where A< speedup (in terms of the effective memory access time) <B. Here, A and B are positive integers.

Choices:
(A) 2L+3P+5N+A^3+B^2=293.
(B) AB(5L+2P+N)>10000
(C) 4L-2P+3N-5A+2B=205
(D) 3L+PN-4A+2B>1024
(E) None of the Above.

登入後即可作答並保存紀錄。

這一題的完整詳解

觀念解析與參數推導

本題評量分頁系統(Paging System)、反向頁表(Inverted Page Table)、轉址旁存緩衝器(TLB)及需求分頁之有效記憶體存取時間(Effective Memory Access Time, EMAT)之計算與加速比評估。

1. 實體位址位元數 PP

  • 分頁大小(Page Size)=2 KB=211 Bytes= 2\text{ KB} = 2^{11}\text{ Bytes},故頁內偏移量(Offset)需要 11 bits11\text{ bits}。
  • 實體記憶體(Physical Memory)大小 =64 KB=216 Bytes= 64\text{ KB} = 2^{16}\text{ Bytes}。
  • 實體位址(Physical Address)總位元數:
    P=log⁡2(64 KB)=16 bitsP = \log_2(64\text{ KB}) = 16\text{ bits}
  • 實體頁框數(Frame Count)=64 KB2 KB=32=25= \frac{64\text{ KB}}{2\text{ KB}} = 32 = 2^5,頁框號(Frame Number)需 5 bits5\text{ bits},實體位址 P=5+11=16 bitsP = 5 + 11 = 16\text{ bits}。

2. 邏輯位址位元數 LL

  • 傳統單層頁表(Single-level Page Table)中,每一個虛擬頁面(Virtual Page)皆對應頁表中的一筆項目(Entry)。
  • 頁表共有 1024=2101024 = 2^{10} 筆項目,代表虛擬頁號(Virtual Page Number, VPN)佔 10 bits10\text{ bits}。
  • 邏輯位址(Logical Address)總位元數:
    L=VPN bits+Offset bits=10+11=21 bitsL = \text{VPN bits} + \text{Offset bits} = 10 + 11 = 21\text{ bits}

3. 反向頁表項目數 NN

  • 反向頁表(Inverted Page Table)是以實體頁框(Physical Frames)為索引的全域結構,每個實體頁框恰好對應一筆項目(記錄該頁框目前存放哪個行程的哪一個虛擬頁面)。
  • 實體頁框總數 =64 KB2 KB=32= \frac{64\text{ KB}}{2\text{ KB}} = 32。
  • 因此,反向頁表所需的項目數為:
    N=32N = 32

4. 傳統單層頁表之有效記憶體存取時間 XX

  • 記憶體存取時間 tm=180 nst_m = 180\text{ ns}。
  • 頁表命中率 h1=60%=0.6h_1 = 60\% = 0.6,缺頁率(Page Fault Rate)p1=1−0.6=0.4p_1 = 1 - 0.6 = 0.4。
  • 平均缺頁服務時間(Page Fault Service Time)=30,000 μs=30,000,000 ns= 30{,}000\ \mu\text{s} = 30{,}000{,}000\text{ ns}。
  • 命中時需存取 2 次實體記憶體(一次查單層頁表,一次存取資料):180 ns+180 ns=360 ns180\text{ ns} + 180\text{ ns} = 360\text{ ns}。
  • 因此,有效記憶體存取時間 XX 為:
    X=0.6×(180+180) ns+0.4×30,000,000 ns=216+12,000,000=12,000,216 nsX = 0.6 \times (180 + 180)\text{ ns} + 0.4 \times 30{,}000{,}000\text{ ns} = 216 + 12{,}000{,}000 = 12{,}000{,}216\text{ ns}
    (若命中時僅計 180 ns180\text{ ns},則 X=12,000,108 nsX = 12{,}000{,}108\text{ ns},皆約為 12 ms12\text{ ms})。

5. 結合 TLB 之反向頁表有效記憶體存取時間 YY

  • TLB 搜尋時間 tTLB=20 nst_{\text{TLB}} = 20\text{ ns}。
  • 反向頁表命中率 h2=90%=0.9h_2 = 90\% = 0.9,缺頁率 p2=1−0.9=0.1p_2 = 1 - 0.9 = 0.1。
  • 命中時(TLB Hit 且頁面在記憶體中):花費時間為搜尋 TLB 加上存取資料記憶體一次,即 20 ns+180 ns=200 ns20\text{ ns} + 180\text{ ns} = 200\text{ ns}。
  • 未命中時發生缺頁,花費缺頁服務時間 30,000,000 ns30{,}000{,}000\text{ ns}。
  • 因此,有效記憶體存取時間 YY 為:
    Y=0.9×(20+180) ns+0.1×30,000,000 ns=180+3,000,000=3,000,180 nsY = 0.9 \times (20 + 180)\text{ ns} + 0.1 \times 30{,}000{,}000\text{ ns} = 180 + 3{,}000{,}000 = 3{,}000{,}180\text{ ns}

6. 加速比(Speedup)與正整數界限 A,BA, B

  • 加速比定義為存取時間的改善倍率:
    Speedup=XY=12,000,2163,000,180≈3.99983\text{Speedup} = \frac{X}{Y} = \frac{12{,}000{,}216}{3{,}000{,}180} \approx 3.99983
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 5 題10 分

Given the following descriptions, which ONE of the following statements is correct? Please calculate the values of P, D, M, and F to select the correct statement.

Assume that we have a computer system with a 32-bit processor. The computer supports a paged virtual memory. The page size is 8KB. Each address referenced by the processor contains P bits for indexing the page number and D bits for indexing the page offset. Initially, three empty frames are given to execute a process. Assume that the processor of the computer references the following sequence of hexadecimal addresses to execute the process:

4DF6BC24
1CA54523
4DF6AC24
1CA55523
23A22260
23BCDFFF
23A23261
23BCCFFF
1CA54523
23B23261

The working-set model is implemented in the system. The size of the working-set window is W=6. Generally, processes tend to have locality of memory references. Alex looked over the sizes of working sets resulting from the above sequence of referenced addresses. He found that the maximal size of working sets over the referenced sequence is M. Therefore, Alex designed a locality-based page replacement algorithm according to the locality principle. To implement his algorithm, he quantifies "referenced likelihood for each page" as the ratio of the total number of references to the page within the working-set window to the size of the working-set window. Note that the pages referenced within the working-set window include the page that the processor is currently attempting to reference. When a hexadecimal address is referenced, the algorithm needs to track the referenced likelihood for each page. If a page fault happens, the page with the minimal referenced likelihood is selected to be replaced. If multiple pages have the same referenced likelihood, the Least recently used (LRU) algorithm is used to break the tie. So, the total number of page faults arising from the sequence of hexadecimal addresses referenced by the processor is F.

Choices:
(A) 2P+5D+M⁴+F²>1000
(B) M+D+P+F=42
(C) 5M+2D+2P-F=34
(D) M²-D²+3P+4F>500
(E) None of the above.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念與架構解析

本題考查分頁式虛擬記憶體(Paged Virtual Memory)架構中的位址轉換(Address Translation)、工作集模型(Working-Set Model)​以及基於局域性原理之頁面替換演算法(Locality-Based Page Replacement Algorithm)​的模擬與計算。


一、計算頁號位元數 PP 與頁內偏移位元數 DD

  1. 系統位址與分頁大小:
    • 處理器為 32 位元架構,因此虛擬位址總長度為 32 bits32\text{ bits}。
    • 頁面大小(Page Size)為 8 KB=8×1024 Bytes=213 Bytes8\text{ KB} = 8 \times 1024\text{ Bytes} = 2^{13}\text{ Bytes}。
  2. 位元劃分:
    • 頁內偏移量(Page Offset)所需位元數:
      D=log⁡2(8192)=13 bitsD = \log_2(8192) = 13\text{ bits}
    • 虛擬頁號(Virtual Page Number, VPN)所需位元數:
      P=32−D=32−13=19 bitsP = 32 - D = 32 - 13 = 19\text{ bits}

二、十六進位記憶體位址轉換為頁號

虛擬位址由高位 19 bits19\text{ bits} 的頁號與低位 13 bits13\text{ bits} 的頁內偏移組成。若將 32 位元十六進位位址轉為頁號,相當於將位址向右平移 13 位元(即去除最低 13 位元的偏移量,Page Number=Address≫13\text{Page Number} = \text{Address} \gg 13)。

對應題幹所給之 10 個十六進位參考位址,依序轉換如下:

  1. 0x4DF6BC24:0x4DF6BC24≫13=0x26FB5\text{0x4DF6BC24} \gg 13 = \text{0x26FB5}(十進位 159669159669),代記為頁面 AA
  2. 0x1CA54523:0x1CA54523≫13=0x0E52A\text{0x1CA54523} \gg 13 = \text{0x0E52A}(十進位 5866658666),代記為頁面 BB
  3. 0x4DF6AC24:0x4DF6AC24≫13=0x26FB5\text{0x4DF6AC24} \gg 13 = \text{0x26FB5}(十進位 159669159669),為頁面 AA
  4. 0x1CA55523:0x1CA55523≫13=0x0E52A\text{0x1CA55523} \gg 13 = \text{0x0E52A}(十進位 5866658666),為頁面 BB
  5. 0x23A22260:0x23A22260≫13=0x11D11\text{0x23A22260} \gg 13 = \text{0x11D11}(十進位 7297772977),代記為頁面 CC
  6. 0x23BCDFFF:0x23BCDFFF≫13=0x11DE6\text{0x23BCDFFF} \gg 13 = \text{0x11DE6}(十進位 7319073190),代記為頁面 DD
  7. 0x23A23261:0x23A23261≫13=0x11D11\text{0x23A23261} \gg 13 = \text{0x11D11}(十進位 7297772977),為頁面 CC
  8. 0x23BCCFFF:0x23BCCFFF≫13=0x11DE6\text{0x23BCCFFF} \gg 13 = \text{0x11DE6}(十進位 7319073190),為頁面 DD
  9. 0x1CA54523:0x1CA54523≫13=0x0E52A\text{0x1CA54523} \gg 13 = \text{0x0E52A}(十進位 5866658666),為頁面 BB
  10. 0x23B23261:0x23B23261≫13=0x11D91\text{0x23B23261} \gg 13 = \text{0x11D91}(十進位 7310573105),代記為頁面 EE

因此,10 次存取的頁面序列為:
⟨A,B,A,B,C,D,C,D,B,E⟩\langle A, B, A, B, C, D, C, D, B, E \rangle


三、工作集模型(Working-Set Model)與最大工作集大小 MM

工作集視窗大小為 W=6W = 6。依題意,在第 tt 次存取時的滑動視窗包含當前存取在內的最近至多 6 次頁面參考。各時間點 tt 的工作集如下:

  • t=1t=1:視窗 ⟨A⟩  ⟹  {A}\langle A \rangle \implies \{A\},大小為 11
  • t=2t=2:視窗 ⟨A,B⟩  ⟹  {A,B}\langle A, B \rangle \implies \{A, B\},大小為 22
  • t=3t=3:視窗 ⟨A,B,A⟩  ⟹  {A,B}\langle A, B, A \rangle \implies \{A, B\},大小為 22
  • t=4t=4:視窗 ⟨A,B,A,B⟩  ⟹  {A,B}\langle A, B, A, B \rangle \implies \{A, B\},大小為 22
  • t=5t=5:視窗 ⟨A,B,A,B,C⟩  ⟹  {A,B,C}\langle A, B, A, B, C \rangle \implies \{A, B, C\},大小為 33
  • t=6t=6:視窗 ⟨A,B,A,B,C,D⟩  ⟹  {A,B,C,D}\langle A, B, A, B, C, D \rangle \implies \{A, B, C, D\},大小為 44
  • t=7t=7:視窗 ⟨B,A,B,C,D,C⟩  ⟹  {A,B,C,D}\langle B, A, B, C, D, C \rangle \implies \{A, B, C, D\},大小為 44
  • t=8t=8:視窗 ⟨A,B,C,D,C,D⟩  ⟹  {A,B,C,D}\langle A, B, C, D, C, D \rangle \implies \{A, B, C, D\},大小為 44
  • t=9t=9:視窗 ⟨B,C,D,C,D,B⟩  ⟹  {B,C,D}\langle B, C, D, C, D, B \rangle \implies \{B, C, D\},大小為 33
  • t=10t=10:視窗 ⟨C,D,C,D,B,E⟩  ⟹  {B,C,D,E}\langle C, D, C, D, B, E \rangle \implies \{B, C, D, E\},大小為 44

在整個參考序列中,工作集的最大大小為:
M=max⁡(1,2,2,2,3,4,4,4,3,4)=4M = \max(1, 2, 2, 2, 3, 4, 4, 4, 3, 4) = 4


四、基於局域性之頁面替換演算法與缺頁次數 FF

  1. 演算法規則:

    • 系統初始分配 33 個空實體框架(Frames)。
    • 每個頁面的「參考可能性(Referenced Likelihood)」定義為該頁面在視窗 W=6W=6 內的出現次數與視窗大小 WW 的比值。在同一個時間點進行比較時,各候選頁面的分母均相同,故等價於比較該頁在當前視窗內的參考次數。
    • 若發生缺頁(Page Fault)且框架已滿,選擇參考可能性最小(即視窗內出現次數最少)的頁面替換。
    • 若有多個頁面具有相同之最小參考可能性,則採用 LRU(Least Recently Used) 策略打破平手,替換最久未被使用的頁面。
  2. 逐步模擬過程:

    • t=1t = 1(存取 AA):框架為空   ⟹  \implies 缺頁(Fault #1)。直接載入空框架,框架內容為 {A}\{A\}。
    • t=2t = 2(存取 BB):框架未滿   ⟹  \implies 缺頁(Fault #2)。載入空框架,框架內容為 {A,B}\{A, B\}。
    • t=3t = 3(存取 AA):AA 已在框架中   ⟹  \implies 命中(Hit)。框架內容維持 {A,B}\{A, B\}。
    • t=4t = 4(存取 BB):BB 已在框架中   ⟹  \implies 命中(Hit)。框架內容維持 {A,B}\{A, B\}。
    • t=5t = 5(存取 CC):框架未滿   ⟹  \implies 缺頁(Fault #3)。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 6 題5 分

🖼️【此處有附圖,請對照原卷】
The following figure shows an example data structure used for opening a file from file systems in user space, kernel memory, and secondary storage in operating systems. The number in the circle represents one operation in the computing systems.

Select the correct statement(s) for the above figure:
(A) Operation will be conducted to ensure that there is no duplicated file in the same directory.
(B) Operation refers to the file descriptor to the system-wide open-file table to create an entry.
(C) When the file 'foo.txt' does not exist in the file system, Operation is called to create a new entry in the directory structure.
(D) When the file 'foo.txt' has been open in the system, Opertion is will not be called.
(E) When the file 'foo.txt' has not been open in the file system, Opertion creates an entry to store the File Control Block of the file.

🖼️ 本題含圖表,以下為原卷對應頁面:
原卷第 5 頁

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題測驗作業系統中檔案系統(File System)在開啟檔案(open 系統呼叫)時,於使用者空間(User Space)、核心記憶體(Kernel Memory)​與次級儲存裝置(Secondary Storage)​之間所涉及的記憶體資料結構與運作流程:

  1. 目錄結構(Directory Structure):記錄檔名與其對應的檔案控制區塊(File Control Block, FCB / inode)的對應關係。
  2. 系統層級開啟檔案表(System-wide Open-file Table):在核心記憶體中快取所有目前被系統開啟的檔案之 FCB、開啟計數(open count)等全域資訊。
  3. 行程層級開啟檔案表(Per-process Open-file Table):記錄個別行程開啟檔案的狀態,包含檔案讀寫指標(file offset)、存取模式,以及指向系統層級開啟檔案表對應項目的指標。
  4. 檔案描述子(File Descriptor, fdfd):open 成功後回傳給使用者空間的非負整數,為行程層級開啟檔案表的索引值。

解題方法

由原卷附圖可辨識出系統開啟檔案的完整資料流與編號操作:

  • 使用者空間(User Space):包含 open("foo.txt") 呼叫(操作 ①)與接收回傳的檔案描述子(操作 ②)。
  • 核心記憶體(Kernel Memory):由左至右分別為 system-wide open-file table(操作 ④ 建立對應項目存放 FCB)、per-process open-file table、以及快取的 directory structure。
  • 次級儲存裝置(Secondary Storage):包含磁碟上的 directory structure(操作 ③ 進行目錄搜尋)與儲存檔案後設資料的 File-Control Block (FCB)(操作 ⑤ 將磁碟上的 FCB 載入至核心記憶體)。

解題切入點在於區分「檔案首次開啟」與「檔案已被系統中其他行程開啟」時的行為差異,以及各資料結構的角色定位。


選項分析

  • (A) 錯誤:
    操作 ① 是行程在使用者空間發出 open("foo.txt") 系統呼叫以開啟既有檔案。其目的是透過檔案路徑查詢目錄並取得檔案存取權限與代碼,並非用來「確認同目錄下沒有重複的檔案(ensure that there is no duplicated file in the same directory)」。檢查同目錄是否有重複檔名是「建立新檔案」(如 creat 或 open 帶有 O_CREAT | O_EXCL 旗標)時的動作。

  • (B) 錯誤:
    操作 ② 是系統呼叫完成後,核心將產生的檔案描述子(file descriptor)​回傳給使用者空間(user space)的呼叫行程。檔案描述子是該行程之 per-process open-file table 的索引。選項敘述「returns the file descriptor to the system-wide open-file table to create an entry」完全錯誤,檔案描述子並非回傳給系統層級開啟檔案表,且系統表的項目早在先前步驟已建立。

  • (C) 錯誤:
    操作 ③ 是在次級儲存裝置的目錄結構中比對搜尋 foo.txt 的目錄項。在一般的 open("foo.txt") 呼叫下,若檔案不存在於檔案系統中,目錄搜尋將宣告失敗,系統直接回傳錯誤(回傳 -1 並設定 errno = ENOENT),絕不會自動呼叫操作 ③ 去建立新的目錄項(create a new entry)。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 7 題5 分

Given a set of 5 processes, the arrival time of the i-th process Pi is A[i], and the burst time of the i-th process Pi is B[i], as shown below:

ProcessArrival Time, A[i]Burst Time, B[i]
P1010
P208
P305
P402
P504

Perform round-robin scheduling for these five processes. Assume that the average waiting time of all processes is a x 10^2+ b x 10^1+ c x 10^0+ d x 10^-1. Select the correct description(s).

Choices:
(A) a + b + c + d = 8.
(B) b = 2c.
(C) b + d = 3.
(D) 5b = c.
(E) a x c = 5.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

  1. 輪轉排程法(Round-Robin Scheduling, RR):

    • 機制:RR 排程為先佔式(Preemptive)排程演算法,系統將 CPU 時間劃分為固定長度的時間區段,稱為時間切片(Time Quantum, qq)。
    • 佇列管理:系統以 FIFO Ready Queue 管理行程。當新行程抵達或被中斷搶佔時,會被置於佇列尾端;CPU 總是分派給佇列前端的行程。
    • 搶佔與完成:若行程在時間切片 qq 結束前未執行完畢,定時器中斷(Timer Interrupt)會將其搶佔並放回佇列尾端;若行程在時間切片內執行完畢,則提前釋放 CPU 並結束。
  2. 行程時間指標與定義:

    • 完成時間(Completion Time, CiC_i):行程 PiP_i 結束執行的時間點。
    • 轉返時間(Turnaround Time, TATiTAT_i):行程從抵達到完成的總時間差:
      TATi=Ci−A[i]TAT_i = C_i - A[i]
    • 等待時間(Waiting Time, WiW_i):行程在 Ready Queue 中等待被分派 CPU 的累計時間:
      Wi=TATi−B[i]=Ci−A[i]−B[i]W_i = TAT_i - B[i] = C_i - A[i] - B[i]
    • 平均等待時間(Average Waiting Time, AWT\text{AWT}):所有行程等待時間的算術平均值:
      AWT=1n∑i=1nWi\text{AWT} = \frac{1}{n} \sum_{i=1}^{n} W_i
  3. 數值展開式與題意分析:

    • 題目將平均等待時間表示為:
      AWT=a×102+b×101+c×100+d×10−1\text{AWT} = a \times 10^2 + b \times 10^1 + c \times 10^0 + d \times 10^{-1}
      此形式即十進位數值展開式,其中 aa 為百位數、bb 為十位數、cc 為個位數、dd 為十分位數(小數點後第一位)。
    • 由於所有行程的 CPU 執行總時間僅為 ∑B[i]=10+8+5+2+4=29\sum B[i] = 10 + 8 + 5 + 2 + 4 = 29,平均等待時間必然小於 29,百位數字必然為 a=0a = 0。
    • 題目未指定時間切片 qq,但在常見整數時間切片設定中,當時間切片 q=2q = 2 時,推導出之平均等待時間恰為 15.215.2(即 a=0,b=1,c=5,d=2a=0, b=1, c=5, d=2),能同時滿足選項 (A)、(C)、(D) 的嚴密關係,符合命題設計。

詳細推導與甘特圖(以 q=2q = 2 為例)

題目給定 5 個行程,抵達時間皆為 A[i]=0A[i] = 0,初始佇列依序為 [P1,P2,P3,P4,P5][P_1, P_2, P_3, P_4, P_5],Burst Time 分別為:
B[1]=10,B[2]=8,B[3]=5,B[4]=2,B[5]=4B[1] = 10, \quad B[2] = 8, \quad B[3] = 5, \quad B[4] = 2, \quad B[5] = 4

以時間切片 q=2q = 2 進行排程模擬:

1. 甘特圖執行歷程

  • 第 1 輪(t=0→10t = 0 \to 10):

    • t=0→2t = 0 \to 2:P1P_1 執行 2 單位,剩餘 8,移至佇列尾端。佇列:[P2,P3,P4,P5,P1][P_2, P_3, P_4, P_5, P_1]
    • t=2→4t = 2 \to 4:P2P_2 執行 2 單位,剩餘 6,移至佇列尾端。佇列:[P3,P4,P5,P1,P2][P_3, P_4, P_5, P_1, P_2]
    • t=4→6t = 4 \to 6:P3P_3 執行 2 單位,剩餘 3,移至佇列尾端。佇列:[P4,P5,P1,P2,P3][P_4, P_5, P_1, P_2, P_3]
    • t=6→8t = 6 \to 8:P4P_4 執行 2 單位,剩餘 0,執行完畢,完成時間 C4=8C_4 = 8。佇列:[P5,P1,P2,P3][P_5, P_1, P_2, P_3]
    • t=8→10t = 8 \to 10:P5P_5 執行 2 單位,剩餘 2,移至佇列尾端。佇列:[P1,P2,P3,P5][P_1, P_2, P_3, P_5]
  • 第 2 輪(t=10→18t = 10 \to 18):

    • t=10→12t = 10 \to 12:P1P_1 執行 2 單位,剩餘 6,移至佇列尾端。佇列:[P2,P3,P5,P1][P_2, P_3, P_5, P_1]
    • t=12→14t = 12 \to 14:P2P_2 執行 2 單位,剩餘 4,移至佇列尾端。佇列:[P3,P5,P1,P2][P_3, P_5, P_1, P_2]
    • t=14→16t = 14 \to 16:P3P_3 執行 2 單位,剩餘 1,移至佇列尾端。佇列:[P5,P1,P2,P3][P_5, P_1, P_2, P_3]
    • t=16→18t = 16 \to 18:P5P_5 執行 2 單位,剩餘 0,執行完畢,完成時間 C5=18C_5 = 18。佇列:[P1,P2,P3][P_1, P_2, P_3]
  • 第 3 輪(t=18→23t = 18 \to 23):

    • t=18→20t = 18 \to 20:P1P_1 執行 2 單位,剩餘 4,移至佇列尾端。佇列:[P2,P3,P1][P_2, P_3, P_1]
    • t=20→22t = 20 \to 22:P2P_2 執行 2 單位,剩餘 2,移至佇列尾端。佇列:[P3,P1,P2][P_3, P_1, P_2]
    • t=22→23t = 22 \to 23:P3P_3 執行 1 單位,剩餘 0,執行完畢,完成時間 C3=23C_3 = 23。佇列:[P1,P2][P_1, P_2]
  • 第 4 輪(t=23→27t = 23 \to 27):

    • t=23→25t = 23 \to 25:P1P_1 執行 2 單位,剩餘 2,移至佇列尾端。佇列:[P2,P1][P_2, P_1]
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 8 題5 分

Given a set of 5 periodic processes, which all arrive at time 0, as shown below:

ProcessRelative DeadlinePeriodBurst Time
P1105010
P2101205
P3580010
P4102010
P5104010

Perform the deadline-monotonic scheduling. Select the correct description(s).
(A) Deadline monotonic algorithm and rate monotonic algorithm have the same schedulability for this task set.
(B) The worst-case response time (from the arrival time to the time of the completion of a process) of P2 is 20.
(C) Process P4 is schedulable.
(D) If we increase the burst time of P3 by X time units, the worst-case response time of P3 will also be increased by X time units.
(E) All the tasks remain schedulable when the burst time of process P1 is set to 8.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

  1. 即時系統排程(Real-Time Scheduling):
    • Deadline-Monotonic (DM) 排程演算法:屬於靜態優先級(Static Priority / Fixed Priority)搶佔式排程。優先級分配原則為相對截止時間(Relative Deadline, DiD_i)越短者,優先級越高。
    • Rate-Monotonic (RM) 排程演算法:優先級分配原則為週期(Period, TiT_i)越短者,優先級越高。當任務的相對截止時間等於週期(Di=TiD_i = T_i)時,DM 與 RM 等價;但若 Di≤TiD_i \le T_i,DM 為最佳靜態優先級排程演算法(Optimal Static Priority Scheduling Algorithm)。
  2. 最差回應時間分析(Worst-Case Response Time, WCRT Analysis):
    • 任務 PiP_i 的回應時間 RiR_i 為從到達時間(Arrival Time)至執行完成時間(Completion Time)的時間差。
    • 當所有任務同時於 t=0t=0 到達時(Critical Instant),PiP_i 的最差回應時間公式為迭代式:
      Ri(k+1)=Ci+∑j∈HP(i)⌈Ri(k)Tj⌉CjR_i^{(k+1)} = C_i + \sum_{j \in HP(i)} \left\lceil \frac{R_i^{(k)}}{T_j} \right\rceil C_j
      其中 CiC_i 為任務 PiP_i 的 Burst Time(執行時間),HP(i)HP(i) 表示優先級高於 PiP_i 的任務集合(Higher-Priority Tasks)。
  3. 可排程性(Schedulability):
    • 任務 PiP_i 為可排程(Schedulable),若且唯若其最差回應時間不超過相對截止時間,即 Ri≤DiR_i \le D_i。
    • 若任務集中存在至少一個任務滿足 Ri>DiR_i > D_i,則該任務集不可排程(Unschedulable)。

解題方法

Step 1: 確定 DM 與 RM 下的優先級排序

整理題目所給定之各任務參數:

  • P1:D1=10,T1=50,C1=10P_1: D_1 = 10, T_1 = 50, C_1 = 10

  • P2:D2=10,T2=120,C2=5P_2: D_2 = 10, T_2 = 120, C_2 = 5

  • P3:D3=5,T3=800,C3=10P_3: D_3 = 5, T_3 = 800, C_3 = 10

  • P4:D4=10,T4=20,C4=10P_4: D_4 = 10, T_4 = 20, C_4 = 10

  • P5:D5=10,T5=40,C5=10P_5: D_5 = 10, T_5 = 40, C_5 = 10

  • DM 優先級(依 DiD_i 由小到大排序):
    D3=5<D1=D2=D4=D5=10D_3 = 5 < D_1 = D_2 = D_4 = D_5 = 10。
    P3P_3 擁有嚴格最小的相對截止時間,故 P3P_3 具有最高優先級。

  • RM 優先級(依 TiT_i 由小到大排序):
    T4(20)<T5(40)<T1(50)<T2(120)<T3(800)T_4 (20) < T_5 (40) < T_1 (50) < T_2 (120) < T_3 (800)。
    優先級順序為 P4>P5>P1>P2>P3P_4 > P_5 > P_1 > P_2 > P_3。

Step 2: 驗證關鍵任務之 WCRT 與可排程性

  1. P3P_3 在 DM 下的 WCRT:
    由於 P3P_3 為最高優先級任務,HP(P3)=∅HP(P_3) = \emptyset,不受任何其他任務搶佔:
    R3=C3=10R_3 = C_3 = 10
    因為 R3=10>D3=5R_3 = 10 > D_3 = 5,P3P_3 錯過截止時間。因此,P3P_3 不可排程,整個任務集在 DM 下為不可排程(Unschedulable)。

  2. P3P_3 在 RM 下的 WCRT:
    在 RM 下,P3P_3 的週期最長(T3=800T_3 = 800),優先級最低。於 Critical Instant (t=0t=0) 時,P3P_3 的回應時間至少需包含所有高優先級任務的一次執行:
    R3≥C4+C5+C1+C2+C3=10+10+10+5+10=45R_3 \ge C_4 + C_5 + C_1 + C_2 + C_3 = 10 + 10 + 10 + 5 + 10 = 45
    因為 R3≥45>D3=5R_3 \ge 45 > D_3 = 5,P3P_3 同樣錯過截止時間。整個任務集在 RM 下亦為不可排程(Unschedulable)。


選項分析

  • (A) 正確:
    在 RM 與 DM 排程演算法下,此任務集均無法使所有任務滿足截止時間要求(兩者的可排程性結果皆為「不可排程」)。因此,兩演算法對此任務集具備相同的可排程性(Same Schedulability)。

  • (B) 錯誤:
    在 DM 排程下,P3P_3 的相對截止時間為 5,嚴格小於 P2P_2 的相對截止時間 10,因此 P3∈HP(P2)P_3 \in HP(P_2)。
    P2P_2 的回應時間必然為自身執行時間 C2=5C_2 = 5 加上高優先級任務造成的干擾時間。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 9 題5 分

Please select the correct description(s).
(A) A system with two processing cores can always get a double speedup.
(B) For process execution, it is possible to have concurrency without parallelism.
(C) The Pthreads API(s) can be used in multithreaded C programs for thread creation and synchronization. When the parent thread calls the pthread_join() function, it waits for the child thread to terminate. It is synchronous threading.
(D) A process transits from a waiting state to a running state when it is assigned CPU for execution.
(E) If the binding of instructions and data to memory addresses (i.e., address binding) is done at the compile time, it is called "dynamic binding".

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題涵蓋作業系統(Operating Systems)五大基礎核心觀念:

  1. 阿姆達爾定律與平行加速極限(Amdahl's Law & Speedup Limit):多核心系統的加速比受到不可平行化序列程式碼比例以及實體硬體開銷(如排程、通訊、快取一致性)的限制,無法達成無條件的線性倍數增長。
  2. 併行與平行之本質區別(Concurrency vs. Parallelism):併行(Concurrency)指多個任務在重疊時間段內皆能推進(Interleaved execution,可於單核心以時分多工實現);平行(Parallelism)指多個任務在同一實體時間點同時執行(Simultaneous execution,必須具備多核心或多處理器硬體支援)。
  3. Pthreads 執行緒同步模型(Pthreads & Synchronous Threading):POSIX Threads(pthread)模型中,同步執行緒(Synchronous threading)指父執行緒建立子執行緒後,透過呼叫 pthread_join() 阻塞並等待子執行緒終止後才繼續執行;非同步執行緒(Asynchronous threading)則由父子執行緒各自獨立平行推進。
  4. 行程五態模型與狀態轉移(Five-State Process Model):行程在等待事件完成時處於 Waiting 狀態,事件發生後只能先轉入 Ready 狀態(進入 Ready Queue),唯有處於 Ready 狀態的行程經由 CPU 排程器派發(Scheduler dispatch)才能轉入 Running 狀態。
  5. 記憶體位址綁定(Address Binding):位址綁定發生於編譯時期(Compile time)與載入時期(Load time)者屬於靜態綁定(Static binding);唯有延遲至執行時期(Execution time / Run time)透過硬體動態轉譯者才稱為動態綁定(Dynamic binding)。

選項詳細解析

  • (A) 錯誤
    根據阿姆達爾定律(Amdahl's Law),若系統擁有 N=2N = 2 個處理核心,程式中可平行化的比例為 PP(序列不可平行化比例為 1−P1 - P),其理論加速比為:
    S=1(1−P)+PNS = \frac{1}{(1 - P) + \frac{P}{N}}
    若程式存在不可平行化的序列區段(即 1−P>01 - P > 0),加速比上限嚴格小於 22。此外,即便程式理論上完全可平行化(P=1P = 1),在真實硬體系統中,建立與管理執行緒、處理核心間的快取一致性(Cache Coherence)協定開銷、鎖定競爭(Lock Contention)以及共用記憶體匯流排頻寬瓶頸,皆會帶來額外負擔,使實際加速比無法達到理想的 2 倍。因此「總是能獲得雙倍加速(always get a double speedup)」之敘述錯誤。

  • (B) 正確
    併行(Concurrency) 與 平行(Parallelism) 具有層次上的定義區別:

    • Concurrency:系統同時處理並推進多項任務的能力。單一處理核心即可透過時間切片(Time-sharing)與內文交換(Context Switch),在多個行程之間交錯執行(Interleaved execution),使各行程在宏觀時間段內皆能取得進展。
    • Parallelism:系統在微觀的同一實體時間點上,同時執行多個任務(Simultaneous execution),必須具備多個實體處理核心或處理器硬體。
      因此,單核心作業系統執行多個行程時,完全能達到「有併行但無平行(concurrency without parallelism)」的執行模式,該敘述正確。
  • (C) 正確
    Pthreads(POSIX Threads API)為 C 語言多執行緒程式設計之標準介面。在執行緒設計模型中:

    • 同步執行緒(Synchronous threading):父執行緒在建立子執行緒後,主動等待所有子執行緒終止以彙整結果或同步進度。在 Pthreads API 中,父執行緒透過呼叫 pthread_join() 函式將自身阻塞,直到指定子執行緒終止,此行為即為同步執行緒(synchronous threading)之典型定義與實作。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 10 題8 分

Given the following C snipped and the disassembly of the resulting object of the code for the x86-64 platforms. Assume the function, up(l,u), up is in %rsi. Answer the value of CN and the full declaration of the structure np_struct.

HINT: for an instruction lea (a, b, num), c, the result of (b*num + a) is moved to c.

extern struct np_struct;
typedef struct {
    int n;
    int m;
    void *up;
} u_struct;

int main() {
    u_struct local;
    local.n = 0;
    local.m = 0;
    local.up = NULL;

    int_np_struct *np_val;
    int *np_val2;
    // ... some initialization of np_val and np_val2 ...
    // These are not shown, assuming they are valid pointers when used.
}

void func(long l, u_struct u, long *up) {
    int n;
    int m;
    u_struct *up_local = &u; // Assume &u is passed from caller
    // The snippet is for the following C code segment:
    // int n_val;
    // int m_val;
    // u_struct *up_next;
    // n_val = up_local->n;
    // m_val = up_local->m;
    // up_next = up_local->up;
    // ...
    // The disassembly only shows the part for accessing u_struct fields.
}

Disassembly:

mov    0x120(%rsi), %ecx           // line 1
add    %ecx, %ecx                  // line 2
lea    (%rsi,%rcx,8), %rdx         // line 3
mov    0x8(%rdx), %rax             // line 4
mov    (%rdx), %r8                 // line 5
mov    0x8(%rsi), %rcx             // line 6
mov    0x0(%rcx), %r9              // line 7
mov    0x8(%rcx), %r10             // line 8
retq                               // line 9

Choices:
(A) CN = 8, nt_struct = {long idx, long idx1, int x4};
(B) CN = 7, nt_struct = {long idx, long idx1, int x5};
(C) CN = 8, nt_struct = {long idx, int idx1, long x5};
(D) CN = 7, nt_struct = {long idx, long idx1, long x3};
(E) None of the above.

登入後即可作答並保存紀錄。

這一題的完整詳解

解題重點

  1. mov 0x120(%rsi),%ecx 取得位於 %rsi+0x120 的 32‑bit 整數,記為 cnt。
  2. add %ecx,%ecx 使 cnt 變為 2·cnt。
  3. lea (%rsi,%rcx,8),%rdx 等價於 %rdx = %rsi + (2·cnt)·8 = %rsi + cnt·16,可視為 以 16 為元素大小的陣列索引。
  4. mov (%rdx),%r8 取該元素的 第 0 個 8‑byte 欄位。
  5. mov 0x8(%rdx),%rax 取 第 1 個 8‑byte 欄位。
  6. 之後的 mov 0x8(%rsi),%rcx、mov (%rcx),%r9、mov 0x8(%rcx),%r10 皆是從 u_struct.up 取得兩個 long 欄位。

因此 np_struct 的前兩個欄位皆是 8‑byte long,第三欄位未在組語裡被存取,僅剩 4‑byte int(可作為額外資料或 padding)。

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 11 題5 分

Two different compilers were used to compile the same program. When using compiler A, the program executed 1.0E9 instructions and took 1.5 seconds to complete. With compiler B, the program executed 1.5E9 instructions and took 1.8 seconds to complete. Assume the processor has a clock cycle time of 1ns.
(A) The average CPI of the program produced by compiler A is 1.5.
(B) The average CPI of the program produced by compiler B is 1.5.
(C) Assume that the compiled programs now run on two new distinct processors. If the execution times on the two processors are the same, the clock of the processor running compiler A's code is 1.37x faster than the clock of the processor running compiler B's code.
(D) Assume that a new compiler, C, is developed using 4.0E8 instructions and achieves an average CPI of 0.8. The speedup of using the new compiler versus compiler A is less than 4.7x.
(E) The speedup of using compiler C described in (D) versus compiler B is less than 4x.

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題的核心為計算機結構中最基礎且最重要的 CPU 效能公式(CPU Performance Equation) 與 效能加速比(Speedup):

  1. CPU 執行時間公式:

    CPU Time=Instruction Count (IC)×CPI×Clock Cycle Time (Tc)=IC×CPIClock Rate (f)\text{CPU Time} = \text{Instruction Count (IC)} \times \text{CPI} \times \text{Clock Cycle Time } (T_c) = \frac{\text{IC} \times \text{CPI}}{\text{Clock Rate } (f)}
    • IC\text{IC}:程式執行的總指令數(Instruction Count)
    • CPI\text{CPI}:每條指令所需的平均時脈週期數(Cycles Per Instruction)
    • TcT_c:時脈週期時間(Clock Cycle Time),單位為秒(s\text{s})
    • ff:時脈頻率/速率(Clock Rate),f=1Tcf = \frac{1}{T_c},單位為赫茲(Hz\text{Hz})
  2. 加速比(Speedup)公式:

    Speedup=Execution TimeoldExecution Timenew=CPU TimebaseCPU Timetarget\text{Speedup} = \frac{\text{Execution Time}_{\text{old}}}{\text{Execution Time}_{\text{new}}} = \frac{\text{CPU Time}_{\text{base}}}{\text{CPU Time}_{\text{target}}}

解題方法

根據題意整理基準處理器與各編譯器的已知條件:

  • 處理器時脈週期時間:Tc=1 ns=1.0×10−9 sT_c = 1\text{ ns} = 1.0 \times 10^{-9}\text{ s}(時脈頻率 f=1 GHz=1.0×109 Hzf = 1\text{ GHz} = 1.0 \times 10^9\text{ Hz})
  • 編譯器 A(Compiler A):
    • 指令數 ICA=1.0×109\text{IC}_A = 1.0 \times 10^9
    • 執行時間 CPU TimeA=1.5 s\text{CPU Time}_A = 1.5\text{ s}
  • 編譯器 B(Compiler B):
    • 指令數 ICB=1.5×109\text{IC}_B = 1.5 \times 10^9
    • 執行時間 CPU TimeB=1.8 s\text{CPU Time}_B = 1.8\text{ s}

選項分析

選項 (A)

由效能公式推導編譯器 A 的平均 CPI:

CPU TimeA=ICA×CPIA×Tc\text{CPU Time}_A = \text{IC}_A \times \text{CPI}_A \times T_c 1.5=1.0×109×CPIA×10−9  ⟹  CPIA=1.51.5 = 1.0 \times 10^9 \times \text{CPI}_A \times 10^{-9} \implies \text{CPI}_A = 1.5

因此編譯器 A 的平均 CPI 為 1.51.5。
此選項正確。


選項 (B)

由效能公式推導編譯器 B 的平均 CPI:

CPU TimeB=ICB×CPIB×Tc\text{CPU Time}_B = \text{IC}_B \times \text{CPI}_B \times T_c 1.8=1.5×109×CPIB×10−9  ⟹  CPIB=1.81.5=1.21.8 = 1.5 \times 10^9 \times \text{CPI}_B \times 10^{-9} \implies \text{CPI}_B = \frac{1.8}{1.5} = 1.2

因此編譯器 B 的平均 CPI 應為 1.21.2,而非 1.51.5。
此選項錯誤。


選項 (C)

設執行編譯器 A 程式碼的處理器為 Processor A(時脈頻率為 fAf_A),執行編譯器 B 程式碼的處理器為 Processor B(時脈頻率為 fBf_B)。

兩者所消耗的總時脈週期數(Clock Cycles)分別為:

  • CyclesA=ICA×CPIA=1.0×109×1.5=1.5×109 cycles\text{Cycles}_A = \text{IC}_A \times \text{CPI}_A = 1.0 \times 10^9 \times 1.5 = 1.5 \times 10^9\text{ cycles}
  • CyclesB=ICB×CPIB=1.5×109×1.2=1.8×109 cycles\text{Cycles}_B = \text{IC}_B \times \text{CPI}_B = 1.5 \times 10^9 \times 1.2 = 1.8 \times 10^9\text{ cycles}

題目假設兩處理器的執行時間相同(CPU TimeA′=CPU TimeB′\text{CPU Time}_A' = \text{CPU Time}_B'):

CyclesAfA=CyclesBfB  ⟹  1.5×109fA=1.8×109fB\frac{\text{Cycles}_A}{f_A} = \frac{\text{Cycles}_B}{f_B} \implies \frac{1.5 \times 10^9}{f_A} = \frac{1.8 \times 10^9}{f_B}

兩處理器的時脈速率比例為:

fAfB=1.51.8=56≈0.833\frac{f_A}{f_B} = \frac{1.5}{1.8} = \frac{5}{6} \approx 0.833

若比較時脈頻率,fB=1.2fAf_B = 1.2 f_A(Processor B 的時脈快 1.21.2 倍);Processor A 的時脈比 Processor B 慢,並非快 1.371.37 倍。
此選項錯誤。


選項 (D)

新編譯器 C 的條件如下:

  • 指令數 ICC=4.0×108=0.4×109\text{IC}_C = 4.0 \times 10^8 = 0.4 \times 10^9
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 12 題6 分

Assume the individual stages of the datapath have the following latencies:
IF: 150ps; ID: 250ps; EX: 50ps; MEM: 200ps; WB: 100ps

Assume the instructions executed by the processor break down as follows:
ALU: 45%; BEQ: 20%; LW: 20%; SW: 15%

Choices:
(A) The clock cycle time in a pipelined processor is 350 ps.
(B) The total latency of an LW instruction in a non-pipelined processor is 850 ps.
(C) The total latency of an LW instruction in a pipelined processor is 1250 ps.
(D) The utilization of the data memory is 35% (Assume there are no stalls or hazards).

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

  1. 管線化時脈週期(Pipelined Clock Cycle Time):
    在管線化(Pipelined)處理器中,時脈週期取決於延遲最長的瓶頸階段(Bottleneck Stage):
    Tclock, pipelined=max⁡(TIF,TID,TEX,TMEM,TWB)T_{\text{clock, pipelined}} = \max(T_{\text{IF}}, T_{\text{ID}}, T_{\text{EX}}, T_{\text{MEM}}, T_{\text{WB}})
    在未額外計入管線暫存器延遲(Pipeline Register Delay)與時脈偏移(Clock Skew)的情況下,時脈週期即為各階段延遲之最大值。

  2. 非管線化指令延遲(Non-pipelined Instruction Latency):
    在非管線化架構(如單週期處理器)中,單一指令的執行延遲為該指令所需經過的所有功能階段延遲之總和:
    Latencynon-pipelined=∑Trequired stages\text{Latency}_{\text{non-pipelined}} = \sum T_{\text{required stages}}

  3. 管線化指令延遲(Pipelined Instruction Latency):
    在同步五階段管線化處理器中,每個管線階段的停留時間固定為一個時脈週期 Tclock, pipelinedT_{\text{clock, pipelined}}。一條指令由取指至寫回完整經過五個階段(IF, ID, EX, MEM, WB)所需的總時間(Latency)為:
    Latencypipelined=Nstages×Tclock, pipelined\text{Latency}_{\text{pipelined}} = N_{\text{stages}} \times T_{\text{clock, pipelined}}

  4. 資料記憶體使用率(Data Memory Utilization):
    在無管線停頓(Stalls)或冒險(Hazards)的理想狀態下,管線每個週期穩定推進一條指令。資料記憶體(Data Memory)僅在 MEM 階段被存取,因此資料記憶體的使用率即為需要存取記憶體的指令(載入與儲存指令)所佔比例之總和:
    UtilizationData Memory=PLW+PSW\text{Utilization}_{\text{Data Memory}} = P_{\text{LW}} + P_{\text{SW}}


數值推導與計算

已知五階段資料路徑之延遲:

  • TIF=150 psT_{\text{IF}} = 150 \text{ ps}
  • TID=250 psT_{\text{ID}} = 250 \text{ ps}
  • TEX=50 psT_{\text{EX}} = 50 \text{ ps}
  • TMEM=200 psT_{\text{MEM}} = 200 \text{ ps}
  • TWB=100 psT_{\text{WB}} = 100 \text{ ps}

指令組合比例(Instruction Mix):

  • ALU=45%\text{ALU} = 45\%
  • BEQ=20%\text{BEQ} = 20\%
  • LW=20%\text{LW} = 20\%
  • SW=15%\text{SW} = 15\%

詳細推導如下:

  1. 管線化時脈週期:
    Tclock, pipelined=max⁡(150,250,50,200,100)=250 psT_{\text{clock, pipelined}} = \max(150, 250, 50, 200, 100) = 250 \text{ ps}

  2. LW 指令在非管線化處理器之總延遲:
    LW 指令必須依序執行五個階段(IF, ID, EX, MEM, WB):
    LatencyLW, non-pipelined=150+250+50+200+100=750 ps\text{Latency}_{\text{LW, non-pipelined}} = 150 + 250 + 50 + 200 + 100 = 750 \text{ ps}

  3. LW 指令在管線化處理器之總延遲:
    LW 指令經歷 5 個管線階段,每個階段耗時 1 個時脈週期(250 ps250 \text{ ps}):
    LatencyLW, pipelined=5×250 ps=1250 ps\text{Latency}_{\text{LW, pipelined}} = 5 \times 250 \text{ ps} = 1250 \text{ ps}

  4. 資料記憶體(Data Memory)使用率:
    在五階段管線中,各類指令對 Data Memory(MEM 階段)的使用情形:

    • LW:自資料記憶體讀取資料(使用)
    • SW:將資料寫入資料記憶體(使用)
    • ALU、BEQ:不存取資料記憶體(不使用)

    在無 Stall/Hazard 情況下,每個時脈週期皆有一條指令處於 MEM 階段,故資料記憶體的使用率為:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 13 題5 分

Consider a processor with a load delay slot: this means that the value loaded into a destination register (Rd) is not available for use in the instruction immediately following the load. Assume the following code sequence runs on this processor (with RISC-V ISA).

addi x3, x0, 0x18  // x3 = 24
lw   x4, 0(x3)     // x4 = Mem[x3+0]
addi x3, x0, 0x18  // x3 = 24
lw   x4, 0(x3)     // x4 = Mem[x3+0]

Suppose that MEM[32] = -4, and the load delay slot is ignored. What will be the value (in decimal) stored in the register x4 after all the code is executed?

Choices:
(A) 48
(B) 14
(C) -8
(D) 40
(E) None of the above.

登入後即可作答並保存紀錄。

這一題的完整詳解

步驟

  1. 第 1 條 addi x3, x0, 0x18 → x3 = 0x18 = 24。
  2. 第 2 條 lw x4, 0(x3) 以 x3 為基址取記憶體位址 24 的字,得到 x4 = MEM[24]。
  3. 第 3 條 addi x3, x0, 0x18 再次把 x3 設為 24(與第 1 條相同)。
  4. 第 4 條 lw x4, 0(x3) 同樣以地址 24 讀取,覆寫 x4 為 MEM[24]。
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 14 題4 分

(Question Group A: 8points – 2 questions) Assume we have a 2-way set associative cache that employs a write-back policy and uses a perfect LRU mechanism to decide which block to evict. The tag store, which includes bits for valid status, dirty status, and LRU tracking, requires 13×2813 \times 2^8 bits of storage space. The cache implements virtual indexing with physical tagging. The system has a virtual address space of 1 MB, uses pages of 1 KB in size, and each cache block is 8 bytes long. Answer the following questions.

  1. (4 points) What is the size of the cache's data store?
    (A) 1KB
    (B) 2KB
    (C) 4KB
    (D) 8KB

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

  1. 快取記憶體儲存結構:
    快取(Cache)由資料儲存區(Data Store)​與標籤儲存區(Tag Store / Directory)​兩大部分組成:

    • Data Store:實際儲存資料區塊(Block / Line)的內容。
    • Tag Store:儲存用於位址比對與快取管理所需的元資料(Metadata),包含實體標籤(Physical Tag)、有效位元(Valid bit)、髒位元(Dirty bit)及替換策略追蹤位元(如 LRU bit)。
  2. 2-way 集合關聯式快取的各欄位與位元需求:
    設快取共有 SS 個集合(Sets),每個集合包含 2 個路(Ways / Blocks):

    • Valid bit(有效位元):用以標記區塊資料是否有效,每個區塊各需 1 位元,每集合共需 2×1=22 \times 1 = 2 位元。
    • Dirty bit(髒位元):因快取採用寫回政策(Write-Back),每個區塊需 1 位元標記是否被修改過,每集合共需 2×1=22 \times 1 = 2 位元。
    • LRU bit(替換追蹤位元):在 2-way 集合關聯式架構下,一組僅有 2 個區塊,只需 1 個位元(例如 0 代表 Way 0 較久未用、1 代表 Way 1 較久未用)即可完美記錄兩區塊的存取先後順序(Perfect LRU)。
    • Physical Tag(實體標籤):設每個區塊之實體標籤長度為 TT 位元,一組有 2 個區塊,共需 2T2T 位元。
  3. VIPT(Virtually Indexed, Physically Tagged)架構特性:

    • 虛擬位址(Virtual Address)提供區塊偏移(Block Offset)與快取索引(Index)。
    • 實體位址(Physical Address)則提供實體標籤(Physical Tag)以進行快取比對。
    • 若分頁大小為 PP,則頁內偏移(Page Offset)為 p=log⁡2(P)p = \log_2(P) 位元;區塊大小為 BB,則區塊內偏移為 b=log⁡2(B)b = \log_2(B) 位元。
    • 頁內偏移中可直接提供給快取索引的位元數為 p−bp - b。若快取索引位元數 s>p−bs > p - b,則超出部分必來自虛擬頁碼(Virtual Page Number, VPN)。

解題步驟

  1. 建立每個集合的 Tag Store 位元方程式:
    每個集合(Set)在 Tag Store 中需儲存:

    • 2 個區塊的 Tag 位元:2×T2 \times T
    • 2 個區塊的 Valid 位元:2×1=22 \times 1 = 2
    • 2 個區塊的 Dirty 位元:2×1=22 \times 1 = 2
    • 該集合共用的 1 個 LRU 位元:11

    因此,每個集合在 Tag Store 中所佔的位元數為:

    Bits per set=2T+2+2+1=2T+5\text{Bits per set} = 2T + 2 + 2 + 1 = 2T + 5
  2. 求解集合數 SS 與標籤長度 TT:
    題幹給定 Tag Store 總容量為 13×28 bits13 \times 2^8\text{ bits}。
    設快取的集合數為 SS,則:

    S×(2T+5)=13×28S \times (2T + 5) = 13 \times 2^8
    • 依快取設計原理,集合數 SS 必為 2 的冪次(即 S=2sS = 2^s)。
    • 而 2T+52T + 5(其中 TT 為標籤長度之非負整數)必為奇整數。
    • 在因數分解 13×2813 \times 2^8 中,唯一的奇數因數即為 1313。

    由此可唯一解出:

    2T+5=13  ⟹  2T=8  ⟹  T=4 bits2T + 5 = 13 \implies 2T = 8 \implies T = 4\text{ bits} S=28=256 個集合S = 2^8 = 256\text{ 個集合}

    (對應之快取索引長度為 s=log⁡2(256)=8 bitss = \log_2(256) = 8\text{ bits})。

  3. 計算快取資料儲存區(Data Store)容量:
    由題目條件:

    • 集合數 S=256S = 256
    • 關聯度(Associativity)W=2W = 2(2-way)
    • 區塊大小(Block Size)B=8 BytesB = 8\text{ Bytes}

    資料儲存區總容量為:

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 15 題4 分

(Question Group A: 8points – 2 questions) Assume we have a 2-way set associative cache that employs a write-back policy and uses a perfect LRU mechanism to decide which block to evict. The tag store, which includes bits for valid status, dirty status, and LRU tracking, requires 13×2813 \times 2^8 bits of storage space. The cache implements virtual indexing with physical tagging. The system has a virtual address space of 1 MB, uses pages of 1 KB in size, and each cache block is 8 bytes long. Answer the following questions.

  1. (4 points) How many bits of the virtual index come from the virtual page number?
    (A) 1 bit
    (B) 2 bits
    (C) 3 bits
    (D) 4 bits

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

  1. VIPT(Virtually Indexed, Physically Tagged)架構:

    • 處理器發出虛擬位址(Virtual Address, VA)以存取快取。
    • 快取索引(Index)與區塊偏移(Block Offset):直接取自虛擬位址的低位元部分,使快取尋址可與 TLB 轉譯平行進行,降低存取延遲。
    • 實體標籤(Physical Tag):由 TLB 將虛擬頁號(VPN)轉譯為實體頁號(PPN)後,與快取目錄中儲存的實體標籤進行比對。
    • 分頁限制與重名(Aliasing)問題:
      • 設分頁大小為 PP,則頁內偏移(Page Offset, PO)為 p=log⁡2(P)p = \log_2(P) 位元。
      • 設快取區塊大小為 BB,則區塊內偏移(Block Offset)為 b=log⁡2(B)b = \log_2(B) 位元。
      • 頁內偏移扣除區塊偏移後,剩下的 p−bp - b 位元在虛擬位址與實體位址中是完全相同的。
      • 若快取索引位元數 s≤p−bs \le p - b,則索引完全落在頁內偏移內,不會有快取重名問題(VIPT 實質等同於 PIPT)。
      • 若快取索引位元數 s>p−bs > p - b,則索引中超出 p−bp - b 的高位元(共 s−(p−b)s - (p - b) 位元)必須跨越頁面邊界,直接取自虛擬頁號(VPN)。
  2. 2-way 集合關聯式快取的標籤目錄(Tag Store)位元結構:
    設快取共有 SS 個集合(Sets),每個集合包含 2 個路(Ways / Blocks):

    • Valid bit(有效位元):每個區塊需 1 位元以標示資料是否有效,每組共 2 位元。
    • Dirty bit(髒位元):因採用寫回策略(Write-Back),每個區塊需 1 位元以記錄資料是否被修改,每組共 2 位元。
    • LRU tracking(替換策略位元):在 2-way 架構下,一組僅有 2 個區塊,只需 1 個位元(0 代表 Way 0 較久未用、1 代表 Way 1 較久未用)即可達到嚴格且完美的 LRU 順序追蹤(Perfect LRU)。
    • Physical Tag(實體標籤):設實體標籤長度為 TT 位元,一組有 2 個區塊,共需 2T2T 位元。

    因此,每個集合在 Tag Store 中所佔的位元數為:

    Bits per set=2×(T+1+1)+1=2T+5\text{Bits per set} = 2 \times (T + 1 + 1) + 1 = 2T + 5

解題步驟

  1. 由 Tag Store 總容量求解集合數 SS 與標籤長度 TT:
    題目給定標籤目錄(Tag Store)之總儲存容量為 13×28 bits13 \times 2^8\text{ bits}。
    設快取的集合數為 S=2sS = 2^s,則有方程式:

    S×(2T+5)=13×28S \times (2T + 5) = 13 \times 2^8
    • 快取集合數 SS 依架構設計必為 2 的整數冪次(2s2^s)。
    • 2T+52T + 5(其中 TT 為標籤長度,為非負整數)必為奇整數。
    • 將 13×2813 \times 2^8 進行因數分解,其唯一的奇數因數為 13。

    因此可唯一確定:

    2T+5=13  ⟹  2T=8  ⟹  T=4 bits2T + 5 = 13 \implies 2T = 8 \implies T = 4\text{ bits} S=28=256 個集合S = 2^8 = 256\text{ 個集合}

    由此得知快取的索引位元數為:

    s=log⁡2(256)=8 bitss = \log_2(256) = 8\text{ bits}
  2. 分析虛擬位址(Virtual Address)之位元劃分:

    • 虛擬位址空間 =1 MB=220 Bytes= 1\text{ MB} = 2^{20}\text{ Bytes},故虛擬位址總長度為 20 位元(VA[19:0])。
    • 分頁大小 =1 KB=210 Bytes= 1\text{ KB} = 2^{10}\text{ Bytes},故頁內偏移(Page Offset, PO)佔低 10 位元(VA[9:0])。
    • 虛擬頁號(VPN)佔高 10 位元(VA[19:10])。
    • 快取區塊大小 =8 Bytes=23 Bytes= 8\text{ Bytes} = 2^3\text{ Bytes},故區塊內偏移(Block Offset)佔低 3 位元(VA[2:0])。
  3. 計算虛擬索引中來自虛擬頁號(VPN)的位元數:

    • 虛擬位址的各欄位結構如下:
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 16 題8 分

Storing and re-loading data from the main memory during the loop cause a slowdown.

Assume that you have a new instruction called fmv.d that executes in a single cycle. This instruction performs register move operations. For instance: 'fmv.d rd, rs1' — writes the value of floating-point register rs1 into the floating-point register rd.

If the fmv.d instruction is used to optimize the code's performance by using registers to carry the data between iterations of the loop, how many clock cycles are needed to execute this code?

addiu x12, x10, x5
addiu x11, x11, 16

LOOP:
fld f0, -16(x11)
fld f1, 0(x11)
fadd.d f2, f0, f1
fsd f2, 0(x11)
addiu x11, x11, 8
ble x11, x12, LOOP

Here, an instruction's latency represents how many clock cycles must pass before another instruction can use its result. The floating point instructions have the following associated latencies (in cycles):
fadd.d = 4
fld = 6
fsd = 1

Choices:
(A) 15984
(B) 17587
(C) 25280
(D) 26583
(E) None of the above.

登入後即可作答並保存紀錄。

這一題的完整詳解

步驟概覽

  1. 迴圈次數
    起始位址 : x11 ← x11 + 16
    每次遞增 : x11 ← x11 + 8
    結束位址 : x12 = x10 + x5

    迴圈執行次數

N=x5−168+1=x58−1N = \frac{x5-16}{8}+1=\frac{x5}{8}-1

  1. 指令排程(使用 fmv.d)

    • 第一輪 必須從記憶體載入兩個浮點值:
      fld f0, -16(x11)、fld f1, 0(x11)
    • 之後每輪 只需載入一個新值 (fld f1) 並用 fmv.d 把前一次的結果搬到 f0。

    各指令延遲(cycle)

fmv.d=1,fld=6,fadd.d=4,fsd=1,addiu=1,ble=1\begin{aligned} &\text{fmv.d}=1,\quad \text{fld}=6,\quad \text{fadd.d}=4,\\ &\text{fsd}=1,\quad \text{addiu}=1,\quad \text{ble}=1 \end{aligned}
  1. 每輪所需週期

    • 第一輪

6+6+4+1+1+1=20 cycles6+6+4+1+1+1 = 20\ \text{cycles}

  • 其餘 N−1N-1 輪

1+6+4+1+1+1=15 cycles1+6+4+1+1+1 = 15\ \text{cycles}

🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

第 18 題6 分

Consider a computer system equipped with a single CPU with the following organization: It contains two cores, where each core implements superscalar processing with two functional units and can execute instructions out of order. The cores operate in single-threaded mode, meaning each core can only execute one thread at a time.

Two threads, X and Y, must be executed on this CPU. Each thread consists of the following sequence of operations. Unless noted or encountered a hazard, you can assume all operations take a single cycle to execute.

XY
A1: takes two cycles to executeB1: no dependencies
A2: depends on the result of A1B2: conflicts for a functional unit with B1
A3: conflicts for a functional unit with A1B3: no dependencies
A4: depends on the result of A2B4: depends on the result of B2

How many cycles will it take to execute these two threads? How many issue slots are wasted because of hazards?

(A) 4 cycles, 7 slots wasted
(B) 4 cycles, 3 slots wasted
(C) 5 cycles, 5 slots wasted
(D) 6 cycles, 7 slots wasted

登入後即可作答並保存紀錄。

這一題的完整詳解

核心觀念

本題考兩核心處理器的排程與功能單元衝突。每個核心每週期有兩個功能單元,因此最多可同時執行兩個操作;兩個核心分別執行執行緒 X、Y。

排程時要遵守:

  • 資料相依:後續操作必須等前一操作完成,才能執行。
  • 功能單元衝突:兩個操作需要同一功能單元時,不能重疊執行。
  • 超純量與亂序執行:每週期最多執行兩個操作;若操作彼此獨立且功能單元可用,可以同時執行。
  • 浪費的槽位:在四週期的總執行期間,每個核心每週期有兩個功能單元槽位。未執行操作的槽位計為浪費;執行兩週期的 A1 在第二週期仍占用一個功能單元槽位。

解題方法與排程

先分別排程兩條執行緒,再以較長的執行緒決定總週期數。

週期核心 1:執行緒 X核心 2:執行緒 Y
1A1(執行中)、空槽B1、B3
2A1(續行)、空槽B2、空槽
3A2、A3B4、空槽
4A4、空槽空槽、空槽

X 的 A1 需兩週期,因此 A2 必須等 A1 完成後才能執行。A3 與 A1 有功能單元衝突,也要等 A1 完成;第 3 週期 A2、A3 可同時執行。A4 依賴 A2,因此在第 4 週期執行。X 共需 4 週期。

Y 的 B1、B3 沒有相依,可在第 1 週期同時執行。B2 與 B1 有功能單元衝突,排在第 2 週期;B4 依賴 B2,排在第 3 週期。Y 共需 3 週期。

兩條執行緒分別在不同核心執行,總週期數由較長的 X 決定,因此共需 4 週期。

浪費槽位計算

四週期內共有:

2 個核心×2 個功能單元×4 週期=16 個槽位2\text{ 個核心}\times 2\text{ 個功能單元}\times 4\text{ 週期}=16\text{ 個槽位}

A1 執行兩週期,占用兩個槽位;其餘七個操作各占一個槽位,合計占用九個槽位。因此浪費的槽位數為:

16−9=716-9=7
🔒

後續完整解題步驟與【答案】

免費註冊,享三天全站完整詳解閱覽。

免費註冊

其他考古題