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

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

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

逐題詳解

110%

[10%] Please describe the steps involved in developing and executing assembly language programs?

寫與組譯

① 編輯(edit):寫 assembly 原始檔 .s

② 組譯(assemble):assembler 把每個 .s 翻成 object file .o(machine code + symbol table + relocation 資訊;pseudo-instruction 在這步展開、label 換成位址)

連結與執行

③ 連結(link):linker 把多個 .o 與 library 合在一起,解決外部 symbol,決定各 segment 位址,輸出 executable

④ 載入(load):OS 的 loader 讀 executable,配置記憶體、把 text/data 複製進去、設好 stack 與參數、把 PC 指到起始位址

⑤ 執行(execute):CPU 開始執行;有錯用 debugger 單步檢查,改原始檔回到 ①

edit → assemble → link → load → execute(debug)

210%

[10%] Please write the MIPS code to compute the mathematic formula \(a\times b+3c-10\).

假設 \(a,b,c\) 放在 $s0, $s1, $s2,結果放 $s3,乘積不超過 32 bit

mult  $s0, $s1        # {Hi,Lo} = a*b
mflo  $t0              # $t0 = a*b
add   $t1, $s2, $s2    # $t1 = 2c
add   $t1, $t1, $s2    # $t1 = 3c
add   $t0, $t0, $t1    # a*b + 3c
addi  $s3, $t0, -10    # a*b + 3c - 10

3c 也可以寫 sll $t1,$s2,1 再 add;addi 的立即值可以是負數,不需要 subi

310%

(a) [5%] If processor A has a higher clock rate than processor B, and processor A also has a higher MIPS rating than processor B, explain whether processor A will always execute faster than processor B.

(b) [5%] Computer A has an overall CPI of 1.5 and can be run at a clock rate of 700 MHz. Computer B has a CPI of 2.0 and can be run at a clock rate of 650 MHz. We have a particular program to run and this program has exactly 120,000 instructions when compiled for computer A. How many instructions would the program need to have when compiled for Computer B if we want the two computers to have exactly the same execution time for this program?

(a) 不一定。\(\text{Time}=\dfrac{IC\times CPI}{f}\),\(\text{MIPS}=\dfrac{f}{CPI\times10^6}\),兩者都沒有算 instruction count \(IC\)

  A、B 的 ISA 不同時,同一個程式的 \(IC\) 不一樣:A 的指令較簡單、\(IC\) 較大,MIPS 高也可能比較慢

  例:A 3 GHz、CPI 1(3000 MIPS)、程式要 \(10^{10}\) 條 → 3.33 s;B 2 GHz、CPI 2(1000 MIPS)、程式要 \(10^{9}\) 條 → 1 s,B 較快

(b) \(T_A=\dfrac{120000\times1.5}{700\times10^6}=\dfrac{180000}{700\times10^6}\) s

  \(T_B=\dfrac{IC_B\times2.0}{650\times10^6}\),令 \(T_A=T_B\)

  \(IC_B=\dfrac{180000\times650}{700\times2.0}=\dfrac{117{,}000{,}000}{1400}\approx83{,}571\)

(a) 不一定 (b) 約 83,571 條

410%

(a) [4%] What are the two characteristics of program memory accesses that caches exploit?

(b) [6%] What are three types of cache misses?

(a) temporal locality:用過的資料不久後會再用(迴圈變數、指令)→ 留在 cache

  spatial locality:用了某位址,附近位址也會用(陣列、循序指令)→ 一次搬一整個 block

(b) compulsory miss(cold/first reference):block 第一次被用,一定 miss

  capacity miss:cache 太小,放不下 working set,被替換掉的 block 之後又要用

  conflict miss(collision):direct-mapped/set-associative 多個 block 映到同一個 set 互相擠掉,但 cache 整體還有空位

(a) temporal locality、spatial locality (b) compulsory、capacity、conflict

510%

[10%] Answer TRUE or FALSE for the following questions.

(a) [2%] A virtual cache access time is always faster than that of a physical cache.

(b) [2%] Both DRAM and SRAM must be refreshed periodically using a dummy read/write operation.

(c) [2%] High associativity in a cache reduces compulsory misses.

(d) [2%] A write-through cache typically requires less bus bandwidth than a write-back cache.

(e) [2%] Memory interleaving is a technique for reducing memory access time through increased bandwidth utilization of the data bus.

(a) virtual cache 用 virtual address 索引,hit 時省去 TLB 轉換,但有 synonym/homonym 問題,要清 cache 或加 PID;"always" 不成立 → F

(b) 只有 DRAM 要 refresh(電容漏電);SRAM 是 flip-flop,通電就保持 → F

(c) compulsory miss 是第一次用就 miss,和 associativity 無關;associativity 減少的是 conflict miss → F

(d) write-through 每次 store 都寫到 memory,bus 流量比 write-back(dirty block 被替換才寫回)大 → F

(e) interleaving 把連續位址分散到多個 bank,可同時存取、讓 data bus 的頻寬用得更滿,降低有效 access time → T

(a) F (b) F (c) F (d) F (e) T

69%

[9%] Answer TRUE or FALSE for the following questions. Please justify your answers.

(a) [3%] Multithreading is deemed as one of the necessary conditions of a deadlock. If the statement is true, please describe why it is a necessary condition. If it is false, please itemize all necessary conditions of a deadlock.

(b) [3%] A transaction is a series of read and write operations upon some data followed by a commit operation. If the series of operations in a transaction cannot be completed, the transaction must be aborted and the operations that did take place must be rolled back. The series of operations in a transaction must be treated as one indivisible operation to ensure the integrity of the data being updated. Is the statement true? If it is true, please specify the name of this requirement. If it is false, please describe why it is wrong.

(c) [3%] A RAID Level 1 organization achieves better performance for 'read' requests than a RAID Level 0 organization (with non-redundant striping of data). Is it true? Justify your answer.

(a) F。multithreading 不是必要條件(多個 process 也會 deadlock)。四個必要條件,要同時成立:

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

  ② hold and wait:拿著資源又在等別的資源

  ③ no preemption:資源不能被強制拿走,只能自己釋放

  ④ circular wait:存在等待環 \(P_0\to P_1\to\cdots\to P_n\to P_0\)

(b) T。這個要求叫 atomicity(ACID 的 A,all or nothing):做完 commit 或全部 rollback,不能只做一半

(c) T。RAID 1 每筆資料有兩份,read 可以從任一份讀:兩個 read 請求可同時送到兩顆碟,或挑 seek 較短、較閒的那一顆,read 的吞吐量最多 2 倍、延遲也較低;

  RAID 0 每個 block 只存在一顆碟,只能去那顆讀

RAID 1 的 write 要寫兩份,write 不比 RAID 0 快;答案是針對 read。若比的是單一大檔案的循序讀取,RAID 0 把資料 stripe 到多顆碟、頻寬最高,RAID 1 不及;本題取「mirror 的任一份都能讀」的角度,答 T

(a) F (b) T,atomicity (c) T

76%

[6%] Select the correct answers for the following questions.

(a) [3%] Which of the following scheduling algorithms could result in starvation: 1. First-come, first-served 2. Shortest job first 3. Round robin 4. Priority

(b) [3%] Which of the following states are valid states in Linux, i.e., the possible STAT indicated in the ps command: 1. Running or runnable 2. Stopped 3. Interruptible sleep 4. Calling 5. Initialize

(a) SJF:短 job 一直來,長 job 永遠排不到;Priority:高優先權一直來,低優先權永遠排不到 → 2、4

  FCFS 依到達順序、Round robin 輪流,都不會 starvation

(b) ps 的 STAT:R(running or runnable)、S(interruptible sleep)、D(uninterruptible sleep)、T(stopped)、Z(zombie)

  1、2、3 是;4 Calling、5 Initialize 不是 STAT 的狀態

(a) 2、4 (b) 1、2、3

810%

[10%] The following implementation of a semaphore is risky. Please describe why it could fail in a multithreaded application. What kind of system supports is required to implement a thread-safe semaphore?

class Semaphore {
    int available;   // in shared memory
  public:
    Semaphore() { available = 1; }
    int acquire() {
        while (available < 1) {
            sleep(1);
        }
        --available;
    }
    int release() { ++available; }
};

為什麼會出錯

① acquire 的「檢查 available < 1」和「--available」是兩個動作,中間可以被切換(race condition)

  available=1,threads T1、T2 都檢查通過(都看到 1),再各自 --available → available=-1,兩個 thread 同時進入 critical section,mutual exclusion 失效

② --available、++available 本身是 load → 加減 → store,不是 atomic;兩個 release 同時跑可能少加一次(lost update)

③ 以 sleep(1) 輪詢(busy waiting),浪費 CPU,也有 1 秒的喚醒延遲

需要的支援

讓「檢查並減一」變成不可分割的一步:

• 硬體 atomic instruction:test-and-set、compare-and-swap、fetch-and-add

• 單處理器可在檢查/修改時關 interrupt;多處理器要靠上面的 atomic instruction(spinlock)

• OS/thread library 提供 mutex/semaphore 系統呼叫,等不到時把 thread 放進 wait queue 並 block,release 時喚醒,不用輪詢

非 atomic 的 check-then-decrement 有 race condition;要靠 atomic 硬體指令(TAS/CAS)加 OS 的 blocking wait queue

910%

[10%] Consider the following set of jobs to be scheduled for execution on a single CPU system.

JobArrivalSizePriority
J10102 (Silver)
J2281 (Gold)
J3333 (Bronze)
J41042 (Silver)
J51213 (Bronze)
J61541 (Gold)

(a) [5%] Draw a Gantt chart showing FCFS scheduling for these jobs.

(b) [5%] Consider FCFS, SJF, and non-preemptive PRIORITY scheduling algorithms. Which of the foregoing scheduling policies provides the lowest waiting time for this set of jobs? What is the average waiting time with this policy?

(a) FCFS(依 arrival 順序 J1、J2、…、J6):

J1J2J3J4J5J60101821252630

(b) waiting time = 開始執行時間 − arrival time(non-preemptive,開始後一路做完)

FCFS:J1 0、J2 10−2=8、J3 18−3=15、J4 21−10=11、J5 25−12=13、J6 26−15=11 → 總和 58,平均 \(58/6\approx9.67\)

SJF(non-preemptive,每次從已到達的挑最短):t=0 只有 J1;t=10 在等的 J2(8)、J3(3)、J4(4) 挑 J3;t=13 剩 J2(8)、J4(4)、J5(1,t=12 到) 挑 J5;t=14 挑 J4(4);t=18 J2(8)、J6(4) 挑 J6,最後 J2

J1J3J5J4J6J20101314182230

J1 0、J3 10−3=7、J5 13−12=1、J4 14−10=4、J6 18−15=3、J2 22−2=20 → 總和 35,平均 \(35/6\approx5.83\)

Priority(non-preemptive,數字小=優先權高;同優先權先到先做):t=10 在等 J2(1)、J3(3)、J4(2) 挑 J2;t=18 有 J6(1) 先;t=22 剩 J4(2);最後 J3(3) 先於 J5(3)

J1J2J6J4J3J50101822262930

J1 0、J2 8、J6 18−15=3、J4 22−10=12、J3 26−3=23、J5 29−12=17 → 總和 63,平均 \(63/6=10.5\)

(b) SJF 最小,average waiting time \(=35/6\approx5.83\) msec(FCFS 9.67、Priority 10.5)

1010%

[10%] A system has four processes and five resources which can be allocated. The current allocation and maximum needs are given below. To ensure it is a safe state, what is the smallest value of \(x\) according to the banker's algorithm? Justify your answer.

ProcessAllocationMaxAvailable
ABCDEABCDEABCDE
P0102112121100x12
P12011022210
P21101021310
P31101011221

先算 Need(Max − Allocation):

Need = Max − AllocABCDE
P011000
P102100
P210300
P300211

Available \(=(0,0,x,1,2)\),只有 C 欄的 \(x\) 未知

\(x=0\):P0 要 A=1、P1 要 B=2、P2 要 A=1,Available A、B 都是 0;P3 要 C=2 > 0 → 沒有人能做,unsafe

\(x=1\):P3 要 C=2 > 1,P1 要 B=2,P0、P2 要 A → 仍沒有人能做,unsafe

\(x=2\):Available \((0,0,2,1,2)\),P3 的 Need \((0,0,2,1,1)\) 滿足 → P3 做完,歸還 \((1,1,0,1,0)\) → \((1,1,2,2,2)\)

  P0 Need \((1,1,0,0,0)\) 可以 → 歸還 \((1,0,2,1,1)\) → \((2,1,4,3,3)\)

  P2 Need \((1,0,3,0,0)\) 可以 → 歸還 \((1,1,0,1,0)\) → \((3,2,4,4,3)\)

  P1 Need \((0,2,1,0,0)\) 可以 → 全部做完,safe sequence \(\langle P_3,P_0,P_2,P_1\rangle\)

\(x=2\)

115%

[5%] Consider a demand-paging system with a paging disk that has an average access and transfer time of 20 milliseconds. Addresses are translated through a page table in main memory, with an access time of 1 microsecond per memory access. Thus, each memory reference through the page table takes two accesses. To improve this time, we have added an associative memory that reduces access time to one memory reference, if the page-table entry is in the associative memory. Assume that 70 percent of the accesses are in the associative memory and that, of the remaining, 10 percent (or 3 percent of the total) cause page faults. What is the effective memory access time?

三種情形:

• 在 associative memory(70%):1 次存取 \(=1\ \mu s\)

• 不在、但 page 在記憶體(\(30\%-3\%=27\%\)):查 page table 1 次+存取 1 次 \(=2\ \mu s\)

• page fault(3%):到 disk 把 page 讀進來 \(20\ \text{ms}=20{,}000\ \mu s\)(相對於它,記憶體存取的 \(\mu s\) 可忽略)

\(\text{EAT}=0.70\times1+0.27\times2+0.03\times20{,}000=0.7+0.54+600=601.24\ \mu s\)

page fault 那項再加上 2 次記憶體存取(20,002 μs):\(0.03\times20{,}002=600.06\),總共 601.30 μs,數字差在小數點以下

EAT \(\approx601.24\ \mu s\approx0.6\) ms

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

國立成功大學 109–115 的詳解收錄在付費版;106–115 全年度完整詳解共 289 頁,逐題推導。

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

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com