免費詳解 / 中山 / 軟體 / 104

104 中山資工所考古題解答|軟體逐題詳解

104 中山資工所軟體考古題的逐題詳解,共 8 題,免費公開。

逐題詳解

110%

[Sorting: 10%] Please prove that any comparison-based sorting algorithm requires O(n lg n) computation time in the worst case, where n is the number of objects to be sorted.

要證的是下界 \(\Omega(n\lg n)\)

comparison sort 的執行畫成 decision tree:內部節點是一次比較 \(a_i:a_j\),兩個分支是比較結果,葉子是一種輸出順序

某個輸入的比較次數 \(=\) 根到對應葉子的路徑長,worst case \(=\) 樹高 \(h\)

\(n\) 個相異元素有 \(n!\) 種排列,每一種都必須是某片葉子 → 葉子數 \(\ell\ge n!\)

高 \(h\) 的二元樹至多 \(2^h\) 片葉子 → \(2^h\ge n!\) → \(h\ge\lg(n!)\)

\(n!\ge\left(\dfrac n2\right)^{n/2}\)(後 \(n/2\) 個因子都 \(\ge n/2\))

\(h\ge\dfrac n2\lg\dfrac n2=\dfrac n2\lg n-\dfrac n2=\Omega(n\lg n)\)

得證

merge sort、heapsort 的 \(O(n\lg n)\) 因此是 asymptotically optimal。

210%

[Basic Data Structures: 10%] (1) From the viewpoint of an operating system, please give practical applications of using a stack, queue, and linked list. (3%)

(2) Given a prefix expression “+ + (2) × (3) (4) × + × (5) (6) (7) (8)”, please convert it to the infix expression and calculate the result. (2%)

(3) What is the assumption of simple uniform hashing? Under this assumption, given a hash table with m slots that stores n elements, what is the expected time taken by an unsuccessful search when the hash collisions are resolved by chaining? (3%)

(4) Please explain the property of a binary search tree. (2%)

(1) stack:function call 的 activation record(參數、return address、區域變數);interrupt/system call 時保存 CPU state

queue:scheduler 的 ready queue、各 device 的 I/O wait queue、printer spooling、message passing 的 buffer

linked list:串起所有 PCB、free memory 的 free list、free frame list、linked allocation 的檔案區塊

(2) prefix:+ + 2 × 3 4 × + × 5 6 7 8,逐一取兩個運算元

\(+\ 2\ (\times\ 3\ 4)=2+3\times4=14\);\(\times\ (+\ (\times\ 5\ 6)\ 7)\ 8=(5\times6+7)\times8=296\)

\((2+3\times4)+(5\times6+7)\times8=14+296=310\)

(3) simple uniform hashing:任一個 key 被 hash 到 \(m\) 個 slot 中任一個的機率都是 \(1/m\),且與其他 key 落在哪裡無關

unsuccessful search 要走完整條 chain,chain 期望長度 \(=\alpha=n/m\),再加上算 hash 的 \(O(1)\)

\(\Theta(1+\alpha)=\Theta(1+n/m)\)

(4) 對每個 node \(x\):左子樹所有 key \(\le x\) 的 key,右子樹所有 key \(\ge x\) 的 key(遞迴地對每個 node 成立);inorder traversal 會由小到大輸出所有 key

315%

[Graph: 15%] You are given the adjacency matrix of an undirected graph:

ABCDEFS
A0470000
B4098000
C79011610
D081100100
E0060035
F00110302
S0000520

Starting from node S, please answer the following questions. (1) Draw the corresponding graph. (3%) (2) Write down the sequence of nodes that you visit by using breadth-first search. If there are multiple choices to select nodes, just follow the alphabetical order. (4%) (3) Write down the sequence of nodes that you visit by using depth-first search. If there are multiple choices to select nodes, just follow the alphabetical order. (4%) (4) Show the sequence of edges added to the minimum spanning tree based on the Prim's algorithm. Please use the edge weight to represent each edge in your answer. (4%)

(1) 11 條邊:A–B 4、A–C 7、B–C 9、B–D 8、C–D 11、C–E 6、C–F 1、D–F 10、E–F 3、E–S 5、F–S 2

ABDCFES4798116110352

(2) BFS(鄰居依字母序):S 的鄰居 E、F;E 帶出 C;F 帶出 D;C 帶出 A、B

S, E, F, C, D, A, B

(3) DFS:S → E → C(E 的鄰居 C、F、S,先 C)→ A → B(A 的鄰居 B、C)→ D(B 的鄰居 A、C、D)→ F(D 的鄰居 B、C、F);F 的鄰居全走過,回溯結束

S, E, C, A, B, D, F

(4) Prim 由 S 開始

樹中節點候選邊選
SS–E 5、S–F 2S–F 2
S, FS–E 5、F–E 3、F–C 1、F–D 10F–C 1
S, F, CF–E 3、C–E 6、C–A 7、C–B 9、C–D 11、F–D 10F–E 3
S, F, C, EC–A 7、C–B 9、C–D 11、F–D 10C–A 7
+AA–B 4、C–B 9、C–D 11、F–D 10A–B 4
+BB–D 8、C–D 11、F–D 10B–D 8

加入的邊依序:2, 1, 3, 7, 4, 8 (MST 總權重 25)

ABDCFES4798116110352

415%

[Advanced Data Structures: 15%] (1) Please give the five properties of a binomial tree. (5%)

(2) Consider a Fibonacci heap as follows: roots 9, 20, 40 (circular root list). 9 has children 26, 19, 25; 26 has children 28, 48; 28 has child 37; 19 has child 32. 20 has children 23, 41; 23 has child 54. 40 has child 43. The number in each node indicates its key value. Suppose that we decrease key 48 to 17 and then decrease key 37 to 7. Please show the final result of the revised heap. (5%)

(3) Why does a B-tree can help reduce the cost of disk access? (5%)

(1) binomial tree \(B_k\):

① 恰有 \(2^k\) 個 node

② 高度為 \(k\)

③ 深度 \(i\) 處恰有 \(\binom ki\) 個 node(\(i=0,\dots,k\))

④ root 的 degree 為 \(k\),大於其他任何 node 的 degree

⑤ root 的 \(k\) 個 child 由左到右分別是 \(B_{k-1},B_{k-2},\dots,B_0\) 的 root(即 \(B_k\) 由兩棵 \(B_{k-1}\) 連成)

(2) decrease-key 的做法:新 key 小於 parent 時,把該 node 切到 root list;若 parent 不是 root 且未標記就標記它,已標記就也切下來(cascading cut);最後更新 min

48 → 17:17 \(\lt\) parent 26,切下 17 放進 root list;26 未標記 → 標記 26,停止

37 → 7:7 \(\lt\) parent 28,切下 7 放進 root list;28 未標記 → 標記 28,停止(28 原本未標記,不會往上動 26)

7 \(\lt\) 9,min pointer 指向 7

7min179204026mark192528mark3223415443

root list:7, 17, 9, 20, 40(min = 7);被標記的 node:26、28

decrease-key 不做 consolidate,root 的棵數由 3 變 5;題目圖沒有標記任何 node,視為全部未標記。

(3) disk 存取成本以 seek+rotation 為主,讀 1 byte 與讀一整個 block 差不多,所以要減少的是讀 block 的次數

B-tree 的一個 node 剛好放進一個 disk block,可容納數百到上千個 key 與 child pointer,fan-out 很大,高度只有 \(O(\log_tn)\)(\(t\) 為 minimum degree);一次搜尋只要讀少數幾個 node,node 內的比較在記憶體完成

例:\(t=1001\)、\(n=10^9\),高度約 2~3;binary search tree 高度約 \(\lg n\approx30\),每層可能都是一次 disk access。B-tree 也保持平衡,最壞情況不退化。

510%

[Process: 10%] (1) Please explain the five states of a process. (5%) (2) What is the risk of terminating a single thread by calling the exit() procedure in a multi-threading program? (2%) (3) Except for CPU utilization and throughput, please explain the three criteria to measure the performance of a process scheduling algorithm. (3%)

(1) new:process 正在建立 ready:資源齊全、等 CPU running:指令正在 CPU 上執行 waiting:等 I/O 或某事件完成 terminated:執行結束,等待回收

轉換:new → ready(admitted);ready → running(scheduler dispatch);running → ready(interrupt、時間片用完);running → waiting(I/O 或 event wait);waiting → ready(I/O 或 event 完成);running → terminated(exit)

(2) exit() 結束的是整個 process,所有 thread 一起被殺:其他 thread 做到一半就消失、共用資料可能留在不一致狀態、持有的 lock 不會釋放;只結束單一 thread 應用 pthread_exit() 或讓 thread 函式 return

(3) turnaround time:從提交到完成的總時間

waiting time:在 ready queue 中等待的時間總和

response time:從提出要求到第一次有回應的時間

610%

[File System and Disk: 10%] (1) Except for single-level and two-level directories, what are the three popular schemes to define the logical structure of directories? (3%)

(2) Suppose that the range of a disk's cylinders is [1, 200] and the disk head currently stops at cylinder 55. Let the disk queue contain the requests of blocks on cylinders {99, 187, 39, 124, 14, 131, 65, 70}. Please show how the disk head moves in the SSTF, SCAN, C-SCAN, and C-LOOK disk scheduling schemes. (6%)

(3) What is a component unit of the distributed file system? (1%)

(1) tree-structured directory:可建子目錄形成樹,每個檔案有唯一 path,不能共享

acyclic-graph directory:允許目錄或檔案被多處 link 而共享,不許有環,要用 reference count 決定刪除

general graph directory:允許有環,彈性最大,但走訪可能無窮迴圈,需要 garbage collection 回收

(2) head 在 55,請求 14, 39, 65, 70, 99, 124, 131, 187;SCAN、C-SCAN、C-LOOK 先往大號方向

SSTF:55 → 65(10)→ 70(5)→ 99(29,比回頭 39 的 31 近)→ 124(25)→ 131(7)→ 187(56)→ 39(148)→ 14(25)

\(10+5+29+25+7+56+148+25=305\)

SCAN:往上到 187,走到邊界 200 再折返往下

\((200-55)+(200-14)=145+186=331\)

C-SCAN:往上到 200,直接跳回 1,再往上

\((200-55)+(200-1)+(39-1)=145+199+38=382\)

C-LOOK:往上到最後一個請求 187,直接跳到最小請求 14,再往上

\((187-55)+(187-14)+(39-14)=132+173+25=330\)

排程走訪順序總移動量
SSTF65, 70, 99, 124, 131, 187, 39, 14305
SCAN65, 70, 99, 124, 131, 187, (200), 39, 14331
C-SCAN65, 70, 99, 124, 131, 187, (200), (1), 14, 39382
C-LOOK65, 70, 99, 124, 131, 187, (14), 39330

括號是只經過不服務的位置;C-SCAN、C-LOOK 總量含折返那一段(不含則 183、157)。若 SCAN 先往小號方向:55 → 39 → 14 → 1 → 65 → … → 187,共 \((55-1)+(187-1)=240\)。

(3) component unit:可以獨立存放在單一機器上的最小檔案集合(同一 unit 的檔案必在同一處)

715%

[Memory: 15%] (1) Let the time to search the TLB and to access memory is 25 ns and 95 ns, respectively. Supposing that the TLB has 80% hit ratio, what is the effective memory-access time? Notice that you have to give the calculation. (3%)

(2) Please explain both external and internal fragmentation in memory allocation. (4%)

(3) From the viewpoint of a process, what is thrashing? (2%)

(4) What is memory-mapped I/O? Why do we need it? (3%)

(5) Why can virtual memory speed up process creation? (3%)

(1) hit:\(25+95=120\) ns;miss:\(25+95+95=215\) ns(多讀一次 page table)

\(\text{EAT}=0.8\times120+0.2\times215=96+43\)

139 ns

(2) external fragmentation:contiguous allocation/segmentation 反覆配置釋放後,空閒空間切成許多小洞,總和夠大但沒有任何一個連續洞放得下新 request;用 compaction 或 paging 解決

internal fragmentation:配置單位是固定大小的 block(如 page frame),分到的空間比要的多,多出的部分在 block 內部用不到(例:5 KB 的需求拿 2 個 4 KB frame,浪費 3 KB)

(3) thrashing:process 拿到的 frame 少於它的 working set,幾乎每次存取都 page fault,時間花在換頁上,真正執行的指令很少;CPU utilization 下降

(4) memory-mapped I/O:把 device 的 control register/buffer 對應到一段位址,CPU 用一般 load/store 操作 device,不用專用 I/O 指令

需要的理由:程式模型一致(定址、protection 沿用記憶體機制);大量資料(如 frame buffer)可直接寫入,省去逐 byte 的 I/O 指令

(5) copy-on-write:fork() 時父子共用 page,只在某方寫入時才複製該 page,不必整份複製 address space

demand paging:用到才載入 page;code/library 的 page 可共享

815%

[Synchronization: 15%] (1) Please explain the three synchronization mechanisms for threads to access a shared data item in UNIX systems. (6%)

(2) Suppose that three processes P1, P2, and P3 respectively have timestamps 17, 25, and 30, and P2 currently holds a resource requested by both P1 and P3. Please give the results of wait-die and wound-wait schemes used to handle deadlocks. (4%)

(3) Please explain how the two-phase locking protocol works to check the correctness of a non-serial schedule for atomic transactions. (5%)

(1) mutex lock:進 critical section 前 lock、出來 unlock,同時只有一條 thread 持有

semaphore:整數加上 wait/signal 兩個 atomic 操作;counting 管多個資源、binary 等同 mutex,也可做事件通知

condition variable:搭配 mutex,條件不成立時 wait(同時放掉 mutex),條件改變時由別的 thread signal/broadcast 喚醒

(2) timestamp 小者較老;P1(17)老於 P2(25)老於 P3(30)

P1 要 P2 的資源P3 要 P2 的資源
wait-die(non-preemptive)P1 較老 → P1 waitP3 較年輕 → P3 die(rollback,保留 timestamp 30 重啟)
wound-wait(preemptive)P1 較老 → wound P2,P2 rollback,資源給 P1P3 較年輕 → P3 wait

(3) 2PL:每個 transaction 分兩個階段

growing phase:可取得 lock,不可釋放任何 lock

shrinking phase:釋放第一個 lock 後進入,只可釋放,不可再取得

檢查:non-serial schedule 中若每個 transaction 都遵守「所有 lock 取完才開始釋放」,該 schedule 就是 conflict serializable,與依 lock point(取得最後一個 lock 的時刻)排序的 serial schedule 等價

2PL 不保證沒有 deadlock,也不保證沒有 cascading rollback;strict 2PL 把 exclusive lock 留到 commit/abort 才放。

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

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

購買 · NT$ 850 看中山其他免費詳解

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com