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

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

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

逐題詳解

110%

PART I: COMPUTER ARCHITECTURE

1. (10 pts) Wearable computing devices are very popular nowadays. One news articles says that an airline company is testing a new system where airline workers and gate agents use wearable devices such as Google Glass to streamline the check-in process for passengers. As illustrated in the picture below, the gate agent wears Google Glass. The Google Glass may recognize the person in front of her and tell her about this person, even without looking at his passport.

(原題附圖:gate agent 戴 Google Glass 的照片,與「How Google Glass works」構造圖,標有 Battery、CPU (incl. GPS)、Camera (video / photo)、Speakers (phone)、Micro (phone)、Prism (visual overlay))

Assume that you are going to design the system. Please answer the following questions:

a. [5%] Draw the diagram for the computer architecture of Google Glass. The diagram should include the components in the picture and perhaps some other components not showing in the picture. The diagram should illustrate the connections between the components. Discuss the requirements of the Google Glass in this specific application and present your choice of the components, including CPU, memory, storage, etc.

b. [5%] Do you think that Google Glass can accomplish the job alone? Actually, Google Glass needs to work with a server to recognize the person and retrieve information. Please draw the diagram for the system infrastructure. The diagram should include the hardware and software components involved in the operation. Describe how the system works.

a. 需求(check-in 人臉辨識):

① 低功耗、輕:戴在頭上,電池小(約 600–800 mAh)、散熱只能靠被動,SoC 整體只能 1–2 W 等級 ② 低延遲:看到人到顯示資訊要在 1–2 秒內 ③ 要連網:資料量(人臉資料庫、旅客資料)放不進眼鏡,要靠 Wi-Fi/手機的 LTE ④ 感測:camera、麥克風、GPS/IMU,輸出:prism 顯示、喇叭

Camera(video / photo)MicrophoneGPS / IMU(感測器)Touchpad(輸入)SoC(低功耗、單晶片)CPU雙核 ARM 約 1–1.5 GHz(OS、通訊、控制)GPU/ISP/codec(影像前處理、H.264)DSP/audio(麥克風、喇叭)Memory controllerDisplay controller(prism 投影)Wi-Fi/Bluetooth(LTE 靠手機)DRAMLPDDR 1–2 GBFlash16–32 GBDisplay(prism)Speaker(骨傳導)Server(人臉辨識、旅客資料)Wi-Fi/LTEBattery約 600–800 mAhPMIC:DVFS、powergating、低功耗 standby

元件選擇:CPU 雙核 ARM 低功耗核心(約 1–1.5 GHz,要跑 OS、通訊、控制,不做重運算);GPU/ISP/video codec 在同一顆 SoC 內,camera 影像直接做前處理與 H.264 編碼;DRAM LPDDR 1–2 GB(放 frame buffer 與程式);Flash 16–32 GB(OS、app,少量暫存);Wi-Fi/Bluetooth 內建,LTE 透過手機;PMIC 做 DVFS、power gating,沒事就 standby

b. Glass 自己做不來:旅客臉部特徵與航班資料庫很大(上百萬筆),辨識需要 GPU 等級的運算、也要集中管理與更新,電池與散熱都撐不住;所以 Glass 只負責擷取與前處理,辨識交給 server

Google Glass(擷取影像、偵測臉、壓縮)TLSInternet(Wi-Fi/LTE)API gatewayload balancer(認證、audit)人臉辨識 service(GPU 伺服器)臉部特徵DB旅客 service(航班、護照)航空公司DBLog/audit/權限身分+登機資訊(回傳最少必要資訊)→ 顯示在 Glass

運作:① Glass 擷取影像,在本機偵測人臉、裁切、壓縮(只送臉部或特徵,降低傳輸量)② 經 TLS 加密,由 Wi-Fi/LTE 送到 server 的 API gateway(認證、load balancer、audit log)③ 人臉辨識 service 在特徵 DB 找最像的 → 旅客 service 用身分查航班、護照等資料 ④ 只把最少必要資訊(姓名、航班、座位、是否需要注意)回傳,顯示在 prism、必要時喇叭念出 ⑤ 每個 request 有 timeout,失敗重試;server 端 replication 保持可用

a. 低功耗雙核 ARM SoC(含 GPU/ISP/codec)+LPDDR+Flash+Wi-Fi,電源管理做 DVFS; b. 不能獨立完成,Glass 做擷取與前處理,server 做人臉辨識與資料查詢

210%

2. (10 pts)以下摘自 Engaget 中文版(2014-02-11):

在這一代發表的 Kaveri APU,AMD 又再更進了一步,提出了「異質系統架構(Heterogeneous System Architecture,HSA)」,讓 CPU 和 GPU 可以共同分享處理器上的記憶體,並且模糊了 CPU 和 GPU 之間的分界。一直以來 AMD 就強調使用 GPU 來協助 CPU 做運算,而非只是單純的進行顯示工作而已,而在 Kaveri 上,GPU 的地位已經提升和 CPU 同等的地位,所以 AMD 把它也稱做處理器的「運算核心」。舉例來說,最高階的 Kaveri 處理器 A10-7850K 就有 4 個 CPU、8 個 GPU,成為成為「12 運算核心」的處理器。不過要能利用到 HSA 的最佳效能,還有待軟體方面跟上就是了。

下圖來自 AMD 的網站,點出 HSA 與傳統系統架構的不同處:

APU(NON-HSA)CPU MEMORYGPU MEMORYCPU(S)CPU(S)CPU(S)CPU(S)GPU(S) ×6APU WITH HSAhUMA(共享記憶體)CPU(S)CPU(S)CPU(S)CPU(S)GPU(S) ×8

請回答以下問題:

a. [2%] 在傳統的 NON-HSA 系統架構中,使用者如何讓 GPU 幫忙 CPU 進行運算工作?(HINT: 處理機之間如何交換資料,管道為何?)

b. [2%] HSA 所提出的異質處理機架構設計,有什麼優點?

c. [3%] HSA 讓 CPU 和 GPU 共同分享處理器上的記憶體,在處理機架構上必須有什麼改變?改變之後的優點為何?請就效能、成本、技術、複雜度等面相討論之。

d. [3%]文中提到『要能利用到 HSA 的最佳效能,還有待軟體方面跟上』。其中的一項挑戰,就是讓為 HSA 編寫的程式具備高度的可攜性(portability),讓一個程式可在不同 ISA 的 GPU 上執行。因此 HSA 所提出一套稱為 HSAIL(HSA Intermediate Language)的組合語言,定義出新的 ISA,建議開發者先將程式編譯成 HSAIL 後再提供給使用者。使用者在執行程式時,再利用 Just-in-Time (JIT) Optimization 的技術,對於可以解決此類問題。請解釋 JIT Optimization 技術,並且討論 HSA 這項技術的優缺點。

a. NON-HSA:CPU 與 GPU 各有自己的記憶體(圖:CPU MEMORY、GPU MEMORY)。管道是 PCIe 匯流排上的資料複製(DMA):

① CPU 程式在 CPU memory 準備資料 ② 透過 driver 把資料複製到 GPU memory ③ CPU 送出 kernel 命令(command queue)、GPU 計算 ④ 用 interrupt/polling 等 GPU 做完 ⑤ 把結果複製回 CPU memory

用 driver 把資料經 PCIe 複製進 GPU memory、GPU 算完再複製回來(兩邊 memory 分開,靠 copy 交換資料)

b. CPU 與 GPU 共用同一個(virtual)記憶體位址空間:不用複製資料、可以直接傳 pointer;少了 copy 的延遲與 bandwidth 與 driver 開銷,兩邊按工作特性分工(循序給 CPU、資料平行給 GPU),省電,寫程式簡單

共享記憶體、免複製、延遲低、省電、程式簡單

c. 架構上要改:① 統一的位址空間:GPU 要能認 CPU 的 virtual address(共用 page table、有 IOMMU/TLB,GPU 也能處理 page fault) ② cache coherence:CPU、GPU 的 cache 對同一份資料要一致(硬體 coherent 協定) ③ 共用 memory controller,記憶體頻寬要夠兩邊用 ④ 一致的 memory consistency model 與 atomic 操作,讓兩邊能同步

優點與代價:效能 — 省 copy,延遲與 bandwidth 都好,但 coherence traffic 會吃掉一部分頻寬 成本 — 只用一套 memory,系統成本低;晶片面積加了 coherence 與 IOMMU 技術 — 要解決位址轉換與 coherence 複雜度 — 硬體設計與驗證變複雜,軟體 runtime 變簡單

統一 virtual address+cache coherence+共用 memory controller;換到免複製與簡單的程式模型,代價是硬體複雜度與面積

d. JIT:程式先編成與 ISA 無關的 HSAIL;執行時 runtime(finalizer)才把 HSAIL 編譯成這顆 GPU 的機器碼,編譯時已知道實際的 GPU 型號與資源,可以針對它做最佳化(register 配置、unroll、指令選擇)

優點:同一份程式可在不同 ISA 的 GPU 上跑(portability),不用為每種 GPU 重編;runtime 才最佳化,能用到新硬體的特性

缺點:第一次執行有編譯延遲(startup);runtime 要多一層 finalizer,佔記憶體;JIT 的最佳化時間有限,效能可能不如針對該 GPU 離線調過的程式;除錯與重現結果比較困難

JIT=執行時才把 HSAIL 編成目標 GPU 的機器碼;優點 portability、可針對硬體最佳化,缺點 startup 延遲、多一層 runtime、不一定最佳

35%

3. (5 pts) MediaTek, a Taiwanese company which develops SoC (System-on-Chip) solutions for smartphones, is also interested in HSA (See Question 2). Since many users play video games on their smartphones, GPU is very important to the design of smartphone chips. Assume that you are helping MediaTek in designing processor chips for the smartphones of Year 2020.

a. [3%] You are told that the bandwidth of wide-area wireless network will be increased by 100 times, from today's 10mbps to 1Gbps. How would that affect your design? Draw the architecture for your chip design, including the specifications of major components, and explain your design.

b. [2%] Would you adopt HSA for the design? Why?

a. 1 Gbps = 125 MB/s 的資料持續進出,影響:

① modem 要整合進 SoC,並用 DMA 把封包直接搬進記憶體,CPU 不碰每個 byte ② 加解密、TCP/IP checksum 這些處理用硬體加速(crypto engine),否則 CPU 被封包處理吃光 ③ 網路快了,影片、遊戲串流(cloud gaming)變常見 → video codec(H.265 4K)、display 路徑要硬體化 ④ 125 MB/s 對 LPDDR(≥ 25 GB/s)只占不到 1%,記憶體頻寬不是瓶頸;真正的問題是無線電與資料處理的耗電,要靠 race-to-idle:快速處理完就進低功耗

Big CPU 叢集4 核 約 2.5 GHzLITTLE CPU 叢集4 核 約 1.5 GHzGPU(HSA)約 1 TFLOPSVideo codecH.265 4K60Coherent interconnect(共享 virtual address、cache coherent,HSA)1 Gbps modem(整合)Crypto/封包DMA 加速ISP/DSP感測器 hubDisplay/UFS 控制器Memory controllerLPDDR ≥ 25 GB/sSoC 2020:5 nm 級製程、整合 modem

規格是 2020 年的估計:CPU 8 核 big.LITTLE、GPU 約 1 TFLOPS 等級、LPDDR 約 25 GB/s;GPU 仍保留,因為雲端遊戲有網路延遲,本機遊戲還是要靠自己畫

modem 整合+DMA+crypto 加速+硬體 codec;記憶體頻寬夠用,重點在 1 Gbps 傳輸的功耗

b. 採用。手機 SoC 本來就是 CPU、GPU 共用一條 LPDDR,改成 HSA(共享 virtual address、cache coherent)可以省掉遊戲裡 texture/frame buffer 在 CPU 與 GPU 之間的複製:CPU 跑遊戲邏輯、GPU 算畫面與物理,用同一份資料

省下 copy 的時間與能量,也省記憶體;代價是 coherence 硬體的面積與功耗,但複製一大塊資料的能量通常更高

YES,共享 frame/texture 省 copy 與能量,符合手機共用記憶體的現況

44%

4. (4 pts) Use an example to support your YES or NO answer. Note that you need to first write down "YES" or NO" and then give an example to support YES or NO. Both answering-YES-or-NO and an example are required. If you just give examples without writing down "YES" or "NO", you receive no point. If you just write down YES or NO, you also receive no point.

a. [2 pts] Looping (that is, a loop code) is one reason to cause spatial locality. Answer YES or NO and then give an code example to illustrate your answer.

b. [2 pts] Two instructions with data dependencies will cause data hazard in pipelining execution. Answer YES or NO and then illustrate your answer with an example.

a. YES

for (i = 0; i < N; i++)
    sum += a[i];

loop 一圈一圈走過 \(a[0],a[1],\ldots\) 相鄰的位址 → 同一個 cache block 的資料接著被用到,spatial locality;loop body 的指令也是連續位址

loop 本身帶來的是 temporal locality(同樣的指令與變數反覆用);spatial locality 要靠 loop 裡存取相鄰位址(陣列走訪)。迴圈若跳很大的 stride(如 a[i*1024])就沒有 spatial locality,所以是「其中一個原因」

YES (例:for 迴圈走訪陣列 a[i])

b. NO

ADD R1, R2, R3
ADD R4, R1, R5      # RAW:用到前一條的 R1

有 data dependency(RAW),但有 forwarding 時,ALU 結果直接從 EX/MEM register 送給下一條的 EX,pipeline 不用 stall → 沒有 hazard;兩條指令隔得夠遠(5 級 pipeline 隔 3 條以上,WB 先寫、ID 後讀)也不會出事

load-use(LW R1,0(R2) 緊接 ADD R4,R1,R5)就算有 forwarding 也要 stall 1 cycle,所以 dependency 不一定、但有時會造成 hazard

NO (例:ADD R1…;ADD R4,R1,… 有 forwarding 不必 stall)

58%

5. (8 pts) Explain why true or why false. Note that you need to first write down "TRUE" or 'FALSE" and then explain why. Both answering-true-or-false and explanation are required. If you just explain without writing down "TRUE" or "FALSE", you receive no point. If you just answer true or false, you also receive no point.

a. [2 pts] The recent iPhone 5S uses the 64-bit Apple A7 processor which has higher benchmark numbers (in terms of GeekBench and Dhrystone) than other recent 32-bit processors such as Qualcomm's Snapdragon 400. A reason is because most programs run much faster with 64-bit instructions (vs. only 32-bit instructions). If the previous sentence is true, write down TRUE and then explain why true. Otherwise, write down FALSE and then explain why false.

b. [2 pts] If two instructions within a basic block do not have data dependencies on each other, we want the hardware scheduler to be able to reorder the two if reordering results in higher performance. But today's x86 hardware will not reorder the instructions if it detects that any of the two instructions might cause an exception. If the previous sentence is true, write down TRUE and then explain why true. Otherwise, write down FALSE and then explain why false.

c. [2 pts] Multicores such as 4-core MTK6589 and 8-core MTK6592 from Mediatek have become popular in mobile hardware. It is because most existing applications on your phone will benefit from the additional processor cores. If the previous sentence is true, write down TRUE and then explain why true. Otherwise, write down FALSE and then explain why false.

d. [2 pts] Single-issue machines will not benefit from software-pipelining or global instruction scheduling. Answer TRUE or FALSE and explain your answer.

a. FALSE。64-bit 的好處是 address space 大(可用超過 4 GB)、整數運算寬度 64-bit;大多數程式用不到,不會「快很多」。A7 分數高主要來自 microarchitecture(寬的 superscalar/out-of-order、較多 register、cache 設計),不是因為 64-bit 指令本身

b. FALSE。現代 x86 是 out-of-order 執行:不管指令會不會產生 exception 都照樣亂序執行,用 reorder buffer 讓結果依原順序 commit,exception 在 commit 時才處理(precise exception),所以不必因為「可能 exception」就不 reorder

c. FALSE。多核心能加速的只有「有平行化、能同時有多個 runnable thread」的程式;大多數 app 主要是循序或只有少數 thread,Amdahl's law 限制了加速。手機做多核心是因為多工同時跑多個 process、以及用多顆較慢的核心比單顆高頻核心省電

d. FALSE。single-issue 的 pipeline 也有 load-use、長延遲運算(乘除、浮點)、branch delay 造成的 stall;software pipelining 與 global scheduling 把不同 iteration、不同 basic block 的指令交錯,填掉這些空檔,所以仍然受益

a. FALSE b. FALSE c. FALSE d. FALSE

62%

6. (2 pts) Assuming we are going to fetch some data in the memory hierarchy to CPU for computation, sort the following elements of the memory hierarchy in order of increasing access time (in other words, start from the fastest.) (a) Solid State Disk (b) L1 Cache (c) L2 Cache (d) Network (e) Main Memory (f) CPU Registers (g) Hard Disk

register 不到 1 ns;L1 約 1 ns;L2 約 3–10 ns;DRAM 約 50–100 ns;SSD 約 0.1 ms;HDD 約 5–10 ms(seek+rotation);network(跨網路取資料)至少要網路來回延遲,廣域網路 ≥ 10 ms,排在最後

區域網路上 server 的資料可能比 HDD 快(約 0.5 ms 的 RTT),這裡照一般的 hierarchy 圖把 network 放最底層

(f) → (b) → (c) → (e) → (a) → (g) → (d)

74%

7. (4 pts) Given an application that runs for 100 seconds, and 20 out of those 100 seconds must run sequentially:

a. [1 pt.] If we run the application on the recent 4-core MTK6589, what's the maximum speedup we can expect?

b. [1 pt.] If we run the application on the recent 8-core MTK6592, what's the maximum speedup we can expect?

Next, given an application that runs for 100 seconds, and 60 out of those 100 seconds must run sequentially:

c. [1 pt.] If we run the application on the recent 4-core MTK6589, what's the maximum speedup we can expect?

d. [1 pt.] If we run the application on the recent 8-core MTK6592, what's the maximum speedup we can expect?

Amdahl:\(S(N)=\dfrac{100}{T_{seq}+(100-T_{seq})/N}\)

a. \(T_{seq}=20\),\(N=4\):\(\dfrac{100}{20+80/4}=\dfrac{100}{40}=2.5\)

b. \(N=8\):\(\dfrac{100}{20+80/8}=\dfrac{100}{30}=\dfrac{10}3\approx3.33\)

c. \(T_{seq}=60\),\(N=4\):\(\dfrac{100}{60+40/4}=\dfrac{100}{70}=\dfrac{10}7\approx1.43\)

d. \(N=8\):\(\dfrac{100}{60+40/8}=\dfrac{100}{65}=\dfrac{20}{13}\approx1.54\)

a. \(2.5\) b. \(10/3\approx3.33\) c. \(10/7\approx1.43\) d. \(20/13\approx1.54\)

81%

8. (1 pt) Other than RAW data hazards, what are other data hazard types?

WAR(anti-dependence):後面的指令寫某暫存器,前面的指令還沒讀

WAW(output dependence):兩條指令寫同一個暫存器,寫入順序要保持

WAR、WAW

96%

9. (6 pts) Instruction scheduling refers to re-ordering of instructions to avoid stalls due to control or data hazards without changing the program semantics. The goal is to improve performance without affecting program correctness. Following is our machine model:

Each machine cycle can execute 2 operations:

A. one ALU operation or branch operation Op dst,src1,src2 executes in 1 clock

B. one load (LD) or store (ST) operation:LD dst, addr result is available in 2 clocks(Note that load is pipelined: can issue LD next clock);ST src, addr executes in 1 clock cycle

Below are 5 instructions: i1 to i5. The figure includes all but one important dependency edges. That is, one edge is intentionally left out. Please find the missing edge before answering the following questions.

指令用到的 unit
i1LD R2 <- 0(R1)mem
i2ST 4(R1) <- R2mem
i3LD R3 <- 8(R1)mem
i4ADD R3 <- R3,R4alu
i5ADD R3 <- R3,R2alu

(原題附圖:已畫出的 dependency edge 有 i3→i4、i4→i5(標 1)、i1→i5(標 2))

a. [0.5 pt.] Schedule the 5 instructions above in order to finish the 5 instructions in the shortest possible time. Then, answer: What's the minimal number of cycles required to execute these 5 instructions?

b. [0.5 pt.] In your schedule above, if you answer "n" cycles to the question above, number your cycles as cycle_1, cycle_2, …, cycle_n. In your schedule, which cycle is "i1" being executed?

c.–f. [0.5 pt. each] In your schedule, which cycle is "i2" / "i3" / "i4" / "i5" being executed?

g. [1 pt] List all the data hazard types between i1 and i2.

h. [1 pt] List all the data hazard types between i3 and i4.

i. [1 pt] List all the data hazard types between i4 and i5.

缺的 edge:i1 寫 R2、i2(ST)讀 R2 → RAW,LD 的 latency 2 → i1→i2(2)

i1i2i3i4i52221

(i2 存到 4(R1),i1、i3 讀 0(R1)、8(R1),位址不同,i2 與 i3 之間沒有相依)

a. 每個 cycle 一個 mem op+一個 ALU op。最長相依鏈 \(i_3\to i_4\to i_5\):\(i_3\)(LD)→ 2 clocks 後 \(i_4\) → 1 clock 後 \(i_5\)

\(i_3\) 最早 cycle 1 → \(i_4\) 最早 cycle 3 → \(i_5\) 最早 cycle 4 → 至少 4 cycles

\(i_3\) 先發(cycle 1),\(i_1\) 就排 cycle 2:R2 在 cycle 4 可用,剛好給 \(i_5\) 與 \(i_2\)

cycleALU slotmem slot
1—i3:LD R3,8(R1)
2—i1:LD R2,0(R1)
3i4:ADD R3,R3,R4—
4i5:ADD R3,R3,R2i2:ST 4(R1),R2

若先排 \(i_1\)(cycle 1)再排 \(i_3\)(cycle 2),\(i_5\) 要到 cycle 5,共 5 cycles,不是最短。4 是下界(\(i_3\) 最早 cycle 1),且上面是唯一的 4-cycle 排法

a. 4 cycles b. \(i_1\):cycle 2

c. \(i_2\):cycle 4  d. \(i_3\):cycle 1  e. \(i_4\):cycle 3  f. \(i_5\):cycle 4

c. 4 d. 1 e. 3 f. 4

g. \(i_1\):LD 寫 R2;\(i_2\):ST 讀 R2 → RAW

h. \(i_3\) 寫 R3、\(i_4\) 讀 R3 → RAW;兩者都寫 R3 → WAW;\(i_3\) 沒有讀 R3,沒有 WAR

i. \(i_4\) 寫 R3、\(i_5\) 讀 R3 → RAW;\(i_4\) 讀 R3、\(i_5\) 寫 R3 → WAR;兩者都寫 R3 → WAW

g. RAW h. RAW、WAW i. RAW、WAR、WAW

1015%

PART II – OPERATING SYSTEMS

NOTE that in the exam, it is intended to provide redundant information or miss certain assumption to disguise you. Please make your own assumption if necessary to answer the questions.

In 2014, Taiwan Highway Commission officially discontinue the highway toll ticket system in favor of an electronic toll collection system, called ETC for short. The service company develops several services to allow the users to query historical toll charge, the balance of their account if there is any, and the estimated charge via mobile applications and web services. We will go through the exercise of software design to construct a highly available and responsive similar system via proper system services and operating system configuration.

The client server model shown below is the general application model to build such services. Mobile applications or web browsers are the user interfaces of the services. To serve the queries, a three-tier server model is adopted. In the model, there are a web server, application servers, and storage servers. The web server accepts the requests from mobile applications or web browsers, formats the query results in proper ways, and returns the formatted results. The applications conduct the computation logic, query the database server for required data, and compute the results. The storage servers store the user data, vehicle and travel information, and charge to the vehicle.

(原題附圖:Clients(Web Browsers、Mobile Applications)→ Internet → Web Server → 多台 Application Server ↔ 多台 Storage Server)

10. (15 pts) On the client side, the (mobile or web) application issues the requests, receives the requests from the server and presents the results to the user. Please answer the following questions.

a. (5 pts) The application may use either blocking or nonblocking calls to send the requests. Please describe these two types of function calls including process behavior, data transmission, and storage access of both caller and callee processes.

b. (5 pts) Suppose blocking functions are used to issue requests. When the servers are too busy to process the requests, the application will be stalled. Signals are usually used to stop the request. Please describe how to stop the request by signals.

c. (5 pts) Please describe the steps of processing a signal to a process. The description should include process behavior in user space and kernel space.

a. blocking call(例:read/recv 預設):

caller:進入 kernel 後狀態從 running 變 waiting(blocked),放進該 I/O 的 wait queue,CPU 讓給別的 process;操作完成才被喚醒、回到 ready queue,回傳結果

callee(server/device driver):照常處理請求(向 storage 讀、計算),資料先到 kernel buffer/socket buffer,再由 kernel 複製到 caller 的 user buffer;caller 在這段時間沒有進度

nonblocking call(O_NONBLOCK、send/recv 帶 NONBLOCK 旗標,或 asynchronous I/O):

caller:呼叫立刻回傳(資料已備妥就回傳資料,否則回 EAGAIN/EWOULDBLOCK 或只代表「已送出」),狀態維持 running,可以做別的事,之後用輪詢、select/poll/epoll 或 completion 通知再取結果

callee:獨立處理,資料到 kernel buffer 後,等 caller 下一次呼叫(或 async 完成時)才複製進 caller 的 buffer;async 時 caller 的 buffer 在完成前必須保持有效

blocking:caller 睡到完成、資料複製到 buffer 才醒;nonblocking:立即回傳、caller 繼續跑,用輪詢/事件取得結果

b. server 太忙時,在 blocking 呼叫(recv 等)之前先設定逾時:alarm(t)/setitimer,或由另一個 thread、使用者按「取消」對該 process 送 signal(kill/pthread_kill)

signal 到達時,kernel 把睡在 wait queue 的 process 叫醒、執行 handler;被中斷的 system call 回傳 -1、errno=EINTR(handler 要不加 SA_RESTART,否則會自動重啟又睡回去)

handler 設一個 cancel flag;程式看到 EINTR+flag 就放棄這個 request,關閉 socket、釋放 buffer,向使用者顯示逾時

設逾時或由別處送 signal → kernel 喚醒 blocked 的 caller、system call 回 EINTR → 程式檢查 flag、關閉連線、結束這筆 request

c. ① 產生:kill()、timer、exception(SIGSEGV)或裝置事件,kernel 在目標 process 的 pending signal 集合設 bit(若被 mask 就先留在 pending)

② 遞送:目標 process 被排到 CPU、要從 kernel mode 回 user mode 之前,kernel 檢查有沒有「pending 且沒被 mask」的 signal

③ 預設動作(terminate、ignore、stop、continue)由 kernel 直接做;有註冊 handler 的,kernel 把 user context(PC、register、signal mask)存到 user stack 上的 signal frame,設定 PC 指向 handler、返回位址指向 sigreturn 的 trampoline,然後回 user mode

④ user space:process 執行 handler(遞送中的同一種 signal 預設被 mask)

⑤ handler 返回 → sigreturn system call 進 kernel,恢復原本的 context 與 mask → 回 user mode,從被打斷的地方繼續(被中斷的 system call 依設定回 EINTR 或重啟)

pending → 回 user mode 前檢查 → 存 context 建 signal frame → user space 跑 handler → sigreturn 還原 → 繼續執行

1110%

11. (10 pts) All these servers are supposed to accept simultaneous requests, handled by either multiple processes or multiple threads in servers. Please answer the following questions.

a. (5 pts) When the services are implemented as multiple process applications. Please describe the difference between fork() and vfork() defined in POSIX standard (or IEEE Std 1003) to create child processes and handle incoming requests. You should discuss their memory layout, scheduling policy, and data sharing between parent and child processes.

b. (5 pts) There are several thread models supported by modern operating systems. Please define the one-to-one and many-to-many thread model.

a.

fork()vfork()
memory layoutchild 得到 parent address space 的副本(text、data、heap、stack),用 copy-on-write,各自獨立child 暫時借用 parent 的 address space,不複製 page table;child 改 stack、heap 都改到 parent 的
schedulingparent、child 是兩個獨立可執行的 process,誰先跑由 scheduler 決定parent 被 suspend,直到 child 呼叫 exec 或 _exit 才繼續
data sharing資料各一份,互相看不到修改;只共享開著的 file descriptor(與 shared memory)共用同一份記憶體;POSIX 規定 child 只能 exec 或 _exit,不能改資料、不能 return

handle request:server 對每個 request 用 fork() 產生 child 處理,parent 馬上回去 accept,下一個 request 同時進來也能平行處理;vfork() 讓 parent 停下來等 child,沒有 concurrency,只適合 child 馬上 exec 另一個程式的情況

vfork 在 POSIX.1-2008 已被移除(之前標為 obsolescent),建議用 posix_spawn;題目問的是 IEEE Std 1003 當時(含 vfork)的定義

fork:複製(COW)、父子並行、資料不共享;vfork:借用父記憶體、父暫停、共用資料,child 只能 exec/_exit

b. one-to-one:每個 user thread 對應一個 kernel thread(Linux、Windows)。優點:thread 真的能在多核心平行跑,一個 thread 呼叫 blocking system call 不會擋住其他 thread;缺點:建立 thread 就要建 kernel thread,有 kernel 資源成本,數量受限

many-to-many:\(m\) 個 user thread 多工到 \(n\)(\(n\le m\))個 kernel thread 上,由 user-level thread library 排程。優點:user thread 數量可以很大,kernel thread 仍可平行,blocking 時 kernel 能再給別的 kernel thread;缺點:library 與 kernel 要協作(如 scheduler activations),實作複雜

one-to-one:user thread ↔ kernel thread 一對一;many-to-many:多個 user thread 多工在較少的 kernel thread 上

1215%

12. (15 pts) There are over 300 toll gates (收費感應門架) on the highways to identify passing vehicles by detect an etag on each vehicle to calculate toll charge by mileage. The eTag is a passive RFID (no power) and relies on radio sensors on the gates to collect the information on it using larger power than previously used OBU (on-board unit, powered) module. There are also cameras on the gates to record video of passing vehicles to do automatic plate number identification for vehicles without eTag or manual double-checking. Each gate might cross multiple lanes and a vehicle might cross two lanes when passing gates. Therefore, a passing vehicle might be detected by more than one sensors and a sensor might need to track more than one vehicles at the same time.

a. (5 pts) What locking or synchronization mechanism can help to avoid double charging to a passed vehicle?

b. (5 pts) Using eTag, how to design sensor software to save power consumption in collecting information?

c. (5 pts) Where in the whole system (might be different) are the most reasonable places to do the automatic or manual plate number identification? Why?

a. 問題:同一輛車被同一個門架上兩個 sensor(跨兩車道)、或相鄰時間重複讀到,各自去扣款 → 重複

做法:每次通行用一個唯一 key(eTag ID + gate ID +時間窗)當「已扣款」記錄。sensor thread 要扣款前先對這個 key 取得 lock(mutex/semaphore)→ 檢查是否已有扣款記錄 → 沒有才扣款並寫入、有就跳過 → 放 lock。檢查與寫入要在同一個 critical section 裡做成 atomic(test-and-set/compare-and-swap)

跨主機(門架上的 sensor 分屬不同 server)的情況:在 storage server 的資料庫對這個 key 設 unique constraint,用 transaction 寫入,只有第一個成功的人能扣款(idempotent),其餘收到「重複」就忽略;process 內的 lock 只能保護同一台機器

對「車+門架+時間窗」這個 key 用 lock/atomic 操作做 check-and-charge,跨 server 用資料庫 unique key+transaction

b. eTag 是 passive(沒電),要靠 sensor 發射無線電供電並讀取,耗電在 sensor 端。省電做法:

① 平常讓 sensor 的 radio 與 CPU 進 sleep,用低功耗的車輛偵測器(地磁、紅外線)以 interrupt 喚醒,再開 RFID 讀取,讀完就睡(event-driven,不 busy polling)

② 沒有觸發時用低 duty cycle 輪詢(例如 10 ms 開、90 ms 關),車流量大(尖峰)才提高頻率

③ 讀到 eTag ID 就關掉發射、同一輛車不重複讀;資料累積成一小批再用 DMA/網路送出,減少喚醒次數

event-driven+sleep、低 duty cycle、讀到就停止發射、批次傳送

c. 自動辨識:放在門架旁的 edge(gate controller):影片很大,在 camera 旁先辨識,只把車牌結果與一張截圖送上去,省網路頻寬、延遲低;只對沒有 eTag 或 eTag 讀取結果異常的車才跑

人工覆核:放在中央(application server/後台):只處理 edge 辨識信心低或爭議的少數車;需要集中排班、權限控管、可調閱完整資料(存在 storage server 的影像),不用分散到 300 多個門架

自動辨識在門架 edge(省頻寬、低延遲);人工覆核在中央(集中、需完整資料與權限)

1310%

13. (10 pts) The collected eTag data and recorded video will be sent to storage servers for applications to access. For security, the information needs to be kept for at least 3 months. Suppose the system allows vehicles without eTag, so that plate number identification or other identification method is necessary.

a. (5 pts) How do we store the collected data and video information from gates to servers so that users can access the correct information as soon as possible? Hint: Vehicle by vehicle or periodically? Stateless or stateful? Batched or Instantly?

b. (5 pts) Please estimate the shortest time for any user to access how much toll is charged after passing a gate. Why?

a. 每輛車通過就即時送(instantly、vehicle by vehicle),不要等一段時間再批次

每筆通行紀錄是自成一體的:event ID、車(eTag 或車牌)、門架、時間、里程費;stateless(server 不需要記得這個 gate 前面的狀態,每筆獨立處理,失敗重送用 event ID 去重,idempotent)

儲存:金額與紀錄(metadata)寫進 storage server 的資料庫,以車號為 key(hash 分片)並建時間索引,replication 保持可用;查詢某車的扣款只打一個分片,最近的資料放 cache

影片大、使用者不急著看:分段、批次上傳 object storage,在事件紀錄裡放它的位置;保存至少 3 個月,近期放快的儲存、較舊的搬到便宜的

每輛車即時送 stateless 的事件(以車號為 key);影片批次上傳另存,事件裡只留位置

b. eTag 正常的路徑,各段延遲的量級:

步驟估計
eTag 讀取+門架 controller 處理約 10–50 ms
門架 → server 的網路(專線)約 10–50 ms
application server 算里程費約 5–20 ms
寫入 storage(含 replication commit)約 10–50 ms
使用者用 app 查詢(網路+web/app/storage)約 100–300 ms

合計約 0.2–0.5 秒,抓 約 1 秒以內;使用者實際要自己打開 app,所以主要是查詢往返

沒有 eTag(要車牌辨識):還要等清楚的影像 frame、辨識,約數秒;要人工覆核就是分鐘到小時,都不是最短的路徑

eTag 正常通行:約 1 秒以內(約 0.2–0.5 秒的系統延遲+使用者查詢);車牌辨識約數秒,人工覆核更久

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

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

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

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com