115 年 國立成功大學資訊工程系碩士班《計算機組織與系統》

📄 試題原卷 免費註冊後即可對照原始考卷 PDF免費註冊
📄 以下 5 題共用同一段題幹

READ BEFORE ANSWERING: Reference Concepts

Please read the following principles to assist your reasoning:

  1. Cache Coherence: The MESI protocol is a foundational hardware-based cache coherence mechanism designed to maintain data consistency across the private caches of a multi-core system. It functions by assigning one of four distinct states to every cache line—Modified (M), Exclusive (E), Shared (S), or Invalid (I)—and monitoring bus traffic through a process known as “snooping.” Under this protocol, if a core intends to write to a data block currently in the Shared state, it must first broadcast an Invalidate signal to all other cores to force their local copies into the Invalid state; this allows the initiating core to acquire exclusive ownership and transition to the Modified state, ensuring that no two cores can hold conflicting versions of the same data and that all processors always access the most recent values.

  2. Pipeline Overhead: Pipelining increases Throughput. However, due to “Pipeline Registers” inserted between stages, the setup/propagation time causes the total Latency of a “single instruction” to increase slightly compared to a non-pipelined design.

  3. Memory Hierarchy Goals: L1 Cache optimizes for Speed (Hit Time). L2 Cache optimizes for reducing the Global Miss Rate (intercepting Misses before they reach the slow DRAM). L2 is typically slower and larger than L1.

  4. Branch Limits: In fixed-length ISAs (e.g., RISC-V), the Offset for conditional branches is limited (e.g., 12-bit, approx KB). Long-distance jumps (e.g., 1MB) require compiler workarounds (like trampolines).

  5. Spatial Locality: Cache moves data in Blocks. If a program’s access Stride is too large (e.g., jumping across rows in a large 2D array), it fails to utilize the data within the block, leading to a high Miss Rate.

Instructions:

  1. Use the Reference Concepts above to assist your reasoning.
  2. WARNING: Column B contains “Distractors” (Incorrect Statements).
  3. Column B has MORE options than Column A. Select the BEST match for each item in Column A.

Column B: Characteristics & Distractors

(A) Primarily increases Throughput; single instruction Latency slightly increases due to register overhead.
(B) Splits execution to reduce single instruction Latency to exactly 1/N1/N.
(C) Hardware guarantees zero Data Hazards between any instructions.
(D) Designed to intercept L1 Misses and reduce Global Miss Rate to DRAM.
(E) Designed to be smaller than L1 to achieve higher speeds.
(F) Uses block transfers to exploit Spatial Locality.
(G) Optimizes access for non-contiguous structures like Linked Lists.
(H) Requires “Invalidate” signal before writing to Shared data.
(I) Managed by OS; triggers Context Switch on write.
(J) Used for conditional branches with limited range (e.g., ±4KB\pm 4\text{KB}).
(K) Allows jumping to any address without bit-width limits.

第 1-(1) 題

Pipelining

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

這一題的完整詳解

核心觀念

管線化(Pipelining)將指令執行拆成多個階段,並在階段之間加入管線暫存器。多條指令可同時處於不同階段,因此主要提升的是吞吐量;單一指令仍須依序經過各階段,且管線暫存器帶來額外延遲,因此單一指令的總延遲不會因此降為原本的 1/N1/N。

若管線分成 NN 個階段,時脈週期約為最慢階段的延遲加上暫存器開銷;管線填滿後,理想情況下每個週期可完成一條指令。單一指令的管線延遲則約為 NN 個週期。

解題方法

題目要將「Pipelining」配對到最符合其效果的敘述。判斷重點是分清楚:

  • 吞吐量:單位時間內完成的指令數,管線化主要提升此指標。
  • 單一指令延遲:一條指令從開始到完成所需時間;管線暫存器會帶來額外開銷,並不會縮短成原本的 1/N1/N。

因此應選擇描述「主要提升吞吐量、單一指令延遲略增」的選項。

選項分析

  • (A) 正確。 管線化主要提升吞吐量;階段間的管線暫存器會增加額外開銷,使單一指令延遲略為增加。
  • (B) 錯誤。 管線化不會將單一指令的延遲降至恰好 1/N1/N。它把多條指令的不同階段重疊執行,提升的是整體完成速率。
  • (C) 錯誤。 管線化不保證資料冒險消失。
🔒

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

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

免費註冊

第 1-(2) 題

L2 Cache

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

這一題的完整詳解

核心觀念

L2 Cache 是位於 L1 Cache 與主記憶體之間的第二層快取。它通常比 L1 容量大、存取速度慢,主要目標是在 L1 未命中時提供另一層快速存取機會,減少必須前往 DRAM 的次數。

整體平均記憶體存取時間可表示為:

AMAT=L1 Hit Time+L1 Miss Rate×L1 Miss Penalty\text{AMAT} = \text{L1 Hit Time} + \text{L1 Miss Rate} \times \text{L1 Miss Penalty}

加入 L2 後,L1 未命中的代價會受到 L2 命中率影響;只有 L1、L2 都未命中時,才需要承受前往 DRAM 的高延遲。因此,L2 的重點是降低到達主記憶體的整體未命中率。

解題方法

題目要將「L2 Cache」配對到最符合的敘述。判斷關鍵是分清楚快取階層的設計目標:L1 著重快速命中,L2 著重接住 L1 未命中的資料,降低 DRAM 存取頻率。符合這項功能的是選項 (D)。

選項分析

  • (A) 錯誤。 這描述的是管線化:主要增加指令吞吐量,管線暫存器則可能使單一指令延遲略為增加,與 L2 Cache 的功能無關。
  • (B) 錯誤。 管線化不會讓單一指令延遲精確降為 1/N1/N;此敘述也不是 L2 Cache 的設計目的。
  • (C) 錯誤。 硬體不會保證所有指令之間都沒有資料危障。危障仍須由轉送、停滯、排程等機制處理。
  • **(D) 正確。
🔒

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

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

免費註冊

第 1-(3) 題

Cache Block (Line)

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

這一題的完整詳解

核心觀念

Cache Block(快取區塊,也稱 Cache Line)是快取與較低層記憶體之間搬移資料的基本單位。處理器讀取某個位址時,快取通常會載入包含該位址的一整個區塊,而非只載入單一位元組或字組。

載入區塊可利用空間區域性:程式存取某個位址後,常會繼續存取附近的位址。因此,區塊中尚未被讀取的資料也可能很快派上用場,降低後續存取發生快取未命中的機率。

解題方法

題目要將「Cache Block (Line)」配對到最能描述它的特性。判斷重點是:Cache Block 的作用是以區塊為單位搬移資料,並藉此利用空間區域性。Column B 中符合此描述的是 (F)。

選項分析

  • (A) 錯誤。這描述的是管線化的主要效益與管線暫存器帶來的延遲影響,和快取區塊無關。
  • (B) 錯誤。管線化不會讓單一指令的延遲精確降為原本的 1/N1/N。
  • (C) 錯誤。硬體不會保證所有指令間都沒有資料冒險;資料冒險仍須由管線控制機制處理。
  • (D) 錯誤。這描述 L2 快取攔截 L1 未命中、降低到 DRAM 的全域未命中率。
🔒

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

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

免費註冊

第 1-(4) 題

MESI Protocol

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

這一題的完整詳解

核心觀念

MESI 是多核心處理器常用的快取一致性協定。它以每條快取區塊的狀態追蹤資料是否有效,以及該資料是否由其他核心共享。四種狀態為:

  • M(Modified,已修改):此核心持有唯一副本,且內容已不同於主記憶體。
  • E(Exclusive,獨占):此核心持有唯一副本,內容與主記憶體一致。
  • S(Shared,共享):多個核心可持有相同資料副本,內容與主記憶體一致。
  • I(Invalid,無效):此快取區塊不可使用。

題目考查 MESI 的一致性機制,尤其是核心要寫入處於 S 狀態的資料時,如何取得獨占寫入權。

解題方法

判斷選項時,抓住 MESI 的關鍵動作:若某核心要修改一份由多個核心共享的快取資料,它會發出 Invalidate(失效)訊號,使其他核心持有的副本轉為 I,再取得該區塊的獨占權並進行寫入。這樣可以避免其他核心繼續讀取過期副本。

因此,最符合 MESI 的敘述是選項 (H)。

選項分析

  • (A) 錯誤。 描述的是管線化:管線主要提高指令吞吐量,管線暫存器會帶來額外延遲。這不是 MESI 的功能。
  • (B) 錯誤。 管線化不會把單一指令的延遲精確降為原本的 1/N1/N;此敘述也與快取一致性無關。
  • **(C) 錯誤。
🔒

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

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

免費註冊

第 1-(5) 題

PC-relative Addressing

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

這一題的完整詳解

核心觀念

PC-relative addressing(程式計數器相對定址)以目前指令附近的程式計數器(PC)作為基準,將指令中的位移量加到 PC 上,計算分支目標位址。常見形式為:

目標位址=PC 基準位址+符號延伸後的位移量\text{目標位址} = \text{PC 基準位址} + \text{符號延伸後的位移量}

位移量通常由指令中的有限位元欄位表示,因此可達範圍有限。條件分支常採用這種定址方式;若目標超出範圍,編譯器可能改用多條指令或跳板(trampoline)處理。

解題方法

題目要把「PC-relative Addressing」配對至最符合的敘述。判斷重點是:它以 PC 加上有限寬度的位移量來指定目標,因此最符合 (J) 用於範圍有限的條件分支。

選項分析

  • (A) 錯誤。 描述的是管線化:主要提高吞吐量,管線暫存器可能略增單一指令延遲。
  • (B) 錯誤。 管線化不會把單一指令的延遲降到恰好 1/N1/N;管線化的主要效益是提高吞吐量。
  • (C) 錯誤。 硬體不會保證所有指令之間都沒有資料危障;危障仍須由硬體偵測、轉送或停滯處理。
  • **(D) 錯誤。
🔒

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

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

免費註冊
📄 以下 6 題共用同一段題幹

READ BEFORE ANSWERING: Reference Concepts

Please read the following principles to assist your reasoning:

  1. Cache Coherence: The MESI protocol is a foundational hardware-based cache coherence mechanism designed to maintain data consistency across the private caches of a multi-core system. It functions by assigning one of four distinct states to every cache line—Modified (M), Exclusive (E), Shared (S), or Invalid (I)—and monitoring bus traffic through a process known as “snooping.” Under this protocol, if a core intends to write to a data block currently in the Shared state, it must first broadcast an Invalidate signal to all other cores to force their local copies into the Invalid state; this allows the initiating core to acquire exclusive ownership and transition to the Modified state, ensuring that no two cores can hold conflicting versions of the same data and that all processors always access the most recent values.

  2. Pipeline Overhead: Pipelining increases Throughput. However, due to “Pipeline Registers” inserted between stages, the setup/propagation time causes the total Latency of a “single instruction” to increase slightly compared to a non-pipelined design.

  3. Memory Hierarchy Goals: L1 Cache optimizes for Speed (Hit Time). L2 Cache optimizes for reducing the Global Miss Rate (intercepting Misses before they reach the slow DRAM). L2 is typically slower and larger than L1.

  4. Branch Limits: In fixed-length ISAs (e.g., RISC-V), the Offset for conditional branches is limited (e.g., 12-bit, approx KB). Long-distance jumps (e.g., 1MB) require compiler workarounds (like trampolines).

  5. Spatial Locality: Cache moves data in Blocks. If a program’s access Stride is too large (e.g., jumping across rows in a large 2D array), it fails to utilize the data within the block, leading to a high Miss Rate.

Instructions:

  1. Use the Reference Concepts above to assist your reasoning.
  2. WARNING: Column B contains “Distractors” (Incorrect Statements).
  3. Column B has MORE options than Column A. Select the BEST match for each item in Column A.

Column B: Behavior & Distractors

(A) Total program execution time instantly becomes 1/5th.
(B) Individual instruction Latency may slightly increase due to overhead.
(C) Destroys Spatial Locality; Miss Rate increases significantly.
(D) Spatial locality remains excellent regardless of access order.
(E) Can write directly since it is Shared; no need to signal.
(F) Must acquire Bus Exclusivity and Invalidate Core B's copy.
(G) Hardware auto-extends 12-bit offset to 32-bit.
(H) Compiler uses 'Invert Branch + Unconditional Jump' combination.
(I) Access latency is faster than DRAM but slower than L1.
(J) L2 speed is identical to L1, zero latency difference.
(K) Triggers Write-Through to HDD directly.

第 2-(6) 題

Convert Single-cycle to 5-stage Pipeline

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

這一題的完整詳解

核心觀念

把單週期處理器改成五級管線:指令切成 IF、ID、EX、MEM、WB 五段,段與段之間插入管線暫存器。管線填滿後每個週期可完成一條指令,吞吐量大幅提高;但每條指令仍要依序走完五段,而且每經過一段都多了管線暫存器的設定/傳遞時間,所以單一指令的延遲不但不會變成 1/5,反而可能略為增加(參考概念第 2 點)。

選項分析

🔒

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

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

免費註冊

第 2-(7) 題

Accessing 2D array via A[j][i]A[j][i] (Strided)

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

這一題的完整詳解

核心觀念

C 語言的二維陣列以列優先(row-major)儲存:A[j][i]A[j][i] 的位址是 base+(jN+i)×s\text{base}+(jN+i)\times s(NN 為每列元素數、ss 為元素大小)。若內層迴圈讓 jj 變動,相鄰兩次存取相差 N⋅sN\cdot s 位元組,等於每次跨一整列(strided access)。

快取以區塊為單位搬資料(參考概念第 5 點):步幅太大時,搬進來的區塊只用到一個元素就被換掉,其餘資料都浪費了。

選項分析

🔒

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

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

免費註冊

第 2-(8) 題

Core A writes to “Shared” (S) data.

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

這一題的完整詳解

核心觀念

MESI 快取一致性協定中,Shared(S) 狀態表示多個核心持有同一份資料的唯讀副本。核心要寫入 S 狀態的資料時,必須先在匯流排上取得寫入權,並廣播 Invalidate,讓其他核心的副本變成 Invalid,自己才轉為 Modified 後寫入(參考概念第 1 點)。否則其他核心會繼續讀到過期的舊值。

選項分析

🔒

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

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

免費註冊

第 2-(9) 題

Branch target is 1MB away.

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

這一題的完整詳解

核心觀念

固定長度指令集(如 RISC-V)的條件分支只有 12 位元左右的位移欄位,可達範圍約 ±4\pm4 KB(參考概念第 4 點)。目標在 1 MB 外時,單一條件分支到不了。

解題方法

編譯器(或組譯器)的標準作法:把條件反向,讓它跳過下一條指令;下一條放一個範圍較大的無條件跳躍(如 RISC-V 的 `jal`,約 ±1\pm1 MB;更遠可再用 `auipc`+`jalr`)。例如

```text
beq x1, x2, far_target # 目標太遠,無法直接編碼
```

改寫為

🔒

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

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

免費註冊

第 2-(10) 題

L1 Miss but L2 Hit

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

這一題的完整詳解

核心觀念

記憶體階層:L1 小而快,著重命中時間;L2 較大、較慢,目的是在 L1 未命中時攔截存取,避免一路走到很慢的 DRAM(參考概念第 3 點)。所以「L1 未命中、L2 命中」時,存取時間介於兩者之間。

選項分析

  • (I) 正確。 存取延遲比 DRAM 快、但比 L1 慢——要多付 L2 的存取時間,卻省下了 DRAM 的長延遲。
  • **(J) 錯誤。
🔒

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

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

免費註冊

第 1 題2 分

[2%] If a few bits in an HDD sector or NVM page are corrupted, the controller can recover the correct values using ECC only if

(A) the bit corruption is limited to the data portion of the sector/page.

(B) the bit corruption hasn’t occurred in the ECC of the sector/page.

(C) the bit corruption hasn’t occurred in the ECC or the header of the sector/page.

(D) the number of bits corrupted is low irrespective of where the corruption occurs in the sector/page.

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

這一題的完整詳解

核心觀念

錯誤更正碼(ECC,如 Hamming、BCH、Reed–Solomon)把資料位元與檢查位元一起組成一個碼字(codeword)。寫入時由資料算出檢查位元;讀取時重新計算並比對,得到的症狀(syndrome)可以指出哪些位元出錯並把它改回來。

關鍵性質:ECC 的更正能力是「碼字中最多 tt 個位元出錯都能更正」,這個碼字包含檢查位元本身。例如 Hamming 碼的單一位元錯誤,無論錯在資料位元或檢查位元,症狀都會指出那個位置並更正。所以能否復原,取決於錯了幾個位元,而不是錯在哪裡。

(Silberschatz《Operating System Concepts》:只要損壞的位元很少,控制器就能找出改變的位元並算出正確值;磁碟扇區/NVM 頁的標頭與資料區也各有自己的錯誤更正保護。)

🔒

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

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

免費註冊

第 2 題2 分

[2%] A full bootstrap program

(A) is stored in ROM to ensure it cannot be infected by any virus.

(B) is part of an operating system.

(C) may be infected by viruses.

(D) runs after the operating system has been loaded in memory.

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

這一題的完整詳解

核心觀念

開機程序由開機韌體中的初始程式開始,接著載入磁碟上的開機程式,再由開機程式載入作業系統核心。初始程式通常存放在 ROM 或快閃記憶體;完整開機程式則可能存放在磁碟的開機區,因此有遭病毒感染的風險。

解題方法

依序判斷各選項是否符合開機程式的用途、存放位置與執行時機。關鍵在於區分韌體中的初始開機程式與磁碟上的完整開機程式;前者先執行並載入後者,完整開機程式再載入作業系統。

選項分析

🔒

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

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

免費註冊

第 3 題2 分

[2%] IBM’s indexed sequential access method (ISAM) ____.

(A) uses an index file for sequential access.

(B) uses a small master index (kept in memory) that points to disk blocks of a secondary index, while the secondary index blocks point to the actual file blocks.

(C) uses the pointers (kept in memory) of the actual file blocks.

(D) uses the pointers (kept in memory) that point to disk blocks of a master index which points to disk blocks of a secondary index, while the secondary index blocks point to the actual file blocks.

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

這一題的完整詳解

核心觀念

IBM 的索引循序存取法(ISAM)把資料記錄依索引鍵排序,並以多層索引協助定位資料區塊。其索引通常是稀疏索引:索引項目指向資料區塊,而不是逐筆記錄都建立索引。

本題考的是 ISAM 的索引層級與索引所在位置。其指標路徑為:

記憶體中的主索引→磁碟上的次級索引區塊→實際資料檔案區塊\text{記憶體中的主索引} \rightarrow \text{磁碟上的次級索引區塊} \rightarrow \text{實際資料檔案區塊}

找到起始區塊後,可以依資料排序順序循序讀取後續記錄。ISAM 的索引結構是靜態的;新增記錄放不進原資料區塊時,會使用溢位區域。

解題方法

逐一比對選項描述的指標路徑:主索引是否留在記憶體、次級索引是否位於磁碟,以及次級索引是否指向實際資料區塊。只有(B)完整符合「記憶體主索引 → 磁碟次級索引 → 資料區塊」的結構。

選項分析

🔒

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

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

免費註冊

第 4 題2 分

[2%] Which of the following is true about dynamic storage allocation?

(A) Worst fit provides the best storage utilization.

(B) First fit requires less time for allocation than worst fit on average.

(C) Best fit is clearly better than first fit in terms of time and storage utilization.

(D) First fit is clearly better than best fit in terms of time and storage utilization.

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

這一題的完整詳解

核心觀念

動態儲存配置(dynamic storage allocation)是在空閒區塊中,替新請求尋找足夠大的連續空間。常見策略如下:

  • First fit(首次適配):從空閒串列開頭依序搜尋,找到第一個足夠大的區塊就配置。
  • Best fit(最佳適配):搜尋所有空閒區塊,選擇最小且足以滿足需求的區塊。
  • Worst fit(最差適配):搜尋所有空閒區塊,選擇最大的區塊來配置。

搜尋範圍會影響配置所需時間;而空間利用率與外部碎裂情形,則會受到請求大小、配置與釋放的順序等因素影響。

解題方法

比較各策略的搜尋方式即可判斷時間成本:

  • First fit 找到第一個符合需求的區塊後便停止。
  • Worst fit 必須檢查所有空閒區塊,才能確定哪個是最大的。
  • Best fit 同樣通常需要檢查所有空閒區塊,才能找出最小的合適區塊。

因此,在一般空閒串列搜尋下,First fit 平均搜尋時間通常低於 Worst fit。至於哪種策略的空間利用率較佳,沒有不依賴工作負載的固定答案。

選項分析

🔒

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

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

免費註冊

第 5 題2 分

[2%] In DMA-based I/O,

(A) cycle stealing can slow down the CPU computation, but off-loading the data-transfer work to a DMA controller generally improves the total system performance.

(B) the CPU is relieved from data transfer and is interrupted after every word has been transferred to initiate the next word transfer.

(C) the interrupt mechanism is not used.

(D) data is transferred to/from a single block of memory, but not to/from multiple blocks.

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

這一題的完整詳解

核心觀念

DMA(直接記憶體存取)讓 DMA 控制器在 I/O 裝置與主記憶體之間直接搬移資料。CPU 通常先設定來源、目的地、傳輸長度等資訊,再由 DMA 控制器負責傳輸,CPU 不必逐字搬運資料。

DMA 傳輸期間,控制器可能需要占用記憶體匯流排。若採用 cycle stealing(週期竊取),DMA 控制器會逐次取得匯流排使用權,CPU 因而短暫等待;但 CPU 免去逐字處理資料的工作,整體系統效能通常仍會提升。傳輸完成後,DMA 控制器也可透過中斷通知 CPU。

解題方法

逐項比對選項所描述的工作分配與中斷時機:DMA 的關鍵是由控制器直接搬移資料,CPU 不會在每個字傳輸後都負責啟動下一個字的傳輸。

選項分析

  • (A) 正確。 Cycle stealing 會占用部分記憶體週期,使 CPU 計算稍微變慢;
🔒

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

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

免費註冊

第 6 題2 分

[2%] When designing an embedded I/O data acquisition path, engineers often choose between an interrupt-driven approach and polling. Which statement is correct?

(A) Under high event rates, an interrupt-driven design is always more power efficient than polling because the CPU does not repeatedly read a status register, and interrupts do not introduce meaningful context-switch overhead.

(B) In periodic polling, the average detection latency is mainly determined by the polling period. If event arrivals are uniformly distributed within each polling interval, the expected detection latency is approximately half the polling period, while the worst-case detection latency can be bounded by roughly one polling period (ignoring the polling routine’s execution time).

(C) Using edge-triggered interrupts eliminates interrupt storms, so edge-triggered interrupts are always preferable to level-triggered interrupts for high-frequency events, and no additional debouncing or throttling mechanisms are necessary.

(D) With polling, increasing the polling frequency simultaneously reduces latency and CPU utilization because the system can detect events sooner and return to sleep earlier; therefore, high-frequency polling is typically better for power.

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

這一題的完整詳解

核心觀念

中斷與輪詢都是處理 I/O 事件的方式:

  • 中斷驅動:裝置發生事件時通知 CPU。事件稀疏時,CPU 不必反覆檢查狀態,通常較有效率;但每次中斷都會帶來中斷處理與情境切換成本。
  • 輪詢:CPU 按固定週期檢查裝置狀態。輪詢週期為 TT 時,事件的偵測延遲取決於事件在兩次檢查之間發生的位置。

若事件在一個輪詢週期內均勻發生,偵測延遲介於 00 與 TT 之間,平均延遲為:

E[L]=1T∫0Tt dt=T2E[L] = \frac{1}{T}\int_0^T t\,dt = \frac{T}{2}

忽略輪詢程式本身的執行時間,最壞偵測延遲約為一個輪詢週期 TT。

解題方法

逐項檢查敘述是否正確描述中斷與輪詢在延遲、CPU 負載及事件處理成本上的取捨。題目中特別明確給出「事件在每個輪詢週期內均勻分布」,因此可直接用平均延遲 T/2T/2 判斷相關敘述。

選項分析

🔒

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

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

免費註冊

第 7 題2 分

[2%] You are implementing a very simple file system without journaling. Creating a new file involves allocating one data block, writing the data, initializing the inode, and adding a directory entry that points to that inode. Writes can be reordered by caches unless explicitly flushed. Which sequence best preserves crash consistency, meaning that after a crash you will not end up with a directory entry that references an uninitialized inode or an inode that references garbage data (you may still allow space leaks)?

(A) Persist the directory entry first, then persist the inode, then write the data block, because the name must exist before the file content is visible.

(B) Persist the inode first, then persist the directory entry, then persist the data block, because the inode can be updated later to fix any inconsistency.

(C) Persist the data block first, then persist the initialized inode that points to it, then persist the directory entry, because reachability should be created only after targets are durable.

(D) Persist the data block and inode in any order, because once the directory entry is durable it guarantees both metadata and data consistency.

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

這一題的完整詳解

核心觀念

本題考查檔案系統的當機一致性與持久化順序。快取可能重新排序寫入,因此程式送出寫入請求的先後,不代表資料實際寫入穩定儲存裝置的先後;必須透過明確的 flush,確保前一階段已持久化,才能開始讓後續資料可被存取。

新增檔案涉及三個物件:

  • 資料區塊:存放檔案內容。
  • inode:描述檔案,並指向資料區塊。
  • 目錄項目:記錄檔名,並指向 inode。

一致性的關鍵是:先讓被指向的目標安全,再建立指向它的連結。因此,資料區塊必須先持久化,接著持久化已初始化且指向該區塊的 inode,最後才持久化指向該 inode 的目錄項目。

解題方法

從最外層的目錄項目往下追蹤檔案的可達路徑:

目錄項目→inode→資料區塊\text{目錄項目} \rightarrow \text{inode} \rightarrow \text{資料區塊}

若目錄項目先持久化,當機後就可能出現指向未初始化 inode 的檔名。若 inode 先持久化、資料區塊尚未持久化,當機後 inode 就可能指向未寫好的資料。因此,持久化順序必須與可達路徑相反:

資料區塊→已初始化的 inode→目錄項目\text{資料區塊} \rightarrow \text{已初始化的 inode} \rightarrow \text{目錄項目}
🔒

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

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

免費註冊

第 8 題2 分

[2%] A system has two memory tiers. Tier 0 is small and low latency. Tier 1 is large and higher latency. The operating system manages pages and can migrate them between tiers. Which policy choice most directly reduces migration thrashing (ping pong) while still capturing hot pages?

(A) Migrate any page to Tier 0 immediately on its first access in Tier 1, because first touch is the earliest indicator of hotness.

(B) Use page faults as the primary hotness signal and migrate pages only when they fault repeatedly, because faults capture the most performance critical pages.

(C) Use hardware accessed bits (and optionally dirty bits) sampled over fixed epochs, apply hysteresis with a promotion threshold and a separate demotion threshold, and enforce a minimum residency time before a page is eligible to move again.

(D) Always keep the most recently allocated pages in Tier 0, because temporal locality implies newer pages are hotter than older pages.

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

這一題的完整詳解

核心觀念

本題考查分層記憶體的頁面遷移策略。Tier 0 容量較小、延遲較低;Tier 1 容量較大、延遲較高。作業系統要把常用頁面升到 Tier 0,同時避免頁面因短期使用量變動而頻繁升降,造成遷移來回震盪(ping pong)。

頁面熱度可用固定觀察期間內的硬體存取位元(accessed bit)估計:週期性檢查並清除存取位元,再觀察下一個期間是否被設為 1。若頁面有寫入需求,也可參考 dirty bit。位元提供的是觀察期間內的使用訊號,並非完美的長期熱度指標,因此遷移策略需降低短期波動的影響。

解題方法

判斷政策時,依序檢查三件事:

  1. 熱度訊號是否反映頁面的實際存取? 硬體存取位元能記錄頁面是否被使用;配置時間或缺頁事件都不是完整的存取頻率指標。
  2. 遷移門檻是否避免小幅波動就觸發升降? 採用不同的升級與降級門檻形成遲滯(hysteresis):熱度達較高門檻才升到 Tier 0,降到較低門檻才退回 Tier 1。
  3. 是否限制頁面短時間內再次遷移? 設定最低駐留時間(minimum residency time),讓頁面遷移後先留在該層一段時間,再重新評估是否符合遷移資格。

三項機制合用,可依觀察到的熱度提升真正熱門的頁面,並降低短暫存取變化造成的反覆搬移。

選項分析

🔒

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

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

免費註冊

第 9 題2 分

[2%] Assume a demand-paged system with 3 physical frames and an LRU replacement policy. The page reference string is:

1,2,3,4,1,2,5,1,2,3,4,51, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

How many page faults occur?

(A) 8
(B) 9
(C) 10
(D) 11

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

這一題的完整詳解

核心觀念

頁面不在目前的實體頁框中時,會發生一次缺頁(page fault)。LRU(最近最少使用)置換法則會在頁框已滿時,淘汰「最久沒有被使用」的頁面。

本題有 3 個實體頁框。下表中的頁面順序由左至右表示「由最久未使用到最近使用」;命中時也要更新頁面的使用順序。

解題方法

依序處理每次參照:頁面不在頁框中就記一次缺頁;頁框未滿時直接載入,已滿時淘汰最久未使用的頁面。

次數參照頁面結果淘汰頁面處理後頁面順序(LRU → 最近使用)
11缺頁—1
22缺頁—1, 2
33缺頁—1, 2, 3
44缺頁12, 3, 4
51缺頁23, 4, 1
62缺頁34, 1, 2
75缺頁41, 2, 5
🔒

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

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

免費註冊

第 10 題2 分

[2%] Consider enabling huge pages for a workload that scans large arrays with strong spatial locality. Which statement is correct?

(A) Huge pages can reduce TLB misses by increasing TLB reach, but they can increase internal fragmentation and can make some operations, such as copy on write and page migration, more expensive because the unit of management is larger.

(B) Huge pages primarily improve performance by eliminating page faults, since huge pages prevent the kernel from needing demand paging for that region.

(C) Huge pages reduce page table walk overhead only if the CPU has a larger TLB, otherwise the miss rate cannot change.

(D) Huge pages always reduce memory usage because fewer page table entries are needed, so fragmentation cannot increase.

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

這一題的完整詳解

核心觀念

本題考查**巨頁(huge pages)**對位址轉譯效能與記憶體管理的影響。虛擬位址須經由轉譯後才能存取實體記憶體;TLB(轉譯後備緩衝區)會快取近期使用的位址轉譯。

TLB 的涵蓋範圍(TLB reach)可表示為:

TLB reach=TLB 項目數×每頁大小\text{TLB reach} = \text{TLB 項目數} \times \text{每頁大小}

在 TLB 項目數相同時,頁面越大,TLB 能涵蓋的記憶體範圍越大。對具有強烈空間區域性的連續陣列掃描而言,巨頁能讓更多相鄰資料落在同一頁內,因此有助於減少 TLB miss。

巨頁也有管理上的代價:若實際使用量遠小於配置的頁面大小,會產生較多內部碎裂;而寫入時複製(copy on write)、頁面遷移等以頁面為管理單位的操作,也可能因處理單位變大而更昂貴。

解題方法

先判斷巨頁改變了什麼:它增大每個頁面的大小,因此在 TLB 項目數不變時提高 TLB reach;但它不會消除需求分頁,也不能保證記憶體使用量下降。接著檢查選項是否同時描述了效能收益與管理代價。

選項分析

🔒

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

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

免費註冊

第 11 題2 分

[2%] On a traditional HDD (mechanical disk), which statement about common I/O schedulers and latency is correct?

(A) SCAN (elevator) can reduce average seek distance versus FCFS, but it may still cause long waits for some requests depending on direction and queueing, so fairness improves compared with SSTF but tail latency is not necessarily minimized.

(B) SSTF always provides the best tail latency because it minimizes seek distance for every request, which prevents starvation by construction.

(C) FCFS minimizes variance in response time because it preserves arrival order, which avoids reordering-induced latency spikes.

(D) The same scheduler conclusions apply unchanged to NVMe SSDs because both HDDs and SSDs are dominated by seek time.

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

這一題的完整詳解

核心觀念

傳統 HDD 的 I/O 延遲主要包含磁頭移動到目標磁軌的尋道時間、等待目標磁區轉到磁頭下方的旋轉延遲,以及資料傳輸時間;佇列中的其他請求也會影響等待時間。磁碟排程器會調整服務請求的順序,以降低磁頭移動成本或改善等待時間分布。

  • FCFS:依請求抵達順序服務,順序公平,但磁頭可能來回移動。
  • SSTF:每次選擇距目前磁頭位置最近的請求,通常可減少尋道距離,但遠處請求可能長期等不到服務。
  • SCAN:磁頭沿一個方向掃描並服務沿途請求,到端點後反向,如同電梯運行;通常兼顧尋道效率與較有規律的服務機會。

解題方法

逐一檢查選項是否正確描述排程器的取捨:尋道距離較短,不代表每個請求都等待較短,也不代表尾端延遲或公平性必然最佳。再判斷 HDD 與 SSD 的主要延遲來源是否相同。

選項分析

(A) 正確。 SCAN 會按磁頭掃描方向處理請求,通常比 FCFS 少一些磁頭往返移動,因此有機會降低平均尋道距離。相較於 SSTF,它也較不容易讓遠處請求因為近處請求不斷插隊而持續飢餓。不過,請求仍會受到掃描方向、佇列長度與抵達時機影響;

🔒

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

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

免費註冊

第 12 題2 分

[2%] Which situation is most likely to increase write amplification in an SSD using an FTL with garbage collection?

(A) Large sequential writes to mostly empty media, because sequentiality maximizes valid page copying during garbage collection.

(B) Using overprovisioning, because extra spare area reduces available user space and forces the SSD to copy more data on every write.

(C) Placing hot and cold data together in the same physical blocks, because it reduces metadata overhead and lowers the frequency of garbage collection.

(D) Small random overwrites to a nearly full drive, especially without TRIM, because many blocks contain a mix of valid and invalid pages, increasing copying during garbage collection.

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

這一題的完整詳解

核心觀念

SSD 的寫入放大(Write Amplification,WAF)定義為:

WAF=SSD 實際寫入的資料量主機要求寫入的資料量\mathrm{WAF}= \frac{\text{SSD 實際寫入的資料量}} {\text{主機要求寫入的資料量}}

主機覆寫資料時,SSD 通常不能直接在原地改寫 NAND 頁面,而是把新資料寫到空白頁,並將舊頁標記為無效。當可用空間不足,FTL(Flash Translation Layer)必須進行垃圾回收(Garbage Collection,GC):把受害區塊中的有效頁面搬到其他位置,再抹除整個區塊。

GC 搬移的有效頁面屬於額外寫入,會提高 WAF。受害區塊裡的有效頁面愈多,回收每一頁可用空間所需搬移的資料就愈多。

解題方法

判斷各選項是否會讓 GC 更頻繁,或讓 GC 每次必須搬移更多有效頁面。接近滿載時,可供 FTL 挑選的空閒區塊較少;小型隨機覆寫又會讓有效頁面與無效頁面分散在許多區塊中,因此 GC 常須搬移仍有效的頁面。若沒有 TRIM,已刪除但未通知 SSD 的資料也會被視為有效,妨礙回收。

選項分析

  • (A) 錯。 大量循序寫入、且媒體大多為空時,SSD 有較多空白頁可以直接寫入,短期內不必頻繁 GC。循序寫入本身不會最大化 GC 搬移量。
🔒

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

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

免費註冊

第 13 題2 分

[2%] Three periodic tasks have deadlines equal to periods:

T1:C1=1 ms, P1=4 msT_1: C_1=1\text{ ms},\ P_1=4\text{ ms}

T2:C2=1.5 ms, P2=6 msT_2: C_2=1.5\text{ ms},\ P_2=6\text{ ms}

T3:C3=2 ms, P3=12 msT_3: C_3=2\text{ ms},\ P_3=12\text{ ms}

Assume the standard theoretical model: independent periodic tasks, preemptive uniprocessor, deadline equals period, negligible overhead. Which statement is correct when applying utilization-based schedulability tests?

(A) The total utilization is U=14+1.56+212=23U=\frac{1}{4}+\frac{1.5}{6}+\frac{2}{12}=\frac{2}{3}. Therefore, the task set is schedulable under EDF (since U≤1U\leq 1), and it is also guaranteed schedulable under RM bound for n=3n=3, U≤3(21/3−1)≈0.779U\leq 3(2^{1/3}-1)\approx 0.779.

(B) The task set is not schedulable under EDF because EDF requires U<1U<1.

(C) The task set is not guaranteed schedulable under RM because RM’s utilization bound is U≤ln⁡2≈0.693U\leq\ln 2\approx 0.693 for three tasks.

(D) The task set is guaranteed schedulable under RM only if the periods are harmonic. Since 6 is not a multiple of 4, no RM guarantee can be made from utilization.

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

這一題的完整詳解

核心觀念

本題考查固定優先權排程的 RM(Rate Monotonic)利用率界限,以及 EDF(Earliest Deadline First)的可排程條件。

任務利用率定義為執行時間除以週期:

Ui=CiPiU_i=\frac{C_i}{P_i}

總利用率為所有任務利用率的總和:

U=∑i=1nCiPiU=\sum_{i=1}^{n}\frac{C_i}{P_i}

在題目給定的標準模型下,所有任務期限等於週期。EDF 可排程的充要條件為 U≤1U\leq 1。對有 nn 個週期任務的 RM 排程,Liu–Layland 利用率界限為:

U≤n(21/n−1)U\leq n\left(2^{1/n}-1\right)

此界限是保證可排程的充分條件。

解題方法

先計算三個任務的總利用率:

U=14+1.56+212=0.25+0.25+16=23≈0.667U=\frac{1}{4}+\frac{1.5}{6}+\frac{2}{12} =0.25+0.25+\frac{1}{6} =\frac{2}{3} \approx 0.667

套用 EDF 條件:

23≤1\frac{2}{3}\leq 1

因此任務集合在 EDF 下可排程。

再將 n=3n=3 代入 RM 利用率界限:

URM=3(21/3−1)≈0.780U_{\text{RM}}=3\left(2^{1/3}-1\right)\approx 0.780

由於:

🔒

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

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

免費註冊

第 14 題2 分

[2%] Which statement about priority inversion and its mitigation is correct?

(A) Priority inheritance ensures that a high-priority task can never be blocked by a lower-priority task, since the lower-priority task is immediately boosted above all others and runs to completion.

(B) Priority inversion occurs only with non-preemptive kernels; in a fully preemptive RTOS it cannot happen because the scheduler always runs the highest-priority task.

(C) Replacing mutexes with spinlocks eliminates priority inversion because spinlocks never block; they only busy-wait.

(D) Priority ceiling protocols can prevent deadlock and bound blocking time by restricting which tasks may lock a resource based on a preassigned ceiling, but they may reduce parallelism by being conservative.

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

這一題的完整詳解

核心觀念

本題考查優先權反轉(priority inversion),以及互斥鎖的兩種緩解方法:優先權繼承(priority inheritance)與優先權天花板協定(priority ceiling protocol)。

優先權反轉常見於這種情況:低優先權工作持有某個互斥鎖,高優先權工作因等待該鎖而阻塞;中優先權工作又搶先執行,使低優先權工作無法及時完成,連帶延長高優先權工作的等待時間。

優先權繼承會暫時提高持鎖工作的優先權,通常提高到被它阻塞的最高優先權工作之優先權,讓持鎖工作能較快釋放資源。它能縮短優先權反轉造成的阻塞,但不能保證高優先權工作永遠不會被低優先權工作阻塞。

優先權天花板協定會事先為每個資源設定天花板,通常是所有可能使用該資源的工作中最高的優先權。依協定限制工作取得資源的時機,能防止死結,並限制阻塞時間;較保守的限制也可能降低平行度。

解題方法

判斷各選項是否把緩解措施說得過度絕對,或是否誤解了搶先式排程與自旋鎖的行為。優先權反轉並非只由非搶先式核心造成;只要有共享資源與優先權差異,搶先式核心仍可能發生。

選項分析

🔒

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

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

免費註冊

第 15 題2 分

[2%] In a system with a non-coherent CPU data cache, a device performs DMA writes into a memory buffer that the CPU later reads. Which action is correct for the CPU/driver to ensure it reads the newly DMA-written data?

(A) Before starting the DMA write, the CPU must invalidate the cache lines of the destination buffer; after DMA completion, no cache maintenance is needed because DMA writes update memory directly.

(B) The CPU should clean (write back) the cache lines after DMA completion, because writing back ensures the cache and memory match the device output.

(C) No cache maintenance is required as long as the buffer is aligned to cache-line boundaries and the DMA length is a multiple of the cache-line size.

(D) After DMA completion, the CPU must invalidate (or otherwise ensure invalidation of) the cache lines covering the buffer before reading it, because the CPU cache may still hold stale copies.

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

這一題的完整詳解

核心觀念

非一致性資料快取(non-coherent data cache)不會自動更新 CPU 快取中的資料。DMA 裝置直接把資料寫入主記憶體後,CPU 快取仍可能保留 DMA 前載入的舊資料,因此 CPU 讀取前必須讓涵蓋該緩衝區的快取資料失效,或使用等效的快取同步操作。

  • 失效(invalidate):丟棄快取中的副本,讓之後的讀取從主記憶體取得資料。
  • 清除/寫回(clean / write back):把快取中的資料寫入主記憶體。若快取保留的是舊資料,寫回反而可能覆蓋裝置剛寫入的新資料。

解題方法

題目描述的是「裝置寫入、CPU 之後讀取」的 DMA 流程。判斷重點是:DMA 完成後,CPU 快取中若仍有該緩衝區的舊副本,CPU 可能直接讀到舊值。因此,CPU 在讀取前須失效該緩衝區對應的快取列,確保新資料從主記憶體載入。

實務上,驅動程式也須遵守 DMA 緩衝區的所有權與同步規則:DMA 進行期間避免 CPU 存取該緩衝區;開始 DMA 前也可能需要依平台規範處理快取,以避免髒資料稍後寫回並覆蓋 DMA 結果。

🔒

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

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

免費註冊

第 二-(1) 題8 分

[8%] An orphan process is a process whose parent process no longer exists, i.e., either it has finished or terminated without waiting for its child processes to terminate. Please finish the following code within the boxes, which can create an orphan process.

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int pid = fork();
    if (pid > 0)
    {
        // this is parent process
        ____
    }
    else if (pid == 0)
    {
        // this is child process
        ____
    }
    return 0;
}

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

這一題的完整詳解

核心觀念

孤兒行程(orphan process)是指父行程已結束,但子行程仍在執行的行程。要建立孤兒行程,父行程必須先結束,子行程則要持續存活一段時間;子行程成為孤兒後,會由系統指定的行程收養,通常是 init 或 systemd。

fork() 成功時,父行程收到大於 00 的子行程 PID,子行程收到 00。因此可在父行程分支直接結束,並讓子行程暫停一段時間,確保父行程結束時子行程仍存在。

解題方法

父行程呼叫 exit(0) 結束,不呼叫 wait() 等待子行程;子行程呼叫 sleep(1),在父行程結束後仍繼續存活。

🔒

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

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

免費註冊
📄 以下 6 題共用同一段題幹

An LSM-tree is a disk-oriented data structure that improves write efficiency by buffering updates in memory and flushing them to disk in batches. By doing so, it turns many small random writes into fewer sequential writes, which is especially beneficial for write-heavy workloads.

LevelDB is a popular open-source LSM-tree based key-value store. New updates are first appended to a write-ahead log (WAL) for crash recovery, then inserted into an in-memory MemTable (typically a skip list). When the MemTable becomes full, it is frozen as an Immutable MemTable, and a new MemTable is created for incoming writes. The Immutable MemTable is then flushed to disk as an SSTable, where keys are stored in sorted order.

On disk, LevelDB organizes SSTables into multiple levels (L0L_0, L1L_1, …). Data is first flushed into L0L_0, where SSTables may overlap in key ranges. When L0L_0 grows beyond a threshold, LevelDB triggers compaction: it selects one or more SSTables from level LiL_i and merges them with overlapping SSTables in level Li+1L_{i+1} using a merge-sort. The merged output is written as new SSTables in Li+1L_{i+1}. From L1L_1 onward, SSTables are maintained to be non-overlapping within each level.

Deletes are handled using tombstones. A delete inserts a marker rather than immediately removing data. During compaction, entries covered by tombstones are dropped, which permanently removes the data. Reads search for the newest version first, checking the MemTable, then the Immutable MemTable, then SSTables from L0L_0 down to deeper levels until the key is found or confirmed absent.

🖼️【此處有附圖,請對照原卷】

第 二-(2) 題12 分

Read the description above about the LSM tree and answer the following questions.

(a) [4%] Explain where write amplification comes from in an LSM-tree key-value store such as LevelDB. Your answer should connect amplification to the maintenance workflow described above.

(b) [4%] Explain where read amplification comes from in an LSM-tree key-value store. In particular, why can L0L_0 increase the number of files a lookup must examine?

(c) [4%] Under what conditions does compaction become the bottleneck and lead to higher tail latency or write stalls? Describe the situation using the LSM maintenance workflow (MemTable, Immutable MemTable, flush to L0L_0, and compaction across levels).

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

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

這一題的完整詳解

核心觀念

本題考查 LSM-tree 以「記憶體緩衝+批次寫入」提升寫入效率時,產生的三項代價:

  • 寫入放大(write amplification):使用者提交一份資料,儲存裝置卻須寫入多次。若將 WAL 納入計算,可定義為 Awrite=WAL、flush 與 compaction 的實際寫入位元組總量使用者提交的資料位元組總量。A_{\mathrm{write}} = \frac{\text{WAL、flush 與 compaction 的實際寫入位元組總量}} {\text{使用者提交的資料位元組總量}}。
  • 讀取放大(read amplification):一次查詢須檢查多個資料來源,才能找到最新版本或確認不存在。
  • 維護瓶頸:背景 flush 與 compaction 跟不上前景寫入,造成資料積壓、尾端延遲升高,甚至暫停寫入。

解題方法

附圖以水平線區分記憶體與磁碟:新資料先寫入獨立的 WAL,再進入 MemTable;MemTable 凍結為 Immutable MemTable 後,透過 flush 產生 L0L_0 的 SSTable,接著經 compaction 逐層向下合併。解題時沿著這條流程,分別追蹤「資料被寫幾次」、「查詢須找哪些檔案」及「維護跟不上時堵在哪裡」。

(a) 寫入放大的來源

一筆更新會經過以下寫入:

  1. 追加至 WAL:先寫入磁碟日誌,供當機復原使用。WAL 是獨立日誌,不是 SSTable;將資料放入 MemTable 本身則是記憶體操作。
  2. flush 至 L0L_0:MemTable 凍結後,其內容再寫成磁碟上的 SSTable。
  3. 跨層 compaction:合併 LiL_i 的 SSTable 與 Li+1L_{i+1} 中鍵值範圍重疊的 SSTable,再將結果寫成新的 Li+1L_{i+1} 檔案。因此,同一筆仍有效的資料會隨著後續合併反覆被重寫,連下層既有資料也會一併重寫。

所以,批次順序寫入能減少隨機存取,卻不代表每筆資料只寫一次;額外的 WAL 寫入、初次 flush,以及反覆產生 compaction 輸出,共同造成寫入放大。LevelDB 官方實作說明

Compaction 可淘汰不再需要的舊版本,但不能無條件清除 tombstone。只有確認更深層沒有仍須遮蔽的舊值,且不影響有效快照,才能移除;過早移除會使已刪除資料重新被查到。LevelDB 官方實作程式碼

(b) 讀取放大的來源與 L0L_0 的影響

同一個鍵的版本分散在不同位置。點查詢須依序檢查:

🔒

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

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

免費註冊

其他考古題