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

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

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

逐題詳解

110%

[Process and Synchronization: 10%] (1) What is the priority inversion problem? How can we solve this problem? (4%) (2) Please explain how a deadlock avoidance scheme works? (3%) (3) Please give three necessary conditions for any solution to the critical-section problem. (3%)

(1) priority inversion:高優先權的 H 要用的資源(lock)被低優先權的 L 持有,H 只能等;此時若中優先權的 M 可執行,會搶走 L 的 CPU,結果 M 比 H 先跑,優先權被反轉,H 的等待沒有上界

解法:priority-inheritance protocol——持有資源的 process 暫時繼承所有等它的 process 中最高的優先權,M 就搶不走;放掉資源後還原

(2) deadlock avoidance:process 事先宣告各類資源的最大需求;每次配置前先檢查配下去後系統是否仍在 safe state(存在 safe sequence),是才配,否則讓它等

單一實例資源用 resource-allocation graph 加 claim edge 看是否成環,多實例用 banker's algorithm

(3) mutual exclusion:同時最多一個 process 在 critical section

progress:沒人在 critical section 時,只有想進去的 process 參與決定誰進,且不能無限期延後

bounded waiting:提出進入要求後,別的 process 先進入的次數有上界

210%

[File System and I/O: 10%] (1) What is a mount point? (2%) (2) What is the advantage of multilevel naming? (2%) (3) What are the two parts of the random-access time for a magnetic disk? (2%) (4) Please explain the two interrupt request lines for a CPU? (4%)

(1) mount point:既有檔案系統中用來掛載另一個檔案系統(volume)的目錄;掛上後走到該目錄看到的是新檔案系統的 root,原目錄內容被遮住,unmount 後才回來

(2) multilevel(階層式)naming:以完整 path name 識別檔案,不同目錄可有同名檔案而不衝突;每個使用者只搜自己那一層,搜尋範圍縮小;便於分類、共享與存取控制

(3) seek time(磁頭移到目標 cylinder)+ rotational latency(等目標 sector 轉到磁頭下)

(4) nonmaskable interrupt:不能被關閉,用於不可回復的錯誤(如記憶體 parity error)

maskable interrupt:CPU 可在執行不可被打斷的指令序列前遮蔽;一般 device controller 以這條線通知 I/O 完成

315%

[Memory Management: 15%] (1) Please explain the three common solutions to the dynamic storage allocation problem in memory. (6%) (2) What are the purposes of MMU and TLB? (2%) (3) Show that a stack algorithm will never encounter the Belady's anomaly. (4%) (4) How does the Buddy system work to allocate kernel memory? (3%)

(1) 從一串大小不一的 free hole 挑一個滿足 \(n\) bytes 的:

first-fit:從頭找,第一個夠大的就用,最快

best-fit:找夠大的裡面最小的,剩下的碎片最小,但要掃過整串

worst-fit:找最大的,剩下的洞較大、較可能再被利用,也要掃整串

三者都解不掉 external fragmentation。

(2) MMU:硬體,執行時把 logical address 轉成 physical address(relocation/protection)

TLB:page table entry 的快取,hit 時省掉讀 page table 的那次記憶體存取

(3) stack algorithm:以 \(n\) 個 frame 時,讀完前 \(t\) 個 reference 後在記憶體中的 page 集合 \(B_t(n)\),恆有 \(B_t(n)\subseteq B_t(n+1)\)(inclusion property)

某次 reference 在 \(n+1\) 個 frame 時 fault → 該 page 不在 \(B_{t}(n+1)\) → 不在 \(B_t(n)\)(子集) → \(n\) 個 frame 時也 fault

即 \(n+1\) 個 frame 的 fault 次數 \(\le\) \(n\) 個 frame 的 fault 次數,fault 數不會隨 frame 增加而變多

得證

LRU、OPT 滿足 inclusion property;FIFO 不滿足,才會有 Belady's anomaly。

(4) buddy system:從實體連續的一塊記憶體中配置 2 的冪次大小

要 \(n\) bytes → 進位到 \(2^k\);沒有該大小的 free block 就把更大的 block 對半切成一對 buddy,一直切到剛好 \(2^k\)

釋放時看 buddy 是否也 free,是就合併成上一層大小的 block,往上遞迴 → 容易湊回大塊連續記憶體

代價是 internal fragmentation(例:要 33 KB 得到 64 KB)。

415%

[Protection and Security: 15%] (1) Please give three methods to implement a protection domain and explain when domain switching will occur in each method. (3%) (2) Please list the advantages of using language-based protection. (4%) (3) What is the difference between a computer virus and a computer worm? (4%) (4) In asymmetric encryption, how do you realize key distribution? (2%) (5) What is the difference between signature-based and anomaly detection? (2%)

(1) 每個 user 一個 domain:換使用者(登入/登出)時 domain switching

每個 process 一個 domain:process 送訊息給另一個 process 並等回應時切換

每個 procedure 一個 domain(domain 內物件為該 procedure 的區域變數):呼叫 procedure 時切換

(2) ① 保護需求以宣告寫在程式語言裡,不必為每個資源自己寫檢查程式

② 與特定作業系統無關,可攜

③ 可由 compiler 在 compile time 靜態檢查,執行時不必每次存取進 kernel,成本較低

④ 能描述比 kernel 提供的更細、貼近應用語意的存取權限(以 type 為單位)

(3) virus:藏在合法程式內的一段程式碼,需宿主程式被執行才複製、感染其他程式

worm:獨立的完整程式,自己透過網路在機器間複製散播(例:Morris worm),不需宿主、不需使用者動作

(4) 每人有一對 key:public key 可公開散布(公開目錄或 digital certificate),private key 只由擁有者保管;送方用收方的 public key 加密。public key 的真偽由 certificate authority 簽發的 certificate 保證;不需要先經安全管道交換秘密金鑰

(5) signature-based:比對已知攻擊的特徵樣式,誤報少,但對未知攻擊(zero-day)無效

anomaly detection:先建立正常行為的基準,偏離過大就告警,可抓到未見過的攻擊,但誤報率高

510%

[Hash and Heap: 10%] (1) Supposing that we store n keys in a hash table with size of n2 by a universal hash function, prove that the collision probability is less than 0.5. (5%)

(2) Given a binomial heap below, please show the result of extracting the node with minimum key. (5%)

Binomial heap(head → root list):40 → 13 → 4。40 的 child 是 44;13 的 children 是 31(其 child 80)與 16;4 的 children 是 9、19、15、28,其中 9 的 children 是 11、17、32(11 的 children 14、20,14 的 child 30;17 的 child 41),19 的 children 是 29、26(29 的 child 45),15 的 child 是 21。

(1) \(m=n^2\);universal:任兩個相異 key \(x\ne y\),\(\Pr[h(x)=h(y)]\le1/m=1/n^2\)

\(X\) 為碰撞的 key 對數,\(E[X]=\binom n2\cdot\dfrac1{n^2}=\dfrac{n(n-1)}{2n^2}\lt\dfrac12\)

Markov:\(\Pr[\text{有碰撞}]=\Pr[X\ge1]\le E[X]\lt\dfrac12\)

得證

(2) 原 heap:\(B_1\)(40)、\(B_2\)(13)、\(B_4\)(4),\(2+4+16=22\) 個 node

最小的 root 是 4,拔掉後它的子樹 \(B_0\)(28)、\(B_1\)(15)、\(B_2\)(19)、\(B_3\)(9)依 degree 由小到大串成新 heap,再與 \(\{40(B_1),13(B_2)\}\) 做 union

合併 root list(依 degree):28(0)、40(1)、15(1)、13(2)、19(2)、9(3)

40(\(B_1\)) 與 15(\(B_1\)):15 \(\lt\) 40,15 當 root → 15(\(B_2\)),children 為 21、40

此時有三棵 \(B_2\):15、13、19 → 跳過第一棵,連接後兩棵 13 與 19:13 \(\lt\) 19,13 當 root → 13(\(B_3\))

13(\(B_3\)) 與 9(\(B_3\)):9 \(\lt\) 13,9 當 root → 9(\(B_4\))

28214440154529261980311613301420114117329

root list:28(\(B_0\))→ 15(\(B_2\))→ 9(\(B_4\)),新的最小值是 9

檢查:剩 21 個 node,\(21=10101_2\),剛好 \(B_4+B_2+B_0\)。

610%

[Search and Sort: 10%] (1) What is the major property of a binary search tree? (2%) (2) Given a set of numbers 8, 2, 7, 9, 14, 16, 3, 1, 4, and 10, draw the heap used by the heapsort method. (5%) (3) Prove that the best-case complexity of the quicksort method is O(n lgn). (3%)

(1) 對每個 node \(x\):左子樹的 key 都 \(\lt x\) 的 key,右子樹的 key 都 \(\gt x\) 的 key;inorder traversal 由小到大輸出

(2) heapsort 先 build max-heap。陣列(1 起算)8, 2, 7, 9, 14, 16, 3, 1, 4, 10;從 \(i=5\) 往前 heapify

\(i=5\):14 \(\ge\) 10,不動 \(i=4\):9 \(\ge\) 1、4,不動

\(i=3\):7 與較大的 child 16 交換

\(i=2\):2 與較大的 child 14 交換,再與 child 10 交換

\(i=1\):8 與較大的 child 16 交換,8 \(\ge\) 7、3 停止

1492101473816

[16, 14, 8, 9, 10, 7, 3, 1, 4, 2]

(3) partition 掃一遍子陣列,成本 \(\Theta(n)\);best case 是 pivot 每次落在正中間

\(T(n)=2T(n/2)+\Theta(n)\),master theorem(\(a=b=2\),\(n^{\log_22}=n\))case 2 → \(T(n)=\Theta(n\lg n)\)

遞迴樹:共 \(\lg n\) 層,每層總成本 \(cn\),合計 \(cn\lg n\)

就算每次切成固定比例 \(\alpha:(1-\alpha)\),深度仍是 \(\Theta(\lg n)\)、每層 \(O(n)\),仍是 \(O(n\lg n)\)

得證

715%

[Trees: 15%] (1) What are the two major properties of a tree? (2%) (2) Prove that any red-black tree with k internal nodes can have a height no more than 2lg(k+1). (10%) (3) Explain three variants of a B-tree: B+-tree, B*-tree, and 2-3-4 tree. (3%)

(1) connected(連通)、無 cycle(acyclic)

由此得:\(n\) 個 node 恰有 \(n-1\) 條邊,任兩點間只有唯一一條 path。

(2) NIL 當成外部的黑色 leaf;\(bh(x)\):從 \(x\)(不含)到任一後代 leaf 的路徑上黑色 node 數;\(h\) 為樹高

Claim:以 \(x\) 為 root 的子樹至少有 \(2^{bh(x)}-1\) 個 internal node

對 \(x\) 的高度做歸納:\(x\) 是 NIL,\(bh=0\),\(2^0-1=0\) 個,成立

\(x\) 為 internal node:兩個 child 的 black-height 至少是 \(bh(x)-1\)(紅 child 等於 \(bh(x)\),黑 child 是 \(bh(x)-1\))

子樹 node 數 \(\ge(2^{bh(x)-1}-1)+(2^{bh(x)-1}-1)+1=2^{bh(x)}-1\)

紅 node 的 child 必為黑,root 到 leaf 的路徑上紅 node 不相鄰,黑 node 至少佔一半 → \(bh(root)\ge h/2\)

\(k\ge2^{bh(root)}-1\ge2^{h/2}-1\) → \(2^{h/2}\le k+1\)

\(h\le2\lg(k+1)\)

得證

(3) B+-tree:資料(或指向資料的 pointer)全放在 leaf,internal node 只放引導搜尋的 key;leaf 以 pointer 串成 linked list,range query 與循序掃描找到起點後沿 list 走

B*-tree:node 最低使用率由 1/2 提高到 2/3;node 滿了先把 key 重新分配給相鄰 sibling,塞不下才 split,且是兩個滿 node 拆成三個 2/3 滿的 node,空間利用率高、split 少

2-3-4 tree:order 4 的 B-tree,每個 internal node 有 2、3 或 4 個 child(1~3 個 key),所有 leaf 同深度;與 red-black tree 一一對應

815%

[Graphs: 15%] Given a graph below, please answer the following questions, where you should denote an edge between two vertices i and j by (i,j).

BDGAFICEH31101456781129784

(1) Starting from node E, give the sequence by breadth-first-search. If you have multiple choices, just follow the alphabetical order. (3%) (2) Starting from node D, give the sequence by depth-first-search. If you have multiple choices, just follow the alphabetical order. (3%) (3) Show the result by the Kruskal's algorithm. (3%) (4) Starting from node I, show the result by the Prim's algorithm. (3%) (5) Show the minimum connected dominating set. (3%)

14 條邊:(D,G)1、(E,F)2、(B,D)3、(H,I)4、(B,E)5、(D,F)6、(C,E)7、(F,G)7、(E,H)8、(G,I)8、(A,C)9、(A,B)10、(G,H)11、(B,C)14

鄰居(字母序):A: B C B: A C D E C: A B E D: B F G E: B C F H F: D E G G: D F H I H: E G I I: G H

(1) BFS 由 E:E 的鄰居 B、C、F、H;B 帶出 A、D;F 帶出 G;H 帶出 I

E, B, C, F, H, A, D, G, I

(2) DFS 由 D:D → B(D 的鄰居 B、F、G)→ A(B 的鄰居 A、C、D、E)→ C(A 的鄰居 B、C)→ E(C 的鄰居 A、B、E)→ F(E 的鄰居 B、C、F、H)→ G(F 的鄰居 D、E、G)→ H(G 的鄰居 D、F、H、I)→ I

D, B, A, C, E, F, G, H, I

(3) Kruskal:邊由小到大,成環就丟掉(同權重照字母序)

邊權重取捨
(D,G)1取
(E,F)2取
(B,D)3取
(H,I)4取
(B,E)5取
(D,F)6丟(D–B–E–F 已連通)
(C,E)7取
(F,G)7丟
(E,H)8取
(G,I)8丟(G、I 已連通)
(A,C)9取(第 8 條邊,9 點成一棵樹)

MST:(D,G)、(E,F)、(B,D)、(H,I)、(B,E)、(C,E)、(E,H)、(A,C),總權重 \(1+2+3+4+5+7+8+9=39\)

BDGAFICEH31101456781129784

(4) Prim 由 I:每次由樹內的點往外挑最小的邊

順序加入的邊權重新點
1(I,H)4H
2(H,E)8E
3(E,F)2F
4(E,B)5B
5(B,D)3D
6(D,G)1G
7(E,C)7C
8(C,A)9A

加入順序 (I,H)、(H,E)、(E,F)、(E,B)、(B,D)、(D,G)、(E,C)、(C,A),總權重 39,樹同 (3)

第 2 步 (H,E) 與 (I,G) 同為 8,這裡先加入 (H,E);若先加入 (I,G),樹中的 (G,I) 會取代 (E,H),總權重仍是 39。

(5) connected dominating set \(S\):\(S\) 的 induced subgraph 連通,且圖中每個點不在 \(S\) 內就與 \(S\) 內某點相鄰

最大 degree 為 4,1 個點最多蓋住 5 個點,不夠;2 個相鄰的點最多蓋住 7 個(例 E,B 蓋住 A,B,C,D,E,F,H,缺 G、I),不夠

3 個點:\(\{B,E,H\}\):B 蓋 A、C、D、E;E 蓋 B、C、F、H;H 蓋 E、G、I,共 9 點;B–E、E–H 皆為邊,連通

最小 connected dominating set:\(\{B,E,H\}\),大小 3

\(\{B,D,G\}\)(B–D、D–G 為邊,蓋住全部 9 點)也同為大小 3。

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

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

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

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com