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

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

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

逐題詳解

120%

(20pt)

(1) In multithreaded programs, the kernel informs an application about certain events using a procedure known as a(n) ______. A) signal B) upcall C) event handler D) pool

(2) In Pthreads, a parent uses the pthread_join() function to wait for its child thread to complete. What is the equivalent function in Win32? A) win32_join() B) wait() C) WaitForSingleObject() D) join()

(3) A process that has terminated, but whose parent has not yet called wait(), is known as a ______ process. A) zombie B) orphan C) terminated D) init

(4) Which of the following is true of cooperative scheduling? A) It requires a timer. B) A process keeps the CPU until it releases the CPU either by terminating or by switching to the waiting state. C) It incurs a cost associated with access to shared data. D) A process switches from the running state to the ready state when an interrupt occurs.

(5) Which of the following scheduling algorithms must be non-preemptive? A) SJF B) RR C) FCFS D) priority algorithms

(6) A race condition ______. A) results when several threads try to access the same data concurrently B) results when several threads try to access and modify the same data concurrently C) will result only if the outcome of execution does not depend on the order in which instructions are executed D) None of the above

(7) Which of the following statements is true? A) A counting semaphore can never be used as a binary semaphore. B) A binary semaphore can never be used as a counting semaphore. C) Spinlocks can be used to prevent busy waiting in the implementation of semaphore. D) Counting semaphores can be used to control access to a resource with a finite number of instances.

(8) Which of the following statements is true? A) A safe state is a deadlocked state. B) A safe state may lead to a deadlocked state. C) An unsafe state is necessarily, and by definition, always a deadlocked state. D) An unsafe state may lead to a deadlocked state.

(9) In a dynamically linked library, ______. A) loading is postponed until execution time B) system language libraries are treated like any other object module C) more disk space is used than in a statically linked library D) a stub is included in the image for each library-routine reference

(10) Suppose we have the following page accesses: 1 2 3 4 2 3 4 1 2 1 1 3 1 4 and that there are three frames within our system. Using the LRU replacement algorithm, what is the number of page faults for the given reference string? A) 14 B) 13 C) 8 D) 10

(1) kernel 透過 upcall 通知 user-level thread library 事件(scheduler activation 的機制);signal 是 UNIX 通知 process 的方式 → B

(2) Win32 用 WaitForSingleObject() 等 thread handle 結束;wait() 是 UNIX 等 child process 的 system call → C

(3) 已結束但 parent 還沒 wait():PCB 與 exit status 還留著,叫 zombie;orphan 是 parent 先結束、自己還在跑的 process → A

(4) cooperative = nonpreemptive:拿到 CPU 後只有自己結束或進入 waiting 才交出;A、D 是 preemptive 才有的(timer、interrupt 搶走 CPU);C 的共享資料成本也是 preemption 造成的 → B

(5) FCFS 只能等目前的 process 做完或 block,沒有 preemptive 版本;SJF 有 SRTF,priority 也有 preemptive 版,RR 靠 time quantum 搶佔 → C

(6) 多個 thread 同時存取並修改同一份資料、結果取決於執行順序 → B(只讀不會出問題,C 說反了)

(7) counting semaphore 初值 1 就能當 binary semaphore;binary semaphore 加計數變數也能做 counting semaphore;spinlock 本身就是 busy waiting;counting semaphore 初值設成資源數,wait() 拿一個、signal() 還一個 → D

(8) safe state 保證有 safe sequence,不會 deadlock;unsafe 只是不保證能避開,不等於 deadlock,但有可能走向 deadlock → D

(9) dynamic linking 在執行檔裡對每個 library routine 放一個 stub,執行時 stub 檢查該 routine 是否已在記憶體、沒有就載入,並把自己換成它的位址;延到執行時的是 linking,「loading 延後」是 dynamic loading;system library 當一般 object module 併入是 static linking;dynamic linking 的 library 只存一份、各程式共用,反而省 disk → D

(10) 3 個 frame,frames 由最久沒用到→最近用到:

ref12342341211314
fault✓✓✓✓✓✓✓✓
frames112123234342423234341412421421213231314

fault 共 8 次 → C

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

25%

(5pt) Describe one technique that can enable multiple disks to be used to improve data transfer rate.

data striping(RAID 0 的做法):把資料切成小單位,輪流放到多顆 disk 上

bit-level striping:每個 byte 的第 \(i\) 個 bit 放到第 \(i\) 顆 disk,8 顆 disk 同時讀寫,單次存取的 transfer rate 約為單顆的 8 倍

block-level striping:第 \(i\) 個 block 放到第 \((i\bmod n)+1\) 顆 disk;大筆資料 \(n\) 顆同時傳輸,transfer rate 提高約 \(n\) 倍,多個小請求也能分散到不同 disk 平行處理

310%

(10pt) Give an algorithm to solve the "bounded buffer" problem.

三個 semaphore,buffer 有 \(n\) 格:mutex = 1(保護 buffer)、empty = n(空格數)、full = 0(已放資料的格數)

semaphore mutex = 1, empty = n, full = 0;

/* producer */
while (true) {
    item = produce_item();
    wait(empty);               /* 等有空格 */
    wait(mutex);
    buffer[in] = item;  in = (in + 1) % n;
    signal(mutex);
    signal(full);              /* 多一格有資料 */
}

/* consumer */
while (true) {
    wait(full);                /* 等有資料 */
    wait(mutex);
    item = buffer[out]; out = (out + 1) % n;
    signal(mutex);
    signal(empty);             /* 多一個空格 */
    consume_item(item);
}

wait(empty)/wait(full) 一定要放在 wait(mutex) 之前:順序反過來,producer 在 buffer 滿時先拿 mutex 再卡在 empty,consumer 拿不到 mutex 就清不出空格,兩邊 deadlock

正確性:mutex 保證一次只有一個 process 改 buffer、in、out;empty 為 0 時 producer 被擋住,不會蓋掉未取走的資料;full 為 0 時 consumer 被擋住,不會讀到空格

415%

(15pt) Compare the read/write performance of RAID 4 with RAID 5 in detail.

兩者都是 block-level striping + parity,差別只在 parity 放哪裡:RAID 4 集中在一顆專用的 parity disk,RAID 5 把 parity block 輪流分散到所有 disk(N 顆 disk)

操作RAID 4RAID 5
大量循序讀parity disk 閒置,只有 N−1 顆提供資料N 顆都存有資料,全部可用
小量隨機讀分散在 N−1 顆 data disk 平行處理分散在 N 顆,平行度略高
大量循序寫(整條 stripe)直接算好 parity 一次寫入,效能好同左
小量隨機寫每次都要改 parity disk,成為 bottleneckparity 分散,不同 stripe 的寫入可平行

小量寫入兩者都要 read-modify-write:讀舊 data 與舊 parity、算新 parity、寫回新 data 與新 parity,共 2 次讀、2 次寫

\(P_{new}=P_{old}\oplus D_{old}\oplus D_{new}\)

差別在:RAID 4 的所有寫入都要動同一顆 parity disk,同一時間只能服務一筆小寫入,不管有幾顆 data disk 都被它卡住;RAID 5 不同 stripe 的 parity 落在不同 disk,多筆小寫入只要沒撞到同一顆就能同時進行

結論:讀取兩者接近(RAID 5 多一顆可用 disk);小量寫入 RAID 5 明顯較好,這也是 RAID 5 取代 RAID 4 的原因

55%

(5pt) Please explain the reason why the single-cycle implementation is rarely used to implement any instruction set of a processer.

single-cycle 每道指令都在一個 clock cycle 內完成,cycle time 要配合最慢的指令(通常是 lw:instruction memory → register file → ALU → data memory → register file);其他較快的指令(R-type、beq、sw)也被迫用同樣長的 cycle,時間大量浪費;指令集若有浮點或更複雜的指令,cycle 會被拉得更長

另外同一個功能單元一個 cycle 內只能用一次,所以要有獨立的 instruction memory 與 data memory、PC+4 與 branch target 各自的 adder,硬體成本高

multicycle 與 pipeline 都讓不同指令用不同的 cycle 數並共用硬體,效能與成本都較好,實際處理器很少採 single-cycle

65%

(5pt) If we want to design a carry-select adder to compute the addition of two 8-bit unsigned numbers with ONLY 1-bit full adders and 2-to-1 multiplexers. In addition, the delay time of a 1-bit full adder and a 2-to-1 multiplexer are \(D_{FA}\) and \(D_{MX}\), respectively. Moreover, \(D_{MX}\) is equal to \(0.8\,D_{FA}\). Please determine the minimum delay time for this carry-select adder.

最低的一組用 FA 串成 ripple-carry(carry-in = 0);之後每一組放兩套 ripple-carry adder,分別假設 carry-in 為 0 與 1,全部從時間 0 同時開始算,前一組的 carry 到了再用 mux 選出正確的 sum 與 carry-out

第 \(i\) 組有 \(m_i\) bit,carry-out 出現時間:\(T_1=m_1D_{FA}\),\(T_i=\max(T_{i-1},\ m_iD_{FA})+D_{MX}\)

要最小,每組的兩套 adder 應在前一組 carry 抵達前就算完。以 \(D_{FA}=1\)、\(D_{MX}=0.8\) 排最佳分組(LSB → MSB):

組bit 數兩套 adder 算完前一組 carry 抵達carry-out/sum
11——1.0
211.01.01.8
311.01.82.6
422.02.63.4
533.03.44.2

分組 1,1,1,2,3 共 8 bit,最高組的 sum 與 carry-out 都在 \(4.2\,D_{FA}\) 選出

其他分法都比較慢:常見的 4+4 是 \(4+0.8=4.8\,D_{FA}\);2+2+2+2 是 \(2+3(0.8)=4.4\,D_{FA}\);列舉所有分組,最小就是 4.2

\(4.2\,D_{FA}\)(分組 1,1,1,2,3)

715%

(15pt) The following techniques have been developed for cache optimizations: hit time, miss rate or miss penalty: "Non-blocking cache", "multi-banked cache", and "critical word first and early restart". Please briefly explain these techniques and how they work.

Non-blocking cache(lockup-free cache,降低有效 miss penalty) cache 處理一個 miss 時仍接受後面的存取:hit under miss 讓後面的 hit 照常服務;miss under miss 允許同時有多個未完成的 miss;搭配 out-of-order 執行,把 miss 的等待時間藏起來

Multi-banked cache(提高 bandwidth) 把 cache 切成多個獨立的 bank,依位址(例如 block address mod bank 數)分到不同 bank,不同 bank 的存取可同時進行,pipelined 或多發射的處理器不必排隊等 cache

Critical word first and early restart(降 miss penalty) 處理器通常只急著要 block 裡的某一個 word:critical word first 讓 memory 先送出被要求的那個 word,其餘再補上;early restart 照原順序送,但被要求的 word 一到就讓處理器繼續執行,不等整個 block 填完。block 越大效果越明顯

815%

(15pt) What are "3C cache misses"? List one technique to improve each of the 3C misses.

類型發生原因一種改善方法
Compulsory miss(cold-start)block 第一次被存取,cache 裡本來就沒有加大 block size,或做 prefetching
Capacity miss程式需要的 block 超過 cache 容量,被換掉後又要用加大 cache 容量
Conflict miss(collision)direct-mapped 或 set-associative 下,多個 block 搶同一個 set 而互相踢掉提高 associativity

都要取捨:block size 太大,block 數變少,conflict miss 與 miss penalty 反而上升;提高 associativity 可能拉長 hit time

910%

(10pt) Given the memory references (word addresses): 3, 180, 43, 2, 191, 88, 190, 14, 181, 44, 186, 253, and a direct-mapped cache with 10 blocks. Indicate which of the above 12 memory accesses will encounter a cache miss, if (1) each cache block has only 1 word, and (2) each cache block has 10 words.

cache 有 10 個 block:index \(=\) block address mod 10,tag \(=\lfloor\) block address / 10 \(\rfloor\)

(1) 每個 block 1 word:block address = word address

word address3180432191881901418144186253
block address3180432191881901418144186253
index303218041463
tag018401981911841825
結果missmissmissmissmissmissmissmissmissmissmissmiss

12 次全部 miss:沒有任何位址重複,block 又只有 1 word,沒有 spatial locality 可以利用

(2) 每個 block 10 words:block address \(=\lfloor\) word address / 10 \(\rfloor\)

word address3180432191881901418144186253
block address018401981911841825
index084098918485
tag010010101012
結果missmissmisshitmissmisshitmissmisshithitmiss

miss 的是 3、180、43、191、88、14、181、253(8 次);hit 的是 2、190、44、186(4 次)

88(block 8)進來時把 index 8 的 block 18 換掉,所以之後的 181 又 miss;181 再把 block 8 換回 block 18,186 才 hit

(1) 12 次全 miss (2) miss:3, 180, 43, 191, 88, 14, 181, 253

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

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

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

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com