免費詳解 / 中正 / 硬體 / 105

105 中正資工所考古題解答|硬體逐題詳解

105 中正資工所硬體考古題的逐題詳解,共 12 題,免費公開。

逐題詳解

120%

選擇題 (2 pt each):

(1) If a program terminates abnormally, a dump of memory may be examined by a ______ to determine the cause of the problem. A) module B) debugger C) shell D) control card

(2) A boot block ______. A) typically only knows the location and length of the rest of the bootstrap program B) typically is sophisticated enough to load the operating system and begin its execution C) is composed of multiple disk blocks D) is composed of multiple disk cylinders

(3) Which of the following statements is true? A) Shared memory is typically faster than message passing. B) Message passing is typically faster than shared memory. C) Message passing is most useful for exchanging large amounts of data. D) Shared memory is far more common in operating systems than message passing

(4) According to Amdahl's Law, what is the speedup gain for an application that is 60% parallel and we run it on a machine with 4 processing cores? A) 1.82 B) .7 C) .55 D) 1.43

(5) Which of the following is true of multilevel queue scheduling? A) Processes can move between queues. B) Each queue has its own scheduling algorithm. C) A queue cannot have absolute priority over lower-priority queues. D) It is the most general CPU-scheduling algorithm.

(6) ______ occurs when a higher-priority process needs to access a data structure that is currently being accessed by a lower-priority process. A) A race condition B) Deadlock C) Priority inversion D) A critical section

(7) How many philosophers may eat simultaneously in the Dining Philosophers problem with 5 philosophers? A) 1 B) 2 C) 3 D) 5

(8) Suppose that there are ten resources available to three processes. At time 0, the following data is collected. The table indicates the process, the maximum number of resources needed by the process, and the number of resources currently owned by each process. Which of the following correctly characterizes this state?

ProcessMaximum NeedsCurrently Owned
P₀104
P₁31
P₂64

A) It is safe. B) It is not safe. C) The state cannot be determined. D) It is an impossible state.

(9) Assume a system has a TLB hit ratio of 90%. It requires 15 nanoseconds to access the TLB, and 85 nanoseconds to access main memory. What is the effective memory access time in nanoseconds for this system? A) 22 B) 100 C) 108.5 D) 176.5

(10) Suppose that the operating system uses two internal tables to keep track of open files. Process A has two files open and process B has three files open. Two files are shared between the two processes. How many entries are in the per-process table of process A, the per-process table of process B, and the system-wide tables, respectively? A) 5, 5, 5 B) 2, 3, 3 C) 2, 3, 5 D) 2, 3, 1

(1) 異常結束時的 memory dump 丟進 debugger 才看得到當下的暫存器與變數 → B

(2) boot block 只放得下一小段程式,只知道剩下那段 bootstrap program 在哪、多長,把它載進來後再由它載入 OS → A

(3) shared memory 建好之後直接讀寫同一塊記憶體,不用每次進 kernel;message passing 每次都要 system call,適合小量資料與跨機器 → A

(4) \(S=\dfrac1{(1-f)+f/k}=\dfrac1{0.4+0.6/4}=\dfrac1{0.55}\approx1.82\) → A

(5) multilevel queue 把 process 依性質固定分進不同 queue,每個 queue 可有自己的排班演算法;process 不能在 queue 間移動(那是 feedback queue) → B

(6) 高優先權 process 卡在低優先權 process 持有的資料結構上 → priority inversion → C

(7) 每人要同時拿到左右兩支筷子,5 支最多支援 \(\lfloor5/2\rfloor=2\) 人同時吃 → B

(8) 已配出 \(4+1+4=9\),available \(=10-9=1\);各自還需要:P₀ 6、P₁ 2、P₂ 2,都大於 1,沒有任何一個能先跑完,找不到 safe sequence → unsafe → B

(9) hit:\(15+85=100\);miss:\(15+85+85=185\)(查 TLB、讀 page table、讀記憶體)

\(\text{EMAT}=0.9\times100+0.1\times185=108.5\) ns → C

(10) per-process table:A 有 2 筆、B 有 3 筆;system-wide table 每個被開啟的檔案一筆,兩邊共 5 個開啟、其中 2 個是同一檔案,所以 \(2+3-2=3\) 筆 → B

(1) B (2) A (3) A (4) A (5) B (6) C (7) B (8) B (9) C (10) B

25%

(5 pt) What are the two major problems associated with linked allocation of disk space routines?

① 只適合 sequential access:block 靠指標串起來,要讀第 \(i\) 個 block 得從頭沿著指標走 \(i\) 次,沒辦法直接算出位置,random access 很慢

② 指標佔掉每個 block 的空間(例如 512 byte 的 block 裡 4 byte 當指標,約 0.78%),而且指標若損壞或遺失,後面的 block 全部找不到(可靠度差);常見改法是用 cluster 或 FAT 把指標集中到一張表

38%

(8 pt) The read/write speeds of SSDs (solid state drives) are much higher than those of HDDs (hard disk drives). However, an SSD can only be written to a limited amount of data before it reaches its end-of-life. A professor builds a RAID 4 system and uses 7 HDDs as data drives and an SSD as the parity drive. Please discuss the pros and cons of his design.

RAID 4 = block-level striping + 一顆專用的 parity drive;任何一次寫入都要同時更新 parity,所以 parity drive 是整個 array 的寫入瓶頸

優點

① 瓶頸那一顆換成 SSD,正好解掉 RAID 4 最痛的地方:小量寫入的 read-modify-write 要讀舊 parity、寫新 parity,在 HDD 上是 seek + 旋轉延遲,SSD 幾乎沒有這個成本,parity drive 不再拖慢整體寫入

② parity drive 平常幾乎只被寫、很少被讀,SSD 的高速度與低延遲剛好用在最忙的位置;只多買一顆 SSD,成本比整個 array 都用 SSD 低

缺點

① 寫入次數全部集中在同一顆:7 顆 data drive 的每一次寫入都轉成 parity drive 的一次寫入,SSD 承受的寫入量約是單顆 HDD 的 7 倍,P/E cycle 會遠比其他碟早耗盡,最先壽終正寢

② parity drive 壞掉後 array 就失去容錯能力,而且它是可預期地最先壞,又要更換成本較高的 SSD,等於把最容易故障的元件放在最關鍵的位置

③ 小量寫入還要先從 data HDD 讀舊資料(read-modify-write),這一步仍受 HDD 速度限制,整體寫入效能提升有限

較合理的做法:RAID 5 把 parity 分散到所有碟,寫入量才平均;或 data 與 parity 全用 SSD 並做 wear leveling

49%

(9 pt) A professor has a solution of the reader-writer problem. The solution is based on spin-locks. Assume that the system has 100,000 locks. A reader must acquire a lock to enter the critical section, and a writer must acquire 100,000 locks to enter the critical section. Please implement his solution in C language.

reader 只搶其中一把 lock(各自搶不同把,reader 之間互不干擾);writer 要搶下全部 100,000 把,所以一定和任何一個 reader、任何另一個 writer 互斥。writer 固定照同一個順序取鎖,避免兩個 writer 互相卡住

#include <stdatomic.h>
#define N 100000

static atomic_flag lock[N];            /* 初始 clear = free */

static void acquire(atomic_flag *l) {
    while (atomic_flag_test_and_set_explicit(l, memory_order_acquire))
        ;                              /* spin */
}
static void release(atomic_flag *l) {
    atomic_flag_clear_explicit(l, memory_order_release);
}

void reader(int id) {
    int i = id % N;                    /* 任挑一把即可,例如依 thread id */
    acquire(&lock[i]);
    /* ... read the shared data ... */
    release(&lock[i]);
}

void writer(void) {
    int i;
    for (i = 0; i < N; i++)            /* 固定由小到大取,避免 deadlock */
        acquire(&lock[i]);
    /* ... write the shared data ... */
    for (i = N - 1; i >= 0; i--)
        release(&lock[i]);
}

reader 同一時間只持有一把 lock,不會與 writer 形成循環等待;writer 取鎖順序一致,兩個 writer 也不會 deadlock。代價:writer 的取鎖與放鎖要掃 100,000 次;多個 reader 若挑到同一把 lock 就變成互斥,所以 reader 要盡量平均分散到不同的 lock

58%

(8 pt) Consider a CPU that supports the "TestAndSet" instruction. An engineer uses the instruction to solve the critical section problem, and his solution is shown below. A correct solution must satisfy three conditions, i.e., mutual exclusion, progress, and bounded waiting. Please prove or disprove the correctness of his solution.

while (TRUE) {
    while ( TestAndSet (&lock))
        ;   // do nothing

    //    critical section

    lock = FALSE;

    //      remainder section
}

結論:滿足 mutual exclusion 與 progress,違反 bounded waiting,所以不是正確解

mutual exclusion TestAndSet 是 atomic:讀舊值並把 lock 設成 TRUE。lock 為 FALSE 時,只有一個 process 會讀到 FALSE 而跳出 while,其他的都讀到 TRUE 繼續等;離開前才把 lock 設回 FALSE → 同一時間最多一個在 critical section

progress 離開 critical section 的 process 把 lock 設回 FALSE;在 remainder section 或不想進入的 process 不參與決定,所以只要有人想進,lock 為 FALSE 時就一定有人進得去,決定不會被無限延後

bounded waiting 所有等待的 process 擠在同一個 TestAndSet 上搶,誰搶到純看硬體仲裁,沒有任何機制保證某個等待的 process 在有限次數內輪到;某個 process 可能一再被其他 process 超車而餓死(starvation) → 不成立

要補上 bounded waiting,得像課本那樣加 waiting[] 陣列:離開 critical section 時,由 \(i+1\) 起依序找下一個正在等待的 process,直接把權利交給它,找不到才把 lock 設為 FALSE

65%

(5 pt) If we want to design an adder to compute the addition of two 16-bit unsigned numbers with ONLY 4-bit carry-look-ahead (CLA) adders and 2-to-1 multiplexers. The delay time of a 4-bit CLA adder and a 2-to-1 multiplexer are \(D_{CLA}\) and \(D_{MX}\), respectively. In addition, \(D_{MX}\) is equal to \(0.2\times D_{CLA}\). Please determine the minimum delay time for this 16-bit adder in terms of \(D_{CLA}\).

切成 4 個 4-bit 區塊 \(B_0\sim B_3\)(\(B_0\) 的 carry-in 是 0)。\(B_1,B_2,B_3\) 各放兩份 CLA,分別假設 carry-in 為 0 與 1,全部在時間 0 同時開始算,\(t=D_{CLA}=1\) 時所有候選的 sum 與 carry-out 都備妥;\(B_0\) 的 carry-out \(c_4\) 也在 \(t=1\)

接下來只用 mux 選擇;關鍵是把選擇排成樹狀而不是一路串下去:

時間(\(D_{CLA}\))動作
1.0所有 CLA 算完;\(c_4\) 就緒
1.0 → 1.2用 \(c_4\) 選出 \(B_1\) 的 sum 與真正的 \(c_8\);同時,對「\(c_8=0\)」與「\(c_8=1\)」兩種假設,各用 \(B_2\) 的候選 \(c_{12}\) 選出 \(B_3\) 的 sum 與 \(c_{16}\)(把 \(B_2,B_3\) 壓成兩組以 \(c_8\) 為條件的結果)
1.2 → 1.4用 \(c_8\) 選出 \(B_2\)、\(B_3\) 的真值與 \(c_{16}\)

總延遲:\(D_{CLA}+2D_{MX}=1+2(0.2)=1.4\,D_{CLA}\)

一路串接的 carry-select(\(c_4\to c_8\to c_{12}\to c_{16}\))是 \(D_{CLA}+3D_{MX}=1.6\,D_{CLA}\);\(B_3\) 的輸出取決於 \(c_4\) 經過兩個區塊的傳遞,至少要兩層 mux,所以 1.4 是下限

\(1.4\,D_{CLA}\)

75%

(5 pt) Based on IEEE 754 standard, the single precision numbers are stored in 32 bits with one sign bit, eight exponent bits, and 23 mantissa bits. Please show the representation of −0.6875.

\(0.6875=\dfrac{11}{16}=0.1011_2=1.011_2\times2^{-1}\)

sign:負數,\(s=1\)

exponent:\(-1+127=126=0111\,1110_2\)

mantissa:小數點後的 \(011\),其餘補 0

1 | 01111110 | 01100000000000000000000 (= 0xBF300000)

85%

(5 pt) Caches take advantage of temporal locality and spatial locality to improve the performance of the memory. Please briefly explain what is temporal locality and spatial locality.

temporal locality:剛被存取過的位址,過不久很可能又被存取(例:loop 裡的迴圈變數、counter、stack top)

spatial locality:某個位址被存取後,它附近的位址很可能接著被存取(例:陣列循序掃描、程式碼循序執行)

cache 用前者決定「留下什麼」,用後者決定「一次搬多大一塊」(block 以數十 byte 為單位搬進來)

910%

(10 pt) The following techniques have been developed for cache optimizations: "Pipelined cache" and "multi-banked cache." Please briefly explain these techniques and how they work.

pipelined cache 把一次 cache 存取本身切成多個 pipeline stage(位址解碼、tag 比對、資料讀出),每個 stage 一個 cycle;每個 cycle 都能送進一次新的存取,所以 clock rate 與 bandwidth 都提高

代價:單次 hit 的 latency 變成好幾個 cycle,load-use 的距離變長、branch misprediction penalty 也變大

multi-banked cache 把 cache 切成幾個可獨立運作的 bank,block 位址交錯分配(第 \(i\) 個 block 放 bank \(i\bmod b\));落在不同 bank 的存取可以同時進行,一個 cycle 服務多個請求,提高 bandwidth

只有存取平均分散到各 bank 才有效;全部集中在同一個 bank 就退回單埠的效能,所以交錯方式很關鍵

108%

(8 pt) The following circuit is composed of two D-type flip-flops (DFFs) and an OR gate, where the DFFs are reset when clk=1. Describe the function of the circuit and one possible application of it.

(原題附圖:輸入 clk̄、din、clk;兩個 DFF 的 D 都接 clk̄、rst 都接 clk;上面那個 DFF 的時脈腳接 din(正緣觸發),下面那個接 din 經反相(負緣觸發);兩個 Q 接 OR 閘輸出 dout)

clk̄dinclkDrstDrstdoutQ₁Q₂

兩個 DFF 的 D 都接 clk̄,rst 都接 clk(高電位 reset):

\(clk=1\):兩個 DFF 都被清成 0,\(dout=0\)

\(clk=0\)(reset 解除,\(\overline{clk}=1\),兩個 D 都是 1):上面的 DFF 在 din 的正緣把 1 存進去,下面的 DFF 在 din 的負緣把 1 存進去;只要 din 在這段期間出現任何一次 0→1 或 1→0,對應的 DFF 就變 1,OR 之後 \(dout=1\),並保持到下一次 \(clk=1\) 才被清掉

din 在 clk 為 low 的期間完全不動 → \(dout=0\)

功能:偵測 din 在 clk 為 low 的期間有沒有發生變化(transition detector)

應用:偵測訊號線上的 glitch/雜訊,或偵測 data 在不該變動的時間窗內變動(setup/hold 違規、timing error 偵測,例如 Razor 式的延遲偵測);也可用來判斷輸入有沒有活動,做 clock gating 或喚醒

dout = 1 ⟺ clk 為 low 期間 din 有 0→1 或 1→0 的變化(transition detector)

118%

(8 pt) The following is a code segment in MIPS assembly, where JAL (jump and link) jumps to the label j_target and saves its return address in R31 and JR returns to the next instruction of JAL.

        JAL j_target
        ...
j_target   JR R31

List all possible hazards when the code is executed on a classical 5-stage pipelined datapath (i.e. fetch, decode, execute, memory, and write_back). What are the minimum cycles for an interlock unit to resolve each hazard without data forwarding?

假設:jump 的目標位址在 ID 階段結束時才確定;register file 前半 cycle 寫、後半 cycle 讀;沒有 forwarding、沒有 branch prediction

#hazard成因最少 stall
1control hazard(JAL)JAL 的目標在 ID 結束才知道,此時 IF 已經抓了下一條(錯的)指令1
2data hazard(RAW,\(R31\))JAL 在 WB 才寫 R31,JR 在 ID 就要讀 R311
3control hazard(JR)JR 的目標要讀完 R31 才算得出來,ID 結束才知道,後面又多抓了一條錯的指令1

時序(*:JR 在 ID 等 R31;× 被沖掉):

12345678
JAL j_targetIFIDEXMEMWB
(被沖掉)IF×
JR R31IFID*IDEXMEMWB
(被沖掉)IFIF×
JR 的目標IFID

JAL 在 cycle 5 前半寫入 R31,JR 的 ID 排在 cycle 5(後半讀)才拿到正確值:hazard 2 要多等 1 個 cycle(JR 本來可在 cycle 4 做 ID,因為 hazard 1 已吃掉 1 個 cycle,只剩 1 個要補);JR 的目標在 cycle 5 結束時才確定,cycle 5 抓的那條指令作廢,目標指令 cycle 6 才開始 fetch(hazard 3)

若 register file 不能同一 cycle 先寫後讀,hazard 2 要改成 2 個 stall,總共 4 個 bubble

3 種 hazard,各需 1 個 stall cycle,共 3 個 bubble

129%

(9 pt) Briefly describe the basic ideas of the following terms in computer designs: (a) TLB, (b) BTB, and (c) AMAT.

(a) TLB(translation-lookaside buffer):存放最近用過的 page table entry(virtual page number → physical frame number)的小型高關聯度 cache;命中時直接拿到 frame number,省掉去記憶體查 page table(多層 page table 是好幾次)的成本

(b) BTB(branch target buffer):以 branch 指令的 PC 當索引,存放該 branch 上次跳到哪裡(常帶預測位元)的一張表;IF 階段就能查到預測的目標位址,命中且預測正確時 taken branch 不必插 bubble

(c) AMAT(average memory access time):把 hit 與 miss 依發生頻率加權後的平均存取時間:

\(\text{AMAT}=\text{hit time}+\text{miss rate}\times\text{miss penalty}\)

多層 cache 時,miss penalty 就是下一層的 AMAT,一層一層往下展開

109–115 的詳解也是這樣寫的。

國立中正大學 109–115 的詳解收錄在付費版;106–115 全年度完整詳解共 351 頁,逐題推導。

購買 · NT$ 850 看中正其他免費詳解

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

題目有錯、詳解看不懂,都歡迎來信

想許願補哪一份、對購買有疑問也可以,收到後會盡快回覆。

william.chou.123123@gmail.com