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
105 師大資工所硬體考古題的逐題詳解,共 8 題,免費公開。
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
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
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
| cycle | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| LD | IF | ID | EX | MEM | WB | |||
| ADD R4 | IF | ID | stall | EX | MEM | WB | ||
| ADD R6 | IF | stall | ID | EX | MEM | WB |
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
| cycle | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|
| ADD R5 | IF | ID | EX | MEM | WB | ||
| ADD R1 | IF | ID | EX | MEM | WB | ||
| SUB R4 | IF | ID | EX | MEM | WB |
R5(I1→I2)、R1(I2→I3)兩個都能靠 forwarding 解決;總共 \(5+2=7\) 個 clock cycles
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
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%
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(執行結束)
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 的兩種模型
Please draw the Gantt Chart and compute the average waiting time of the execution of the different processes for following scheduling algorithms.
| Process | CPU execution time | Arrival time |
|---|---|---|
| P1 | 5 | 0 |
| P2 | 6 | 2 |
| P3 | 3 | 1 |
| P4 | 7 | 3 |
| P5 | 5 | 5 |
(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)
完成: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
完成: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
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–4 | P1(剩 1) | P3、P2、P4、P1 |
| 4–7 | P3(完成) | P2、P4、P1、P5 |
| 7–11 | P2(剩 2) | P4、P1、P5、P2 |
| 11–15 | P4(剩 3) | P1、P5、P2、P4 |
| 15–16 | P1(完成) | P5、P2、P4 |
| 16–20 | P5(剩 1) | P2、P4、P5 |
| 20–22 | P2(完成) | P4、P5 |
| 22–25 | P4(完成) | P5 |
| 25–26 | P5(完成) | (空) |
完成: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\)
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
同一年的其他科
同一科的其他年度
聯絡我們
想許願補哪一份、對購買有疑問也可以,收到後會盡快回覆。