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

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

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

逐題詳解

116%

(a) (8 pt) Suppose N processes with IDs 0, 1, 2, …, N − 1 need to share some critical region of code, which only one at a time is allowed to execute, and where they are required to repeatedly cycle through this section in the natural order (0, 1, 2, …, N − 1). If turn is a shared integer variable initialized to zero, and current_pid is the ID of the currently executing process, and each executes the following pseudo-code, could a race condition occur? Why or why not? Be precise.

while(TRUE)
{
  while(turn != current_pid);  /* wait */
  critical_region();
  turn = (turn + 1) % N;
  noncritical_region();
}

(b) (8 pt) How many semaphores would be required if a semaphore solution to this problem was used instead? Write some pseudo-code for this.

(a) 不會有 race condition

共用變數只有 turn,而會寫 turn 的只有「turn == current_pid」的那一個 process;其他 process 都卡在 while 裡,只讀不寫

所以任何時刻最多一個 process 能通過 while 進 critical region,它離開時才把 turn 改成下一個 ID → mutual exclusion 成立,順序也固定是 \(0,1,\dots,N-1\)

前提是對 int 的讀寫是 atomic。代價:輪到 \(i\) 但 \(i\) 還在 noncritical region 或根本不想進來時,其他人想進也進不去(progress 不成立),而且全程 busy waiting 浪費 CPU;不過這題要的就是固定輪流的順序

(b) 需要 \(N\) 個 semaphore,每個 process 一個;s[0] 初值 1,其餘初值 0

semaphore s[N];     // s[0] = 1,  s[1..N-1] = 0

// process i
while (TRUE) {
    wait(s[i]);                 // 等上一個人交棒
    critical_region();
    signal(s[(i + 1) % N]);     // 交棒給下一個
    noncritical_region();
}

一開始只有 s[0] 是 1,所以只有 process 0 能進;它做完 signal(s[1]),process 1 才能進,依序繞回 0

任何時刻 \(N\) 個 semaphore 的值加起來最多是 1,所以 mutual exclusion 成立;跟 (a) 比,等待的 process 是 block 住,不再 busy waiting

(a) 不會 (b) \(N\) 個 semaphore(初值 1,0,…,0),process \(i\) 先 wait(s[i])、離開時 signal(s[(i+1) mod N])

29%

Three processes A, B and C start executing (nearly) together in a multiprocessing environment, and require 7, 12 and 16 seconds, respectively, of run time. What will the average turnaround time be for each of the following scheduling algorithms:

(a) (2 pt) Shortest Job First scheduling (b) (3 pt) Preemptive Round Robin scheduling (c) (4 pt) Consider a preemptive priority-based scheduling in which a process with priority \(i\) will be demoted to priority \(i+1\) if it cannot be finished in given time quantum. The time quantum of priority \(i\) is \(2^i\) seconds. Assume A, B and C start executing in that order, and each initially starts with priority 0.

三個都在 0 到達,burst 7、12、16

(a) SJF 短的先跑:A、B、C

ABC071935

完成時間 7、19、35 → \(\dfrac{7+19+35}{3}=\dfrac{61}{3}\approx20.33\) 秒

(b) Round Robin 題目沒給 time quantum,取 1 秒,照 A→B→C 輪流

ABCABCABCABCABCABCABCBCBCBCBCBC012345678910111213141516171819202122232425262728293035

0–18:三個各跑 6 輪(A 剩 1、B 剩 6、C 剩 10);18–19 A 跑完,完成 19;19–30 只剩 B、C 交替,B 在 30 完成(C 剩 5);30–35 C 跑完

\(\dfrac{19+30+35}{3}=\dfrac{84}{3}=28\) 秒

quantum 趨近 0(processor sharing)時,A、B、C 分別在 21、31、35 完成,平均 29 秒

(c) 降級式 priority 數字小的優先;priority \(i\) 的 quantum 是 \(2^i\),用不完就降一級,同一級照 A、B、C 的順序

ABCABCABCBCC0123579131721263435

priority 0(q=1):A、B、C 各跑 1,剩 6、11、15

priority 1(q=2):各跑 2,剩 4、9、13

priority 2(q=4):A 跑 4 在 13 完成;B 跑 4 剩 5;C 跑 4 剩 9

priority 3(q=8):B 跑 5 在 26 完成;C 跑 8 剩 1;priority 4(q=16):C 跑 1 在 35 完成

\(\dfrac{13+26+35}{3}=\dfrac{74}{3}\approx24.67\) 秒

(a) \(61/3\approx20.33\) 秒 (b) 28 秒(quantum = 1) (c) \(74/3\approx24.67\) 秒

310%

(10 pt) Consider a file system that use inodes to represent files. Disk blocks are 16KB in size, and a pointer to a disk block requires 2 bytes. The file system keeps 15 pointers of the index block in the file's inode. The first 12 pointers point to direct blocks. The next 3 pointers point to single, double and triple indirect blocks, respectively. What is the maximum size of a file that can be stored in this file system?

Hint:

modeowners (2)timestamps (3)size / block countdirect blocks(12 個)single indirectdouble indirecttriple indirectdatadata… 共 12 個 data blockindexdata(2¹³ 個)data三層 index → data

一個 index block 放得下的 pointer 數:\(\dfrac{16\text{ KB} }{2\text{ B} }=\dfrac{2^{14} }{2}=2^{13}\)

pointer指到的 data block 數容量
12 個 direct12192 KB
single indirect\(2^{13}\)128 MB
double indirect\((2^{13})^2=2^{26}\)1 TB
triple indirect\((2^{13})^3=2^{39}\)8 PB

最大檔案 \(=(12+2^{13}+2^{26}+2^{39})\times2^{14}\text{ B}\approx8\text{ PB}+1\text{ TB}+128\text{ MB}+192\text{ KB}\)

這是 inode 結構本身能表示的上限。另外 pointer 只有 2 bytes,整個 disk 最多只能編 \(2^{16}\) 個 block(\(2^{16}\times16\text{ KB}=1\text{ GB}\)),實際存得下的檔案還會被這個上限擋住

\((12+2^{13}+2^{26}+2^{39})\times16\text{ KB}\approx8\text{ PB}\)

415%

(15 pt) How many page faults occur for the three algorithms named FIFO, Optimal and LRU for the following reference string, for 5 page frames?

1, 2, 3, 4, 1, 3, 9, 4, 5, 6, 9, 7, 8, 9, 7, 9, 4, 3, 2, 5, 1, 6, 7, 8

FIFO(frames 由舊到新)

ref123413945697
fault✓✓✓✓✓✓✓✓
frames112123123412341234123491234923495349563495649567
ref897943251678
fault✓✓✓✓✓✓✓✓✓
frames956789567895678956785678467843784328432543251325162516751678

fault 共 17 次

Optimal(換掉下一次用到最晚、或不再用到的 page;frames 以位置表示)

ref123413945697
fault✓✓✓✓✓✓✓✓
frames112123123412341234123491234952349623496234972349
ref897943251678
fault✓✓✓✓✓
frames783497834978349783497834978349782497854978149786497864978649

fault 共 13 次

最後 2、5、1、6 進來時,被換掉的都是之後不再用到的 page(3、4、9 與剛用完的),選哪一個 fault 數都一樣

LRU(frames 由最久沒用到→最近用到)

ref123413945697
fault✓✓✓✓✓✓✓✓
frames112123123423412413241392139413945394563456945697
ref897943251678
fault✓✓✓✓✓✓✓✓✓
frames569785678956897568796879487943794329432543251325162516751678

fault 共 17 次

FIFO 17 次 Optimal 13 次 LRU 17 次

510%

(10 pt) If a 16-bit adder with a carry-in pin is designed by logic gates, and the delay time for an N inputs logic gate is N × T. Please determine and explain if the maximum delay time of this adder can be smaller than 22 × T ?

2 輸入 gate 是 \(2T\),fan-in 越大越慢。先看兩個常見做法:

ripple-carry:\(g_i=a_ib_i\)、\(p_i=a_i\oplus b_i\) 要 \(2T\),每一位 \(c_{i+1}=g_i+p_ic_i\) 要 AND2+OR2=\(4T\),16 位 \(2T+16\times4T=66T\)

4 位一組、兩層 CLA:組內 \(G\) 用 AND4+OR4=\(8T\),第二層 \(8T\),回組內算 \(c_{15}\) 再 \(8T\),加上 \(p,g\) 的 \(2T\) 與最後 XOR 的 \(2T\):\(28T\) → 都超過 \(22T\)

改用 parallel prefix:把 carry-in 當成第 \(-1\) 位(\(g_{-1}=c_0,\ p_{-1}=0\)),則 \(c_{i+1}=G_{[i:-1]}\),合併兩段的運算

\((G,P)\circ(G',P')=(G+P\,G',\ P\,P')\) → AND2+OR2,\(4T\);每一層涵蓋的位數加倍

層涵蓋位數完成時間
\(p,g\)1\(2T\)
第 1 層2\(6T\)
第 2 層4\(10T\)
第 3 層8\(14T\)
第 4 層16\(18T\)

\(c_1\sim c_{15}\) 要涵蓋的範圍最多 16 位,在 \(18T\) 全部算好;\(s_i=p_i\oplus c_i\) 再加 \(2T\) → 所有 sum 在 \(20T\) 完成

carry-out \(c_{16}\) 要涵蓋 17 位,最後一步改用 3 輸入的 \(G_{[15:8]}+P_{[15:8]}G_{[7:0]}+P_{[15:8]}P_{[7:0]}c_0\)(AND3+OR3,\(6T\)),輸入都在 \(14T\) 就緒 → \(20T\)

最大 delay \(=20T\lt22T\)

可以,最大 delay 可做到 \(20T\lt22T\)(parallel prefix / 多層 carry lookahead)

615%

(15 pt) The following techniques have been developed to reduce cache miss penalty or miss rate: "multi-banked caches", "small and simple caches", and "hardware prefetching of instructions". Please briefly explain these techniques and how they work.

multi-banked caches 把 cache 切成幾個可獨立存取的 bank,block address 交錯分配(例:block \(i\) 放 bank \(i\bmod4\))

不同 bank 的存取可以同時進行,提高 cache bandwidth(可 pipeline 多個存取),每個 bank 也比整塊 cache 小、存取較快

small and simple caches cache 做小、associativity 做低(例如 direct-mapped)

tag 比對與選資料的電路變短,hit time 變短、也容易塞進一個 clock cycle,tag 比對還能和資料讀取重疊

代價是 miss rate 可能上升,所以多用在 L1

hardware prefetching of instructions miss 時除了抓需要的 block,同時把下一個連續 block 抓進 stream buffer

指令循序執行,下次要用的指令若在 stream buffer 裡就直接拿,不必再到下一層等,利用 spatial locality 降低 miss penalty 與實際的 miss 數

715%

The following figure shows the datapath for MIPS R-type ALU instructions.

(原題附圖:register file(Read register 1/2、Write register、Write data、Read data 1/2)+ ALU + ALU control;Instr[25:21]→Read register 1、Instr[20:16]→Read register 2、Instr[15:11]→Write register、Instr[5:0]→ALU control,ALU 結果直接接回 Write data)

(a) (9 pt) Please list the three additional multiplexors needed to support LW (load word; I-type).

(b) (6 pt) The program execution time of the single-cycle datapath can be expressed as IC (instruction count) × CPI (cycles per instruction) × clock cycle time. Please describe how these three parameters are affected for the multi-cycle implementation & the pipelined implementation respectively.

(a) lw 的格式是 lw rt, offset(rs):位址 = rs + sign-extended offset,結果寫回 rt(不是 rd);資料來自 data memory(不是 ALU)。要加三個 mux:

Read register 1Read register 2Write registerWrite dataRead data 1Read data 2RegistersInstr[25:21]Instr[20:16]Instr[15:11]01① RegDstALU01② ALUSrcsign-extendInstr[15:0]ALUcontrolInstr[5:0]DatamemoryAddressRead data01③ MemtoReg

① RegDst:Write register 前面,R-type 寫 rd(Instr[15:11]),lw 寫 rt(Instr[20:16])

② ALUSrc:ALU 第二個輸入前面,R-type 用 Read data 2,lw 用 sign-extended 的 16-bit offset(Instr[15:0])算位址

③ MemtoReg:Write data 前面,R-type 寫回 ALU result,lw 寫回 data memory 讀出的資料

圖中另外多出來的 sign-extend 與 data memory 是功能單元,不是 mux

(b)

ICCPIclock cycle time
multi-cycle不變變大(每道指令 3~5 cycles)變短(只需容納一個步驟)
pipelined不變理想約 1;hazard 的 stall 讓它略大於 1變短(最慢 stage + pipeline register 的 overhead)

IC 只由 ISA 與 compiler 決定,換實作方式不會變

multi-cycle 是拿較大的 CPI 換較短的 cycle time;pipeline 讓指令重疊執行,CPI 維持在 1 附近,同時 cycle time 縮短,所以效能最好

(a) RegDst、ALUSrc、MemtoReg (b) IC 皆不變;multi-cycle:CPI↑、cycle time↓;pipelined:CPI≈1、cycle time↓

810%

Consider the following MIPS assembly code

LOOP:  lw   $t0,-4($s0)    # assume $s0 has an initial value of 100
       sw   $t0,996($s0)
       addi $s0,$s0,-4
       bne  $s0,$zero,LOOP
Exit:

How many cycles does it need for the above loop (i.e. till the last bne taking effect)?

(a) (3 pt) on a single-cycle datapath; (b) (4 pt) on a classical 5-stage pipelined datapath (IF/ID/EX/MEM/WB) with interlock only (no forwarding or branch prediction); Assume bne is resolved at the EX stage. (c) (3 pt) on a classical 5-stage pipelined datapath with full forwarding.

$s0 從 100 每次減 4,到 0 離開:共 \(100/4=25\) 輪,每輪 4 道指令,共 100 道

(a) single-cycle:一道指令一個 cycle → 100 cycles

(b) 假設 register file 前半 cycle 寫、後半 cycle 讀;bne 在 EX 決定後才 fetch 下一道。一輪內兩個 RAW,都要等前一道寫回才能在 ID 讀到:

\(\text{lw}\to\text{sw}\)($t0)、\(\text{addi}\to\text{bne}\)($s0);ID 標 * 的是卡住等待

123456789101112
lw $t0,-4($s0)IFIDEXMEMWB
sw $t0,996($s0)IFID*ID*IDEXMEMWB
addi $s0,$s0,-4IFIF*IF*IDEXMEMWB
bne $s0,$zero,LOOPIFID*ID*IDEXMEMWB
下一輪 lwIFID

下一輪 lw 在 cycle 11 fetch,一輪 10 cycles(下一輪 lw 用的 $s0 在 cycle 9 已寫回,不會再卡)

第 25 輪從 cycle \(1+24\times10=241\) 開始,bne 在 \(241+11=252\) 完成 WB:\(24\times10+12=252\)

(c) full forwarding:addi → bne 從 EX/MEM forward 到 bne 的 EX,不用 stall;lw → sw 的 $t0 是 sw 的存入資料,在 MEM 才用,可由 MEM/WB 直接送到,也不用 stall

bne 仍在 EX 決定、沒有 prediction,後面兩道指令被沖掉(taken 時):一輪 \(4+2=6\) cycles

12345678
lw $t0,-4($s0)IFIDEXMEMWB
sw $t0,996($s0)IFIDEXMEMWB
addi $s0,$s0,-4IFIDEXMEMWB
bne $s0,$zero,LOOPIFIDEXMEMWB
(被沖掉)IF×ID×
下一輪 lwIFID

第 25 輪從 cycle \(1+24\times6=145\) 開始,bne 的 WB 在 cycle 152:\(24\times6+8=152\)

若計到最後一個 bne 在 EX 決定為止,(b) 是 250、(c) 是 150;若 forwarding 只送到 EX(lw→sw 仍要 1 個 stall),(c) 一輪 7 cycles,共 \(24\times7+8=176\)

(a) 100 (b) 252 (c) 152(cycles)

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

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

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

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com