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

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

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

逐題詳解

14 分

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

(a) \(32=2^{5}\)

00100000

(b) \(98=64+32+2\)

01100010

26 分

Suppose a program with 10000 instructions is executed by a CPU with a clock rate of 1 GHz. Suppose the CPU time of the program is 20000 nanosecond (or 20000 ns). (a) (2 分) Find the clock period of the CPU (in ns), (b) (2 分) Determine the number of CPU clock cycles for the execution of the program, (c) (2 分) Determine the average CPI (clock cycles per instruction) of the program.

(a) clock period = \(1/\text{clock rate}=1/(10^{9}\text{ Hz})\)

1 ns

(b) cycles = CPU time ÷ clock period = \(20000\text{ ns}/1\text{ ns}\)

20000 cycles

(c) CPI = cycles ÷ instructions = \(20000/10000\)

2.0

320 分

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. In addition, there are no cache misses.

(a) (10 分) Suppose the following code sequence is executed on the processor. Identify all the data hazards which can be solved by forwarding. Determine the total number of clock cycles required for the execution of the code sequence.

LD  R5, 12(R3);   R5 <- MEM[12+R3]
ADD R4, R1, R5;   R4 <- R1+R5
ADD R6, R4, R2;   R6 <- R4+R2

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

ADD R5, R3, R7;   R5 <- R3+R7
ADD R1, R2, R5;   R1 <- R2+R5
SUB R4, R1, R9;   R4 <- R1-R9

(a) RAW hazard:I1(LD)→ I2:R5;I2 → I3:R4

I2 → I3(R4):ALU 的結果在 EX 結束就有,EX/MEM → EX 轉送,可以解決,不用 stall

I1 → I2(R5):load 的資料要到 MEM 結束才有,I2 在下一個 cycle 的 EX 就要用,趕不上,load-use hazard:要 stall 1 個 cycle,再由 MEM/WB 轉送給 I2 的 EX

cycle12345678
LDIFIDEXMEMWB
ADD R4IFIDstallEXMEMWB
ADD R6IFstallIDEXMEMWB

forwarding 可解的:R4(I2→I3);R5(I1→I2)轉送前要先 stall 1 個 cycle。總共 \(5+2+1=8\) 個 clock cycles

(b) RAW hazard:I1 → I2:R5;I2 → I3:R1,三條都是 ALU 指令,結果在 EX 結束就有,各轉送給下一條指令的 EX,沒有 stall

cycle1234567
ADD R5IFIDEXMEMWB
ADD R1IFIDEXMEMWB
SUB R4IFIDEXMEMWB

R5(I1→I2)、R1(I2→I3)兩個都能靠 forwarding 解決;總共 \(5+2=7\) 個 clock cycles

410 分

Consider a direct mapped cache with 64 blocks. (a) (3 分) What cache block number of the cache does block address 129 map to? (b) (3 分) What cache block number of the cache does block address 650 map to? (c) (2 分) Suppose the miss rate for accessing the cache is 0.01 misses per memory access. Assume the hit time is 1 clock cycle, and the miss penalty is 100 clock cycles. What is the average memory access time (AMAT) of the cache (in clock cycles)? (d) (2 分) (Continued from part (c)) Suppose there are 400 memory accesses to the cache. Find the total number of memory-stall cycles.

direct mapped:cache block number = block address mod(cache 的 block 數)

(a) \(129\bmod64=129-128=1\)

block 1

(b) \(650\bmod64=650-640=10\)

block 10

(c) AMAT = hit time + miss rate × miss penalty = \(1+0.01\times100\)

2 個 clock cycles

(d) memory-stall cycles = 存取次數 × miss rate × miss penalty = \(400\times0.01\times100\)(即 4 次 miss,每次 100 cycles)

400 個 clock cycles

510 分

Consider a MIPS processor with separate instruction and data memories. Suppose a code sequence with 15 load instructions, 15 store instructions, and 50 ALU instructions is executed on the processor. (a) (4 分) Determine the number of accesses to the instruction memory, (b) (3 分) Determine the number of accesses to the data memory, (c) (3 分) Suppose there are four miss in the instruction memory for the code sequence. Compute the miss rate of the instruction memory.

指令共 \(15+15+50=80\) 條

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

80 次

(b) 只有 load 與 store 在 MEM 階段存取 data memory,ALU 指令不用:\(15+15\)

30 次

(c) miss rate = \(\dfrac{4}{80}\)

5%

620 分

Please answer following questions about process scheduling: (a) Illustrate the diagram of process state and each state transition. (10 分) (b) Name the three schedulers. And explain their responsibility and difference for the process scheduling. (6 分) (c) What are the two methods in passing parameters between processes? (4 分)

(a) 五個狀態:new(正在建立)、ready(在記憶體、等 CPU)、running(正在 CPU 上執行)、waiting(等 I/O 或某個事件)、terminated(執行結束)

admittedscheduler dispatchinterrupt(時間到)exitI/O 或 event waitI/O 或 event 完成newreadyrunningterminatedwaiting

admitted:new → ready,process 建好、被接受進系統,放進 ready queue

scheduler dispatch:ready → running,CPU scheduler 選中,dispatcher 把 CPU 交給它

interrupt:running → ready,時間片用完(或被優先權較高的 process 搶先),回 ready queue 重新排隊

I/O 或 event wait:running → waiting,呼叫 I/O 或等某事件,主動讓出 CPU

I/O 或 event 完成:waiting → ready,等的事發生了,回到 ready queue(不直接回 running)

exit:running → terminated,執行結束,資源由 OS 回收

(b) 三種 scheduler:

scheduler責任執行頻率與特色
long-term scheduler(job scheduler)決定哪些 job 從 job pool 載入記憶體、成為 ready 的 process;控制 degree of multiprogramming,搭配 I/O-bound 與 CPU-bound 的比例頻率最低(秒~分鐘),可以慢;分時系統常沒有這一層
short-term scheduler(CPU scheduler)從 ready queue 選下一個用 CPU 的 process,並由 dispatcher 切換頻率最高(毫秒),必須很快
medium-term scheduler(swapper)把部分 process 暫時 swap out 到磁碟、之後再 swap in,降低多工程度、緩解記憶體壓力頻率居中,只在需要時才做

差別:long-term 管「誰能進系統」,medium-term 管「誰留在記憶體」,short-term 管「現在誰用 CPU」;執行頻率由低到高依序是 long、medium、short

(c) process 之間互傳資料(IPC)有兩種方式:

① shared memory:建立一塊雙方共用的記憶體區域,process 直接讀寫;速度快(只有建立時需要 kernel),但同步(誰先寫、誰讀)要自己處理

② message passing:用 send(message)、receive(message) 由 OS 轉送訊息;不用共用變數,容易同步、適合分散式系統,但每次都經過 kernel,較慢

若「parameters」是指 system call 的參數,傳法有三種:放暫存器、放記憶體 block(位址放暫存器)、放 stack;題目問兩種,取 IPC 的兩種模型

720 分

Please draw the Gantt Chart and compute the average waiting time of the execution of the different processes for following scheduling algorithms.

ProcessCPU execution timeArrival time
P150
P262
P331
P473
P555

(a) First-Come, First Served (FCFS) (5 分) (b) Preemptive Shortest-Job-First (SJF) (5 分) (c) Non-preemptive Shortest-Job-First (SJF) (5 分) --- Volunteers to release CPU (d) Round Robin (RR) with time slice = 4 time units. (5 分)

waiting time = 完成時間 − 到達時間 − CPU time(所有 process 的 waiting time 平均)

(a) FCFS:依到達順序 P1(0)、P3(1)、P2(2)、P4(3)、P5(5)

P1P3P2P4P5058142126

完成:P1=5、P3=8、P2=14、P4=21、P5=26

waiting:P1=0、P3=\(8-1-3=4\)、P2=\(14-2-6=6\)、P4=\(21-3-7=11\)、P5=\(26-5-5=16\)

\(\dfrac{0+4+6+11+16}{5}=\dfrac{37}5=7.4\)

(b) Preemptive SJF(SRTF):每當有 process 到達或完成,就選剩餘時間最短的

t=0 只有 P1;t=1 P3 到達,剩 3 比 P1 剩 4 短,搶先;P3 跑到 4 結束;剩 P1(4)、P2(6)、P4(7),P1 最短;t=5 P5(5)到達,P1 剩 3,不搶;P1 在 8 結束;剩 P2(6)、P4(7)、P5(5)→ P5、P2、P4

P1P3P1P5P2P40148131926

完成:P1=8、P3=4、P5=13、P2=19、P4=26

waiting:P1=\(8-0-5=3\)、P3=\(4-1-3=0\)、P5=\(13-5-5=3\)、P2=\(19-2-6=11\)、P4=\(26-3-7=16\)

\(\dfrac{3+0+3+11+16}{5}=\dfrac{33}5=6.6\)

(c) Non-preemptive SJF:process 一旦開始就跑到結束,結束後選等待中 CPU time 最短的

P1 0–5;此時到達的有 P3(3)、P2(6)、P4(7)、P5(5),選 P3 → 5–8;剩 P2(6)、P4(7)、P5(5),選 P5 → 8–13;再 P2、P4

P1P3P5P2P4058131926

waiting:P1=0、P3=\(8-1-3=4\)、P5=\(13-5-5=3\)、P2=\(19-2-6=11\)、P4=\(26-3-7=16\)

\(\dfrac{0+4+3+11+16}{5}=\dfrac{34}5=6.8\)

(d) RR,time slice = 4:新到的 process 排在被搶走的 process 之前(同時發生時)

時間執行執行後 ready queue(頭 → 尾)
0–4P1(剩 1)P3、P2、P4、P1
4–7P3(完成)P2、P4、P1、P5
7–11P2(剩 2)P4、P1、P5、P2
11–15P4(剩 3)P1、P5、P2、P4
15–16P1(完成)P5、P2、P4
16–20P5(剩 1)P2、P4、P5
20–22P2(完成)P4、P5
22–25P4(完成)P5
25–26P5(完成)(空)

P1P3P2P4P1P5P2P4P504711151620222526

完成:P1=16、P3=7、P2=22、P4=25、P5=26

waiting:P1=\(16-0-5=11\)、P3=\(7-1-3=3\)、P2=\(22-2-6=14\)、P4=\(25-3-7=15\)、P5=\(26-5-5=16\)

\(\dfrac{11+3+14+15+16}{5}=\dfrac{59}5=11.8\)

810 分

Please answer following question about synchronization: (a) What is a critical-section? (4 分) (b) What is busy waiting? (2 分) What is the advantage and disadvantage of busy waiting? (4 分)

(a) critical section:process 中存取共用資料(共用變數、檔案、裝置)的那一段程式碼。同一時間只能有一個 process 在這段程式碼裡面,否則結果會依執行順序而定(race condition)。設計時要滿足 mutual exclusion、progress、bounded waiting 三個要求;程式結構是 entry section → critical section → exit section → remainder section

(b) busy waiting:等待某個條件成立時,不放棄 CPU,在迴圈裡不斷檢查(例如 while (lock == 1);),這種鎖叫 spinlock

優點:不用 block/wakeup,沒有 context switch 與 system call 的成本;預期等待時間很短(比 context switch 還短)、特別是在多處理器上(持鎖者在另一顆 CPU 上很快就放鎖)時,效率比睡下去再叫醒好

缺點:等待期間白白吃掉 CPU 時間;等待時間長時浪費大;在單一 CPU 上,持有鎖的 process 得不到 CPU,要等時間片用完才換人;也可能造成 priority inversion

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

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

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

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com