計算機系統導覽練習題 (Practice - Computer Systems Tour)
Related Concepts
- 01-Computer-Systems-Tour/01-Information-and-Program-Translation
- 01-Computer-Systems-Tour/02-Hardware-Organization-and-Program-Execution
- 01-Computer-Systems-Tour/03-OS-Abstractions-and-Networks
- 01-Computer-Systems-Tour/04-Amdahls-Law-and-Concurrency-Themes
| 關鍵字 / 情境 | 答案模式 |
|---|---|
| 同一串 bytes 有不同意義 | Information is bits + context |
| 編譯四階段 | Preprocessing (cpp) → Compilation (cc1) → Assembly (as) → Linking (ld) |
| 副檔名鏈與 text/binary 分界 | .c → .i → .s(text)→ .o → executable(binary);分界在 assembler |
| PC (program counter) | 存放下一條機器指令的位址;CPU 不斷「執行 PC 所指指令、更新 PC」 |
| OS 三大抽象 | files → I/O devices;virtual memory → memory+disk;processes → 全部 |
| context switch | save 舊 context → restore 新 context → 交出控制權 |
| kernel 是不是 process | 不是;是常駐記憶體、管理所有 process 的 code 與 data structures |
| 給 α、k 求 speedup | S = 1/[(1−α) + α/k] |
| 加速到無限快 (k → ∞) | S∞ = 1/(1−α);未改善部分是不可逾越的瓶頸 |
| concurrency vs parallelism | 前者 = 多活動同時存在的一般概念;後者 = 用 concurrency 讓系統更快 |
| 平行三層次(高→低) | thread-level → instruction-level → SIMD |
Question 1 - Bits 與 Context [recall]
在一個系統中,磁碟上的檔案、記憶體中的程式、網路上傳輸的封包,本質上都是一串 bits。
那麼,系統靠什麼區分「同一串 bytes」到底是整數、浮點數、字串還是機器指令?
靠檢視它們的情境 (context)——這是全書第一個基本觀念「Information is bits + context」。
資料物件本身沒有內建型別;同一串 bytes 在不同情境下可以是整數、浮點數、字串或機器指令。
延伸:機器中的數值只是數學整數/實數的有限近似 (finite approximations),可能出現出乎意料的行為。
Question 2 - 編譯系統四階段 [recall]
在 Unix 上執行
gcc -o hello hello.c後,hello.c經過四個階段變成可執行檔。
請依序寫出四個階段的名稱、對應的程式,以及各階段的中間檔案副檔名。
- Preprocessing (
cpp):hello.c→hello.i(依#directive 修改原始碼,如插入stdio.h內容) - Compilation (
cc1):hello.i→hello.s(組合語言程式) - Assembly (
as):hello.s→hello.o(relocatable object program) - Linking (
ld):hello.o+printf.o→hello(executable object file)
注意:gcc本身是驅動全部四階段的 compiler driver,不是狹義的 compiler。
Question 3 - Text File 與 Binary File [recall]
編譯過程中會產生
hello.i、hello.s、hello.o與最終的hello。
哪些是 text file、哪些是 binary file?text file 的定義是什麼?分界發生在哪個階段?
hello.c、hello.i、hello.s 是 text file;hello.o 與 hello 是 binary file。
text file 定義:內容全部由 ASCII 字元構成的檔案;其他一律是 binary file。
text/binary 的分界在 assembler (as):它把文字形式的組合語言翻譯成二進位機器指令。
Question 4 - 硬體四大組件與 Word [recall]
一個典型系統的硬體組織由四大類元件構成。
請列出這四大類,並說明 bus 以什麼單位傳輸資料、現今常見的大小為何。
四大組件:buses、I/O devices、main memory (DRAM)、processor (CPU,含 PC、register file、ALU)。
bus 以固定大小的 word(字組) 為傳輸單位;word size 是基本系統參數,現今多為 4 bytes (32-bit) 或 8 bytes (64-bit)。
補充:main memory 邏輯上是從 0 起算的線性位元組陣列。
Question 5 - Program Counter [recall]
從按下電源到斷電,CPU 一直在做同一件事。
請說明 PC (program counter) 存放什麼,以及 CPU 反覆執行的基本循環。
PC 是 CPU 內的 word-size 暫存器,任一時刻都存放主記憶體中下一條要執行的機器指令的位址。
CPU 從開機到斷電不斷重複:執行 PC 所指的指令 → 更新 PC 指向下一條指令。
注意:下一條指令不一定與剛執行的指令在記憶體中相鄰(例如 jump 之後)。
Question 6 - OS 三大抽象 [recall]
OS 透過三個基本抽象達成「保護硬體」與「提供簡單一致機制」兩大目的。
請寫出 files、virtual memory、processes 各自抽象化了哪些硬體。
- files → I/O devices
- virtual memory → main memory + disk
- processes → processor + main memory + I/O devices
記法:process 涵蓋範圍最大(全包),virtual memory 次之,file 最小(只抽象 I/O 裝置)。
Question 7 - Context Switch 與 Kernel [recall]
單一 CPU 上,shell process 與 hello process 能「並行」執行,靠的是 OS 的 context switching。
(a) context 包含哪些狀態?(b) context switch 的三個步驟?(c) kernel 是一個 process 嗎?
(a) Context = PC 目前值 + register file + main memory 內容。
(b) 三步驟:save 舊 process 的 context → restore 新 process 的 context → 把控制權交給新 process(新 process 從上次停下處精確繼續)。
(c) 不是。kernel 是常駐記憶體、用來管理所有 process 的 code 與 data structures 集合;應用程式透過 system call 進入 kernel。
Question 8 - Concurrency 與 Parallelism [recall]
CSAPP 對 concurrency 與 parallelism 給出了明確區分,並指出平行可在三個抽象層次上開發。
請說明兩者差異,並由高到低列出三個平行層次。
- Concurrency:一般性概念,指系統中存在多個同時進行的活動。
- Parallelism:利用 concurrency 讓系統跑更快。
三層次(高→低):thread-level concurrency(multi-core、hyperthreading)→ instruction-level parallelism(pipelining、superscalar)→ SIMD parallelism(一條指令做多個運算)。
Question 9 - Amdahl's Law 基本計算 [application]
某系統中,一段程式佔整體執行時間的 60% (α = 0.6)。
(a) 若把這段程式加速 3 倍 (k = 3),整體 speedup 是多少?
(b) 若加速到無限快 (k → ∞),整體 speedup 上限是多少?
(a) S = 1/[(1−0.6) + 0.6/3] = 1/[0.4 + 0.2] = 1.67×
(b) S∞ = 1/(1−α) = 1/0.4 = 2.5×
洞見:即使把佔 60% 的部分加速到「幾乎不花時間」,整體也只有 2.5 倍——未改善的 (1−α) 部分是不可逾越的瓶頸。
Question 10 - Amdahl's Law 反解 k [application]
系統某部分佔整體執行時間的 90% (α = 0.9)。
若目標是讓整體達到 4× 的 speedup,該部分需要被加速多少倍 (k)?
由 S = 1/[(1−α) + α/k]:4 = 1/(0.1 + 0.9/k)
→ 0.1 + 0.9/k = 0.25 → 0.9/k = 0.15 → k = 6
檢驗:1/[0.1 + 0.9/6] = 1/0.25 = 4 ✓(即書中 Problem 1.2 的模式:兩邊同乘 4k 得 0.4k + 3.6 = k)
Question 11 - 判斷編譯階段 [application]
你有一個 C 檔案開頭如下,執行
gcc -o prog prog.c時發生了一連串轉換:#include <stdio.h> #define MAX 100(a) 上面兩行 directive 由哪個階段的哪個程式處理?處理方式是什麼?
(b) 若 linker 回報undefined reference to 'printf',表示哪個檔案沒被正確合併?
Question 12 - 為何整體加速遠低於預期 [analysis]
工程師 A 花大量成本把系統中某模組加速了 10 倍 (k = 10),該模組原本佔整體執行時間 50% (α = 0.5),
但實測整體只快了約 1.8 倍,他懷疑量測有誤。
請用 Amdahl's Law 分析:量測合理嗎?若想讓整體達到 3×,繼續優化這個模組是否可行?
量測合理:S = 1/[(1−0.5) + 0.5/10] = 1/0.55 ≈ 1.82×,與實測相符。
繼續優化此模組不可行:即使 k → ∞,上限 S∞ = 1/(1−0.5) = 2× < 3×——剩下 50% 未改善部分是天花板。
結論:想大幅加速整個系統,必須改善佔整體很大比例的部分;此時應把資源轉向另外 50% 的程式。
附註:表達改進時用比值 T_old/T_new 加「×」最清楚,百分比定義有歧義。
Question 13 - Web Server 該用 Threads 還是 Processes [analysis]
你要設計一個需要高並行 (concurrency) 的網路伺服器,每個連線的處理單元都必須讀寫同一份共享的快取資料結構。
從第 1 章對 process 與 thread 的描述分析:應選擇「多 process」還是「單一 process 內多 thread」?給出兩個依據。
應選擇單一 process 內多 thread。依據:
- 資料共享:同一 process 的 threads 共享同一份 code 與 global data,共享快取資料結構天然容易;多個 process 各有獨立的 virtual address space(獨占假象),跨 process 共享資料較困難。
- 效率:threads 通常比 processes 更有效率(建立/切換成本較低)。
這正是書中「thread 日益重要」的三個原因之二(第三個:網路伺服器本身就需要 concurrency)。
| 題型模式 | 解題要點 |
|---|---|
| 同一串 bytes 多種解讀 | bits + context:區分資料物件的唯一因素是檢視情境 |
| 編譯流程排序 / 中間檔案 | cpp → cc1 → as → ld;.c/.i/.s 是 text,.o/executable 是 binary |
# directive 歸屬 |
一律 Preprocessor (cpp);stdio.h 內容直接插入 |
| 硬體元件辨識 | buses(以 word 傳輸)、I/O devices、DRAM main memory、CPU(PC/registers/ALU) |
| PC / 執行循環 | PC 存下一條指令位址;循環 = 執行 PC 所指指令 + 更新 PC |
| 抽象 → 硬體配對 | files→I/O;VM→memory+disk;processes→全部;(virtual machine→整台電腦) |
| context switch 排序 | save → restore → 交出控制權;kernel 不是 process |
| Amdahl 正算 | 代入 S = 1/[(1−α) + α/k] |
| Amdahl 反解 / 上限 | 由目標 S 反解 k;k→∞ 時 S∞ = 1/(1−α),(1−α) 是天花板 |
| thread vs process 抉擇 | 共享資料容易 + 更有效率 → 需要共享時選 threads |
| concurrency/parallelism 名詞辨析 | 一般概念 vs 用並行求快;層次 thread → instruction → SIMD |