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

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

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

逐題詳解

120 分

Consider the following code sequence.

SUB R1, R4, R3;     R1 <- R4-R3
ADD R5, R1, R6;     R5 <- R1+R6
LW  R8, 12(R6);     R8 <- MEM[12+R6]
ADD R9, R5, R8;     R9 <- R5+R8
SW  R9, 16(R6);     MEM[16+R6] <- R9

Suppose the code sequence is executed in 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) (5 分) Identify all the data hazards which can be solved by forwarding.

(b) (5 分) Identify all the data hazards which can not be solved by forwarding.

(c) (5 分) Determine the total number of clock cycles required for the execution of the code sequence.

(d) (5 分) Suppose the clock rate of the MIPS processor is 1GHz. Find the CPU time of the code sequence (in terms of ns).

編號 I1~I5。找 RAW(後面的指令讀前面指令寫入的暫存器):

producer → consumer暫存器結果產生的時間consumer 需要的時間
I1 → I2R1I1 的 EX 末I2 的 EX 初
I2 → I4R5I2 的 EX 末I4 的 EX(相隔兩條)
I3 → I4R8I3(lw)的 MEM 末I4 的 EX 初
I4 → I5R9I4 的 EX 末I5(sw)要存的資料

(a) 結果比 consumer 需要的時間早出現,把 EX/MEM、MEM/WB 暫存器的值轉送回 ALU 就行:

I1→I2(R1)、I2→I4(R5)、I4→I5(R9)

(b) lw 的資料要到 MEM 階段結束才有,而 I4 緊接在後、下一個 cycle 就要在 EX 用,轉送也來不及(得往回送到過去的時間):load-use hazard

I3→I4(R8):要 stall 1 個 cycle

(c) I4 在 ID 多停一個 cycle(hazard detection 插入 bubble),之後 R8 由 MEM/WB 轉送到 EX

cycle12345678910
I1 SUBIFIDEXMEMWB
I2 ADDIFIDEXMEMWB
I3 LWIFIDEXMEMWB
I4 ADDIFIDstallEXMEMWB
I5 SWIFstallIDEXMEMWB

沒有 stall:\(5+4=9\) cycles;多 1 個 stall → 10 cycles

10 個 clock cycles

(d) 1 GHz → 每個 cycle 1 ns

CPU time = \(10\times1\text{ ns}=10\) ns

220 分

Consider the following code sequence.

ADD R1, R2, R3;     R1 <- R2+R3
ADD R3, R4, R5;     R3 <- R4+R5
LW  R6, 32(R8);     R6 <- MEM[32+R8]
LW  R7, 36(R8);     R7 <- MEM[36+R8]
ADD R9, R6, R1;     R9 <- R6+R1
ADD R10, R7, R3     R10 <- R7+R3
ADD R11, R9, R10    R11 <- R9+R10
LW  R12, 40(R8)     R12 <- MEM[40+R8]

Suppose the code sequence is executed in a five-stage (IF, ID, EX, MEM and WB) MIPS pipeline processor with separate instruction and data memories.

(a) (5 分) Determine the number of accesses to the instruction memory.

(b) (5 分) Determine the number of accesses to the data memory.

(c) (5 分) Suppose the execution of the code sequence produces 3 misses: 2 misses in the instruction memory, and 1 miss in the data memory. Determine the miss rate of the instruction memory. Determine the miss rate of the data memory.

(d) (5 分) Suppose the hit time of the instruction and data memories is 1 clock cycle. The miss penalty is 100 clock cycles. Based on the results in part (c), please compute the average memory access time (in terms of clock cycles) of instruction and data memories, respectively.

(a) 每條指令在 IF 階段讀 instruction memory 一次,共 8 條

8 次

(b) 只有 load/store 在 MEM 階段碰 data memory;這段程式有 3 個 LW,沒有 SW,ADD 不存取

3 次

(c) miss rate = miss 次數 ÷ 存取次數

instruction memory:\(\dfrac28=25\%\)  data memory:\(\dfrac13\approx33.3\%\)

instruction:25%;data:33.3%(\(\tfrac13\))

(d) AMAT = hit time + miss rate × miss penalty

instruction:\(1+0.25\times100=26\) cycles

data:\(1+\tfrac13\times100\approx34.3\) cycles

instruction 26 cycles;data ≈ 34.3 cycles

310 分

Briefly explain the following terms. (a) (2 分) Control hazard, (b) (2 分) Direct mapped cache, (c) (2 分) Fully associative cache, (d) (2 分) Translation lookaside buffer (TLB), (e) (2 分) Direct memory access (DMA).

(a) Control hazard:branch/jump 還沒決定跳不跳(或跳去哪)時,下一條要抓的指令就已經得抓了,若抓錯,pipeline 裡已經進來的指令要作廢(flush)或先 stall。解法:branch prediction、提早在 ID 比較、delayed branch

(b) Direct mapped cache:每個 memory block 只能放進 cache 中唯一一個位置:\(\text{index}=\text{block address}\bmod\text{cache 的 block 數}\),用 tag 判斷是不是要的那個 block。硬體簡單、速度快,但兩個 block 對應到同一個 index 會互相踢出(conflict miss)

(c) Fully associative cache:memory block 可以放在 cache 的任何位置,查找時同時比對所有 entry 的 tag。沒有 conflict miss,但每個 entry 都要一個比較器、成本高,需要替換政策(LRU 等),只適合很小的 cache(如 TLB)

(d) TLB:存放最近用過的 virtual page → physical frame 對應(page table entry)的小型高速 cache。位址轉換先查 TLB,hit 就直接得到 physical address,省掉再讀 memory 中 page table 的那一次存取;miss 才去查 page table

(e) DMA:由 DMA controller 直接在 I/O 裝置與主記憶體之間搬一整個 block 的資料,不用 CPU 一個字一個字處理。CPU 只負責先設定(記憶體位址、搬運量、方向),搬完 DMA 以 interrupt 通知 CPU,CPU 可以同時做別的事

414 分

Processes in the system move in different states, e.g., “new” and “terminated”. Please answer the following questions.

(a) (5 分) Illustrate the diagram of process state and explain each state transition in detail.

(b) (9 分) Please list at least three other process states and show their relationship. At the same time, please define “short-term scheduling,” “mid-term scheduling,” and “long-term scheduling.”

(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) 另外三個狀態:

① ready suspended:process 在 ready 的狀態被 swap 到磁碟,不佔記憶體;

② waiting(blocked)suspended:在等待事件時被 swap 到磁碟;

③ zombie:已經 terminated,但 parent 還沒用 wait() 取走結束狀態,process table 裡的項目還留著

admitdispatchtimeoutexitevent waitevent 完成swap outswap inswap outevent 完成newreadyrunningterminatedwaitingreadysuspendedwaitingsuspended

關係:記憶體不夠時,OS 把 ready 的 process swap out → ready suspended,等記憶體有空再 swap in 回 ready;waiting 的 process 也可 swap out → waiting suspended;waiting suspended 的事件發生後變 ready suspended,再 swap in 回 ready。process 終止後(zombie)等 parent 回收才真的消失

scheduling 定義:

long-term scheduling(job scheduler):決定哪些 job(new)可以被接受、載入記憶體成為 ready 的 process,控制 degree of multiprogramming,並調配 I/O-bound 與 CPU-bound 的比例;頻率最低(秒~分鐘)

mid-term scheduling:決定哪些 process 暫時 swap out 到磁碟、何時 swap in 回來,降低多工程度、緩解記憶體壓力(對應 suspended 狀態);頻率居中

short-term scheduling(CPU scheduler):從 ready queue 選下一個要用 CPU 的 process;頻率最高(毫秒級),必須很快

514 分

Please answer following questions about CPU scheduling:

(a) (4 分) Please define “FIFO/FCFS” and “Round Robin” scheduling algorithms.

(b) (10 分) Please draw the Gantt Chart and compute the average waiting time of the execution of the following processes with the Round Robin scheduling with time slice = 3 time units.

ProcessCPU execution timeArrival time
P152
P260
P334
P445

(a) FIFO/FCFS:先到先服務。ready queue 是先進先出的 queue,CPU 依到達順序交給 process,跑到結束(或自己等 I/O)才換人,non-preemptive;實作簡單,但短工作可能卡在長工作後面(convoy effect)

Round Robin:給每個 process 一個固定的 time quantum(time slice)。排在 ready queue 最前面的 process 拿到 CPU,用完 quantum 還沒結束就被搶走、排到 queue 尾端,下一個接手;是 preemptive 的 FCFS,回應時間好、公平。quantum 太大退化成 FCFS,太小 context switch 的 overhead 大

(b) time slice = 3;新到的 process 先排進 queue,再把被搶走的 process 排到尾端(這題沒有「同時到達」的情形)

時間執行剩餘執行後的 ready queue(頭 → 尾)
0–3P2P2 剩 3P1(t=2 到)、P2
3–6P1P1 剩 2P2、P3(t=4)、P4(t=5)、P1
6–9P2P2 完成P3、P4、P1
9–12P3P3 完成P4、P1
12–15P4P4 剩 1P1、P4
15–17P1P1 完成P4
17–18P4P4 完成(空)

P2P1P2P3P4P1P4036912151718

完成時間:P1=17、P2=9、P3=12、P4=18

waiting time = 完成時間 − 到達時間 − CPU 時間

P1:\(17-2-5=10\) P2:\(9-0-6=3\) P3:\(12-4-3=5\) P4:\(18-5-4=9\)

average waiting time = \(\dfrac{10+3+5+9}{4}=\dfrac{27}{4}=6.75\) time units

613 分

Please answer following question about synchronization: (a) (3 分) What is a critical-section? (b) (3 分) What are the three requirements of critical-section? (c) (3 分) What is busy waiting? (d) (4 分) What is the advantage and disadvantage of busy waiting?

(a) critical section:process 中存取共用資料(共用變數、檔案、裝置)的那段程式碼。同一時間只能有一個 process 在裡面,否則結果會依執行順序而變(race condition)

(b) 三個要求:

① mutual exclusion:一個 process 在它的 critical section 裡時,其他 process 不能進入自己的 critical section

② progress:沒有人在 critical section 時,想進入的 process 中要能選出一個進去,這個選擇不能被「不想進入的 process」影響,也不能無限期拖延

③ bounded waiting:一個 process 提出進入要求後,在它被允許進入之前,其他 process 能先進入的次數有上限(不會 starvation)

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

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

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

79 分

Given a computer system with a 32-bit virtual address, 4KB pages, and 8B per page entry of a page table. Suppose that the number of bits of physical addresses is 48, and the system is byte-addressable. If multiple-level paging is implemented, please answer the following questions:

(a) (4 分) What is the maximum number of (physical) page frames in the system, where each page frame is the container to store the data of one (virtual) page? What is the maximum number of pages for a process?

(b) (5 分) Suppose that the multi-level paging is adopted. How many levels do we need?

page 大小 4KB = \(2^{12}\) bytes → page offset 12 bit

(a) physical address 48 bit:frame 數 = \(2^{48}/2^{12}=2^{36}\)

virtual address 32 bit:page 數 = \(2^{32}/2^{12}=2^{20}\)(VPN 共 20 bit)

frame 最多 \(2^{36}\)(約 687 億);一個 process 最多 \(2^{20}\) 個 page(1,048,576 個)

(b) 每個 page-table 頁(4KB)放 \(4096/8=512=2^{9}\) 個 entry,一層索引 9 bit

要讓每一層 page table 剛好放進一個 page,20 bit 的 VPN 要分成每段 ≤ 9 bit:\(\lceil20/9\rceil=3\)

2 層不夠:\(9+9=18\lt20\),外層 table 要 \(2^{11}\) 個 entry × 8 B = 16 KB,放不進一個 page

3 層(例如 2+9+9 bit):每層 table 都不超過一頁

單層 page table 要 \(2^{20}\times8\text{ B}=8\) MB,必須連續放進記憶體,所以才分層

3 層

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

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

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

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com