免費詳解 / 師大 / 硬體 / 104

104 師大資工所考古題解答|硬體逐題詳解

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

逐題詳解

14 分

Convert the following unsigned 8-bit binary numbers to decimal numbers: (a) (2 分) 01110101 (b) (2 分) 11100000

(a) \(01110101_2=64+32+16+4+1\)

117

(b) \(11100000_2=128+64+32\)

224

26 分

Consider a CPU with clock cycle time 0.5 nanosecond (or 0.5 ns). Suppose the CPU executes a program with 1000 instructions. The average CPI (clock cycles per instruction) is 2.0 for the program. (a) (3 分) Find the clock rate of the CPU (in gigahertz, or GHz). (b) (3 分) Find the CPU time for executing the program (in ns).

(a) clock rate = \(\dfrac1{0.5\text{ ns}}=2\times10^{9}\) Hz

2 GHz

(b) CPU time = IC × CPI × cycle time = \(1000\times2.0\times0.5\text{ ns}\)

1000 ns

310 分

Consider a MIPS processor with separate instruction and data memories. Suppose the following code sequence is executed on the processor.

LW  R4, 24(R2);     R4 <- MEM[R2+24]
SUB R5, R1, R4;     R5 <- R1-R4
LW  R12, 32(R2);    R12 <- MEM[R2+32]
ADD R8, R12, R4;    R8 <- R12+R4
ADD R10, R2, R8;    R10 <- R2+R8
LW  R6, 8(R1);      R6 <- MEM[R1+8]
LW  R11, 16(R1);    R11 <- MEM[R1+16]
SUB R9, R11, R6     R9 <- R11-R6

(a) (2 分) Determine the number of accesses to the instruction memory. (b) (2 分) Determine the number of accesses to the data memory. (c) (3 分) Suppose there is only one miss in the instruction memory for the code sequence. Compute the miss rate of the instruction memory. (d) (3 分) Suppose there are two misses in the data memory for the code sequence. Compute the miss rate of the data memory.

(a) 每條指令在 IF 抓一次,共 8 條

8 次

(b) 只有 LW 在 MEM 階段存取 data memory(SUB、ADD 不用):LW 共 4 條

4 次

(c) \(\dfrac18\)

12.5%

(d) \(\dfrac24\)

50%

420 分

Consider a five-stage (IF, ID, EX, MEM and WB) MIPS pipeline processor with hazard detection and data forwarding units. Assume the processor includes separate instruction and data memories so that the structural hazard for memory references can be avoided.

(a) (8 分) Suppose the following code sequence is executed on the processor. Identify all the data hazards which can be solved by forwarding.

ADD R5, R7, R12;     R5 <- R7+R12
ADD R4, R5, R6;      R4 <- R5+R6
LW  R8, 12(R4);      R8 <- MEM[12+R4]

(b) (8 分) Repeat part (a) for the following code sequence.

LW  R9, 20(R7);      R9 <- MEM[R7+20]
ADD R1, R9, R5;      R1 <- R9+R5
SW  R1, 12(R6);      MEM[R6+12] <- R1

(c) (4 分) Suppose the following code sequence is executed on the processor. Determine the total number of clocks needed to execute the code sequence.

ADD R6, R2, R3;      R6 <- R2+R3
SUB R8, R5, R6;      R8 <- R5-R6

(a) RAW hazard:

I1 → I2:R5(相隔 1 條);I2 → I3:R4(相隔 1 條,lw 用 R4 當 base 算位址)

兩個 producer 都是 ALU 指令,結果在 EX 結束就有,把 EX/MEM 暫存器的值轉送到下一條指令的 EX 階段 ALU 輸入即可,沒有 stall

I1→I2(R5)與 I2→I3(R4),兩個都能靠 forwarding 解決

(b) RAW hazard:I1 → I2:R9(lw 後緊接使用);I2 → I3:R1(add 後緊接 sw 要存的資料)

I2 → I3:add 的結果在 EX 結束就有,轉送到 sw 的 EX/MEM 階段要存的資料路徑即可,可解

I1 → I2:lw 的資料要到 MEM 階段結束才讀出來,add 下一個 cycle 就要在 EX 用,forwarding 趕不上(load-use hazard),仍要 stall 1 個 cycle,插入 bubble 後再 forward

能靠 forwarding 解的:I2→I3(R1);I1→I2(R9)不行,要 stall 1 個 cycle

(c) SUB 的 R6 來自 ADD(相隔 1 條),forwarding 解決,沒有 stall

cycle123456
ADDIFIDEXMEMWB
SUBIFIDEXMEMWB

6 個 clock cycles(\(5+(2-1)\))

510 分

Briefly explain the following terms. (a) (4 分) Program Counter. (b) (3 分) Control Hazard. (c) (3 分) Direct Mapped Cache.

(a) Program Counter(PC):CPU 內的暫存器,存放下一條要抓取(或正在執行)的指令的位址。每抓一條指令自動 +4(MIPS,一個 word),遇到 branch、jump、call/return、interrupt 時載入新的位址。它是 process 狀態的一部分,context switch 時要存起來、之後載回

(b) Control hazard:pipeline 遇到 branch/jump 時,在還不知道跳不跳(跳去哪)之前就得抓下一條指令;若猜錯,已進入 pipeline 的錯誤指令要 flush,浪費 cycle。解法:branch prediction、提早在 ID 比較、delayed branch

(c) Direct Mapped Cache:每個 memory block 只能放在 cache 中固定的一個位置:\(\text{index}=\text{block address}\bmod\text{cache block 數}\),再用 tag 確認放的是不是要的那個 block。硬體簡單、存取快,但同 index 的 block 會互相踢出(conflict miss)

616 分

Please answer following questions about Memory-Management: (a) Explain the difference between internal and external fragmentation. (6 分) (b) What is dynamic loading? Please include its advantage. (4 分) (c) Address binding of instructions and data to memory address can happen at three stages. Please name the three stages. (3 分) (d) Name the three types of page table structure. (3 分)

(a) internal fragmentation:配給 process 的記憶體區塊比它要求的大,多出來的部分在區塊「內部」沒人用,別人也不能用。例:paging 時最後一頁只用了一部分;固定大小的 partition

external fragmentation:空閒記憶體加起來夠大,但被切成很多不連續的小塊(hole),沒有一塊夠大能滿足新的要求,浪費在區塊「外部」。例:連續配置(可變大小 partition)、segmentation;解法有 compaction(把 process 搬到一起)、或改用 paging(不要求連續)

(b) dynamic loading:routine 在被呼叫時才載入記憶體,沒被呼叫過的永遠不載入;routine 以 relocatable 格式放在磁碟上,呼叫時檢查是否已在記憶體,不在就載入並更新位址表

優點:不用的 routine 不占記憶體,記憶體使用率高,特別適合很大、很少用到的程式碼(例外處理);不必特別的 OS 支援

(c) compile time:編譯時就知道會放在記憶體哪裡,產生 absolute code;load time:載入時才決定,編譯產生 relocatable code;execution (run) time:執行中 process 還可以搬動,位址綁定拖到執行時,需要 MMU 等硬體(base/limit 或 page table)

(d) 三種 page table 結構:① hierarchical paging(multi-level page table);② hashed page table;③ inverted page table

710 分

For the processes shown in the table below. Assume the process arrived at the same time but in the order as they numbered.

ProcessBurst (ms)
P110
P229
P33
P47
P512

Please draw the Gantt Chart and computer the average waiting time using time quantum = 10 ms for: (a) Shortest-Job-First (SJF) (2 分) (b) First-Come, First Served (FCFS) (2 分) (c) Round-Robin (RR) (6 分)

5 個 process 同時(時間 0)到達,waiting time = 完成時間 − burst time

(a) SJF:burst 小的先跑:P3(3)、P4(7)、P1(10)、P5(12)、P2(29)

P3P4P1P5P20310203261

waiting:P3=0、P4=3、P1=10、P5=20、P2=32

\(\dfrac{0+3+10+20+32}{5}=\dfrac{65}5=13\) ms

(b) FCFS:依編號順序

P1P2P3P4P501039424961

waiting:P1=0、P2=10、P3=39、P4=42、P5=49

\(\dfrac{0+10+39+42+49}{5}=\dfrac{140}5=28\) ms

(c) RR,quantum = 10 ms,queue 順序 P1→P2→P3→P4→P5:

P1 剛好跑 10 結束;P2 跑 10,剩 19,排到隊尾;P3(3)、P4(7)一次跑完;P5 跑 10,剩 2;第二輪 P2 再跑 10,剩 9;P5 跑 2 結束;P2 最後跑 9 結束

P1P2P3P4P5P2P5P201020233040505261

完成時間:P1=10、P2=61、P3=23、P4=30、P5=52

waiting:P1=0、P2=\(61-29=32\)、P3=\(23-3=20\)、P4=\(30-7=23\)、P5=\(52-12=40\)

\(\dfrac{0+32+20+23+40}{5}=\dfrac{115}5=23\) ms

816 分

Please answer following questions about Synchronization and Deadlock: (a) Explain why spinlocks are not appropriate for single-processor systems yet are often used in multiprocessor systems? (4 分) (b) Explain what is race condition? Please give an example. (4 分) (c) Explain what is deadlock? (4 分) (d) Name the four conditions that prevent deadlock. (4 分)

(a) spinlock:拿不到鎖就在迴圈裡一直檢查(busy waiting),不睡眠

單一 CPU:鎖被別的 process 持有,那個 process 現在沒有在跑(CPU 在我這裡)。我空轉只是在燒 CPU,鎖不可能在我空轉時被放掉,要等我的時間片用完、被換下去才行,白白浪費整個時間片

多處理器:持有鎖的 process 在另一顆 CPU 上同時跑,很快就會放鎖;若等待時間比 context switch(睡下去再叫醒)還短,空轉等一下比 block 划算,省掉兩次 context switch 的成本

(b) race condition:多個 process(或 thread)並行存取同一份共用資料,而且至少一個在寫,結果取決於它們存取的先後順序(interleaving)

例:兩個 process 都執行 counter++,counter 起始是 5。counter++ 實際是 load、add、store 三個指令:

P1P2counter
load → reg1 = 55
load → reg2 = 55
reg1 = 6,store6
reg2 = 6,store6

兩次加 1 只加了一次(正確是 7)。解法:把 counter++ 放進 critical section,用 mutex/semaphore 互斥

(c) deadlock:一組 process 裡,每一個都在等待這組中另一個 process 手上占著的資源,所以沒有一個能繼續往下走,也永遠等不到

例:P1 拿著 R1 等 R2,P2 拿著 R2 等 R1

(d) 四個必要條件(deadlock 要四個同時成立;破壞任何一個就不會 deadlock):

① mutual exclusion:資源一次只能一個 process 使用

② hold and wait:process 占著已拿到的資源,同時等別的資源

③ no preemption:資源不能被強制搶走,只能由持有者自己釋放

④ circular wait:存在一個 process 環,每個 process 等待的資源被環上下一個 process 占著

98 分

Given the following page reference sequence, please use the LRU strategy to cache and replace pages. Suppose that we have 3 available frames, each of which could cache one page. Note that the frames are initially empty.

1 2 7 3 7 0 8 4 0 4 0 4 3 4 3 6 5 0 2 6 1 2 0

(a) Show the cached pages in the frames in each step of the page reference sequence. (4 分) (b) Calculate the miss ratio or page fault ratio, including the initial/compulsory page faults. (4 分)

(a) LRU:frame 滿了要放新頁時,換掉「最久沒被用到」的那一頁(命中也算被用到);F=page fault,H=hit

ref127370840404
frame 1111333888888
frame 222220000000
frame 37777744444
FFFFHFFFHHHH
ref34365026120
frame 133333000111
frame 200066622222
frame 344445556660
FHHFFFFFFHF

換頁依據:第 4 個 ref(3)時 1 最久沒用,換掉 1;第 6 個(0)時 2 最久,換 2;第 7 個(8)時 3 最久,換 3;第 8 個(4)時 7 最久,換 7;第 13 個(3)時 8 最久,換 8;第 16 個(6)時 0 最久,換 0;第 17 個(5)時 4 最久,換 4;第 18 個(0)時 3 最久,換 3;第 19 個(2)時 6 最久,換 6;第 20 個(6)時 5 最久,換 5;第 21 個(1)時 0 最久,換 0;第 23 個(0)時 6 最久,換 6

(b) 共 23 次存取;hit 在第 5、9、10、11、12、14、15、22 次,共 8 次;page fault = \(23-8=15\) 次(其中前 3 次是 compulsory)

page fault ratio = \(\dfrac{15}{23}\approx65.2\%\)

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

國立臺灣師範大學 109–115 的詳解收錄在付費版;106–115 全年度完整詳解共 309 頁,逐題推導。

購買 · NT$ 850 看師大其他免費詳解

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com