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

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

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

逐題詳解

110 分

Consider a program written in high level programming language is executed separately on two processors with different instruction set architectures. Processor A executes 10 billion instructions with clock rate 4GHz and CPI 1.0. Processor B executes 8 billion instructions with the same clock rate but CPI 1.1. (a) Find the MIPS rates of those processors. (4 分) (b) Find the total execution time for each of the processors. (4 分) (c) Compare their performance. (2 分)

(a) MIPS = clock rate ÷ (CPI × \(10^6\))

A:\(\dfrac{4\times10^{9}}{1.0\times10^{6}}=4000\)  B:\(\dfrac{4\times10^{9}}{1.1\times10^{6}}\approx3636\)

A = 4000 MIPS,B ≈ 3636 MIPS

(b) time = IC × CPI ÷ clock rate

A:\(\dfrac{10\times10^{9}\times1.0}{4\times10^{9}}=2.5\) s  B:\(\dfrac{8\times10^{9}\times1.1}{4\times10^{9}}=2.2\) s

A = 2.5 s,B = 2.2 s

(c) 同一支程式,B 的時間短:\(\dfrac{2.5}{2.2}\approx1.14\)

A 的 MIPS 較高但比較慢:指令集不同、同一程式的指令數不同,MIPS 不能拿來比效能

B 比 A 快約 1.14 倍

210 分

Assume a decimal value −1234 is stored in a 32-bit general purpose register. What is the value (in binary or hexadecimal format) stored in the register if it is represented as (a) a signed integer, and (b) a single-precision floating-point number in the IEEE 754 standard. (各 5 分)

\(1234=1024+128+64+16+2=100\,1101\,0010_2=\text{0x4D2}\)

(a) two's complement:\(\text{0x000004D2}\) → 取反 \(\text{0xFFFFFB2D}\) → 加 1

0xFFFFFB2E

(b) \(1234=1.00110100100_2\times2^{10}\)(11 位元,小數點左移 10 位)

sign = 1(負);exponent = \(10+127=137=1000\,1001_2\)

fraction = 0011010010 後面補 0 到 23 位

1 | 10001001 | 00110100100000000000000 → 1100 0100 1001 1010 0100 0000 0000 0000

0xC49A4000

310 分

Consider a computer datapath divides the execution of an instruction into five stages, i.e. instruction fetch, instruction decode, execution, memory access, and write back. The processing times of these stages are 400ps (\(10^{-12}\) seconds), 200ps, 120ps, 350ps, and 200ps, respectively. (a) What is the highest operating frequency if it is a single cycle (not pipelined) datapath? (4 分) (b) A pipeline register adds 20ps delay. What is the highest operating frequency if it is a five-stage pipeline datapath? (4 分) (c) Assume an infinite code sequence is executed, and no hazard is found between instructions. Compare the performances of the single cycle datapath and the pipelined datapath. (2 分)

(a) clock period = 五個 stage 加總 = \(400+200+120+350+200=1270\) ps

\(f=\dfrac1{1270\text{ ps}}\approx787\) MHz

≈ 787 MHz

(b) clock period = 最慢 stage + pipeline register = \(400+20=420\) ps

\(f=\dfrac1{420\text{ ps}}\approx2.38\) GHz

≈ 2.38 GHz

(c) 指令無窮多、沒有 hazard:single cycle 每 1270 ps 完成 1 個,pipeline 每 420 ps 完成 1 個

speedup = \(\dfrac{1270}{420}\approx3.02\)

不到理想的 5 倍:各 stage 時間不平衡(慢的 400 ps 決定 clock)、每級多 20 ps 的 register 延遲;單一指令的 latency 反而變長(\(5\times420=2100\) ps)

pipeline 約為 single cycle 的 3.02 倍

410 分

Consider the code sequence executing in a five-stage (IF, DE, EX, MEM, and WB) pipelined datapath.

lw  $1, 0($6)    # $1 <- MEM[$6]
lw  $2, 1($6)    # $2 <- MEM[$6+1]
add $3, $1, $2   # $3 <- $1 + $2
sub $4, $3, $5   # $4 <- $3 + $5
beq $3, $0, L1   # jump to L1 if $3 == $0
...
L1:

(a) Show all the possible hazards between instructions. (5 分) (b) Describe how the hazards can be resolved in recent computer architectures. (5 分)

(a) 三類 hazard:

① data hazard(RAW):

producer → consumer暫存器相距說明
lw $1 → add$12 條lw 在 MEM 末才有值,add 在 EX 要用
lw $2 → add$21 條load-use hazard
add → sub$31 條add 在 EX 末就有值
add → beq$32 條beq 比較要用 $3

② control hazard:beq 要到 EX(或 ID)才知道有沒有跳,跳不跳決定前,後面的指令已進入 pipeline

③ structural hazard:若指令與資料共用同一個 memory,lw 在 MEM 存取資料時,與另一條指令的 IF 搶同一個 port

$1、$2、$3、$4 都只寫一次,沒有 WAR、WAW;in-order 5-stage pipeline 本來就不會出這兩類

(b) 

data hazard:forwarding(bypassing)——把 EX/MEM、MEM/WB 暫存器的結果直接送回 ALU 輸入,免等寫回。lw → add($1,相距 2)、add → sub($3)、add → beq($3)都靠 forwarding 解決

load-use(lw $2 → add):資料在 MEM 後才有,forwarding 也來不及,要 stall 1 個 cycle(hazard detection unit 插 bubble),之後再 forward;或由編譯器做 instruction scheduling,在 lw 與 add 之間排進不相依的指令(這段程式裡其他指令都依賴 $3,沒有可搬的,只能 stall)

control hazard:branch prediction(static、dynamic 如 1/2-bit predictor、BTB)、把比較提早到 ID、delayed branch;預測錯誤就 flush 已進入的指令

structural hazard:分開的 instruction/data cache(Harvard)、register file 讀寫用不同 port 或時脈前後半拍

510 分

Consider a 128 bytes fully associative mapped cache with 4-word blocks (a word has 32 bits). Its replacement policy is LRU. Suppose the following memory word addresses (in decimal) are accessed in sequence: 40, 41, 42, 400, 43, 400, 60, 61, 62, 63, 64, 800, 40, 41, 42, 800, 43, 44, 60, 61, 62, 120, 121, 122, 123, 168, 169, 41, 42, and 400.

(a) Show whether it is a hit or a miss in the cache for each of the memory accesses. (6 分) (b) What is the miss rate? (4 分)

block = 4 words = 16 bytes;cache = 128 / 16 = 8 個 block;位址是 word address,block 編號 = \(\lfloor\text{addr}/4\rfloor\)

(a) 

#12345678910
addr4041424004340060616263
block1010101001010015151515
H/MMHHMHHMHHH
#11121314151617181920
addr6480040414280043446061
block1620010101020010111515
H/MMMHHHHHMHH
#21222324252627282930
addr621201211221231681694142400
block153030303042421010100
H/MHMHHHMHHHH

用到的 block 只有 10、100、15、16、200、11、30、42 共 8 個,剛好放得下,LRU 從頭到尾沒有踢出任何 block;miss 都是第一次碰到的 compulsory miss(# 1、4、7、11、12、18、22、26)

(b) miss = 8,共 30 次存取

miss rate = \(\dfrac{8}{30}\approx26.7\%\)

65 分

Illustrate the diagram of CPU switch from process A to process B and explain each step in detail.

process P₀operating systemprocess P₁interrupt 或 system callsave state into PCB₀reload state from PCB₁dispatch → 跑 P₁executinginterrupt 或 system callsave state into PCB₁reload state from PCB₀dispatch → 回到 P₀executingidle

① P₀(A)執行中,發生 interrupt(例如 timer 到時)或呼叫 system call,CPU 轉入 kernel mode

② kernel 把 P₀ 目前的 context(PC、各暫存器、PSW、stack pointer、memory-management 資訊)存進 P₀ 的 PCB,P₀ 狀態改成 ready(或 waiting)

③ CPU scheduler 從 ready queue 選出下一個要跑的行程 P₁(B)

④ dispatcher 從 P₁ 的 PCB 把 context 載回 CPU(換 page table、載入暫存器與 PC),切回 user mode,跳到 P₁ 上次停下的地方

⑤ P₁ 開始執行;P₀ 在 PCB 裡等下次被選中,再用同樣的程序換回來

切換期間 CPU 沒有做使用者工作,是純 overhead,時間長短取決於暫存器數量、memory 速度、硬體支援(例如多組 register set)

77 分

(a) What is a thread? (b) Please describe the difference between a tradition single-threaded process and multithreaded process. (c) Please describe and compare the three multithreading models between user threads and kernel thread.

(a) thread 是 CPU 使用的基本單位,有自己的 thread ID、program counter、register set、stack;同一行程的 thread 共用 code、data 與開啟的檔案、signal 等資源

(b) single-threaded process:一個 control flow,整個行程只有一組 PC/暫存器/stack,一處卡住(例如等 I/O)整個行程停住

multithreaded process:多個 control flow 共用同一份 code/data/files,各有自己的 stack 與暫存器

好處:responsiveness(一條 thread 阻塞,其他仍可跑)、resource sharing(共用記憶體不必 IPC)、economy(建立、切換比 process 便宜)、scalability(多核心上平行)

(c) 三種 model:

model對應優點缺點
many-to-one多個 user thread → 1 個 kernel threadthread 管理在 user space,快、便宜一條 thread 呼叫 blocking system call,整個行程都被擋;多核心上不能平行
one-to-one每個 user thread → 1 個 kernel thread併發度高,blocking 不影響其他;多核心可平行建 user thread 就要建 kernel thread,成本高,數量常有上限
many-to-many多個 user thread 多工到 ≤ 它數量的 kernel threaduser thread 數量不受限、又能平行,blocking 時 kernel 可改排別的實作複雜(two-level model 還允許把某 user thread 綁定到一個 kernel thread)
810 分

In order for a process to run on a multi-processor or multi-core system, what are the levels of scheduling (or queuing) it must go through (Start from the first time when a process submitted to O.S.)? Who handles each of the scheduling? Explain your answer in detail.

由行程送進系統開始,依序經過:

① long-term scheduling(job scheduling) job queue → ready queue。決定哪些 job 可以載入記憶體、成為行程,控制 degree of multiprogramming,並搭配 I/O-bound 與 CPU-bound 的比例。由 OS 的 long-term scheduler(job scheduler)負責;執行頻率低(秒、分鐘級)。分時系統常沒有這一層,新行程直接進 ready queue

② medium-term scheduling 記憶體吃緊時,把部分行程 swap out 到磁碟、之後再 swap in,降低多工程度。由 medium-term scheduler(swapper)配合記憶體管理負責

③ short-term scheduling(CPU scheduling) 從 ready queue 挑一個行程配給 CPU,執行頻率高(毫秒級)。由 short-term scheduler(CPU scheduler)挑選,dispatcher 做 context switch 並交出 CPU

多處理器/多核心的差別:ready queue 可以是全部 CPU 共用一個,或每個 core 各一個 queue(現代 OS 多採 per-core);per-core 時需要 load balancing(push/pull migration)使各 core 負載平均,並考慮 processor affinity(盡量讓行程留在同一個 core,保住 cache)。這些由 kernel 的 scheduler 處理(每個 core 各跑一份 CPU scheduler 或 symmetric multiprocessing)

此外行程等 I/O 時進入各 device 的 device queue,由 device 的 I/O scheduler(device driver、disk scheduler)決定服務順序,完成後回到 ready queue

流程:job pool →(long-term)→ ready queue →(short-term)→ CPU →(I/O、時間到、wait、fork)→ 回 ready/device/waiting queue;記憶體不足時(medium-term)swap out ↔ swap in

94 分

Why we use dynamic loading and what are the advantages of dynamic loading?

dynamic loading:副程式在被呼叫時才載入記憶體;所有 routine 以 relocatable 格式放在磁碟,主程式先載入執行,呼叫某 routine 時先檢查是否已在記憶體,沒有就由 relocatable linking loader 載入並更新位址表,再把控制權交給它

用它的原因:程式可能很大,但同一次執行用到的只是一部分

優點:

① 沒用到的 routine 永遠不載入,節省記憶體,記憶體使用率更高

② 特別適合大量、很少用到的程式碼(錯誤處理、例外情形)

③ 不需要作業系統特別支援,由程式設計師設計即可(OS 可提供 library 協助)

107 分

What are the three classical problems of synchronization? Describe one of them and give a solution to the problem (can be pseudo-code).

① bounded-buffer(producer–consumer)problem ② readers–writers problem ③ dining-philosophers problem

選 bounded-buffer:有 \(n\) 格的 buffer,producer 放資料、consumer 取資料;buffer 滿時 producer 要等,空時 consumer 要等,且對 buffer 的存取要互斥

semaphore:mutex = 1(保護 buffer)、empty = n(空格數)、full = 0(滿格數)

producer:                    consumer:
do {                         do {
  produce item               wait(full);
  wait(empty);               wait(mutex);
  wait(mutex);               remove item from buffer
  add item to buffer         signal(mutex);
  signal(mutex);             signal(empty);
  signal(full);              consume item
} while (true);            } while (true);

wait(empty) 要放在 wait(mutex) 之前;順序相反時,buffer 滿了 producer 拿著 mutex 睡下去,consumer 進不來,deadlock

116 分

Explain what is busy waiting and how to overcome in terms of modify wait() and signal() semaphore operations.

busy waiting:一個行程在 critical section 外不停地迴圈檢查條件(例如 while (S <= 0);),等別人把條件改成真;等的過程一直佔用 CPU,這些 CPU 時間被白白浪費(這種 semaphore 稱 spinlock)

改法:semaphore 除了整數值,再加一條等待佇列(waiting list),wait 時沒資源就讓行程去睡,signal 時再叫醒

typedef struct { int value; struct process *list; } semaphore;

wait(S) {                         signal(S) {
  S.value--;                        S.value++;
  if (S.value < 0) {                if (S.value <= 0) {
    add this process to S.list;       remove a process P from S.list;
    block();                          wakeup(P);
  }                                 }
}                                 }

block():把呼叫的行程從 running 移到 waiting queue,CPU 去排別人;wakeup(P):把 P 從 waiting 移到 ready queue

S.value 為負時,絕對值就是正在等的行程數

wait()、signal() 本身仍要原子執行(單處理器關 interrupt、多處理器用短暫的 spinlock),但只佔一小段程式碼,不像 critical section 可能很長

124 分

What are the pros and cons of choosing a small page size? What are the pros and cons of selection a large page size?

優點缺點
small pageinternal fragmentation 少(平均每個行程浪費半頁);對 locality 的刻畫更細,只載入真正用到的部分,常駐記憶體少page table 很大(entry 多,佔記憶體);TLB 涵蓋範圍(TLB reach)小、miss 多;磁碟 I/O 每頁都有 seek+rotation,傳輸效率差、page fault 次數多
large pagepage table 小;TLB reach 大、miss 少;磁碟一次傳較多資料,seek/latency 的成本分攤,I/O 較有效率,page fault 較少internal fragmentation 多;常把用不到的資料一起載入,浪費記憶體與傳輸;locality 的解析度較粗
137 分

Consider a file system where a file can be deleted and its disk space reclaimed while links to that file still exist. What problems may occur if a new file is created in the same storage area or with the same absolute path name? How can these problems be avoided?

會發生的問題:舊的 link 變成 dangling pointer,仍指向原來的位置

① 新檔案蓋在同一塊磁碟空間(同一個 inode/block):透過舊 link 去存取,會讀到、甚至寫壞另一個不相干的新檔案,檔案內容被誤用或破壞

② 新檔案取了相同的絕對路徑名:用路徑名記錄的 link(symbolic link)不會報錯,會悄悄解析到新檔案,使用者以為在存取原檔,實際拿到別人的資料(資料錯亂、也可能繞過原本的存取權限,造成安全問題)

避免方法:

① reference count(UNIX 的 hard link):檔案記錄有幾個 link 指向它,刪除時只把 count 減 1,歸零才真正回收空間——只要還有 link,空間就不會被別人拿去用

② 刪檔時一併處理所有 link:檔案維護一份 link 清單(back pointers),刪除時逐一移除;缺點是清單可能很長、空間與維護成本高

③ 保留 link,存取時檢查:link 與檔案配對儲存 unique file ID/版本號(generation number),存取時比對不符就視為失效,不會落到新檔案(UNIX 的 symbolic link 只有路徑名,需靠使用者自己清除 dangling link)

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

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

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

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com