A computer has six tape drives, with n processes competing for them. Each process may need two drives. For which values of n is the system deadlock free?
deadlock 的最壞情形:每個 process 各拿到 1 台、都在等第 2 台,而且沒有空的 drive
只要每個 process 先各發 1 台後仍剩 1 台,就有 process 湊滿 2 台跑完並釋放
\(n\cdot(2-1)\lt6\) → \(n\le5\)
\(n=1,2,3,4,5\)(\(n\ge6\) 時 6 個 process 各拿 1 台即 deadlock)
210%
Consider the two-dimensional array A: double A[][] = new double[200][200]; where each double occupies 8 bytes and A[0][0] is at location 200, in a paged system with pages of size 200 bytes. A small process is in page 0 (locations 0 to 199) for manipulating the matrix; thus every instruction fetch will be from page 0. For three page frames, how many page faults are generated by the following array initialization loops, using LRU replacement and assuming (1) page frame 0 has the process in it, (2) the other two are initially empty, and (3) the array is stored in memory column-major?
(a) (5%) for (int i = 0; i < 200; i++)
for (int j = 0; j < 200; j++)
A[i][j] = 0;
(b) (5%) for (int j = 0; j < 200; j++)
for (int i = 0; i < 200; i++)
A[i][j] = 0;
1 page \(=200\) B \(=25\) 個 double;column-major:一整個 column 200 個 double \(=1600\) B \(=8\) pages;全陣列 \(=1600\) pages
Assume a page reference string for a process with m frames (initially all empty). The page reference string has length n with p distinct page numbers occurring in it. For any page-replacement algorithms, (a) (5%) What is a lower bound on the number of page faults? (b) (5%) What is an upper bound on the number of page faults?
(a) frame 一開始全空,每個相異 page 第一次被用到一定 fault
下界 \(p\)
(b) 最壞每次參考都 fault
上界 \(n\)
410%
Given a UNIX i-node with ten direct blocks and three levels of indirect blocks (i.e., a single, a double, and a triple) and assuming that the sizes of a pointer and a block are, respectively, 8 bytes and 8 Kbytes, answer the following questions. (a) (5%) What would be the size of the smallest file allowed in bytes? (b) (5%) What would be the size of the largest file allowed in bytes?
(a) 檔案只要有內容,就至少配 1 個 data block(8 KB);檔案大小本身最小為 1 byte
1 byte
空檔案(0 byte)不佔 data block。
(b) 一個 index block 放 \(8192/8=1024=2^{10}\) 個 pointer
A disk has 10000 cylinders, each with 10 tracks of 512 blocks. A seek takes 1 ms per cylinder moved. If no attempt is made to put the blocks of a file close to each other, two blocks that are logically consecutive will require an average seek, which takes 6 ms. If, however, the operating system makes an attempt to cluster related blocks, the mean interblock distance can be reduced to 2 cylinders and the seek time reduced to 200 μs. Assuming that the rotational latency is 3 ms and the transfer time is 10 μs per block, answer the following questions. (a) (5%) How long does it take to read a 200 block randomly placed file? (b) (5%) How long does it take to read a 200 block clustered file?
Red-Black Tree. (1) (4%) A red-black tree is a binary search tree where every node is either red or black. What are the four properties of a red-black tree? (2) (3%) Suppose that a red-black tree contains n internal nodes. What is its maximum height? What is the time to search one value? What is the time to find the maximum value? (3) (3%) Do you think whether a red-black tree is balanced or not? Please give your reason.
Tree Search & Expression Tree. (1) (6%) Suppose that each internal node of a tree has at most three children. Given the BFS sequence “iceodlmfhjbgna” and the DFS sequence “icdbglemnofhaj” of the tree, please draw that tree. (2) (4%) Given a postfix expression “8 3 2 + - 4 9 3 / + * 3 * 7 +”, please translate it into an infix expression and give the calculation result.
(1) 兩個序列都由 i 開頭 → root 是 i
BFS:i | c e o | d l m f h j | b g n a
DFS:i | c d b g l | e m n | o f h a j
DFS 把 i 的子樹切成三段,段首為 c、e、o → i 的 child 是 c、e、o(BFS 第 2 層)
BFS 第 3 層 d l m f h j 依序屬於 c、e、o 的 child:c 的段 \(\{d,b,g,l\}\) 含 d、l;e 的段 \(\{m,n\}\) 含 m;o 的段 \(\{f,h,a,j\}\) 含 f、h、j → c:d、l;e:m;o:f、h、j
BFS 第 4 層 b g n a:d 的子樹有 b、g;m 有 n;h 有 a
檢查 DFS:i, c, d, b, g, l, e, m, n, o, f, h, a, j ✓;BFS:i | c e o | d l m f h j | b g n a ✓
Treap. (1) (5%) Given the following treap, please show the final result after inserting a node (D:9). (2) (5%) Following (1), please show the final result after inserting a node (F:2).
9 \(\lt\) 23 → D 是 E 的左 child,對 E 右旋,D 再升(E 成 D 的右 child)
9 \(\gt\) 7,heap 條件成立,停止
(2) F:2:G → B → D → E,F 掛在 E:23 右邊;2 比路徑上的 priority 都小,一路往上提
對 E 左旋(F 升,E 成 F 的左 child)
對 D 左旋(F 升,D 成 F 的左 child,F 原左子樹 E 轉成 D 的右 child)
對 B 左旋(F 升,B 成 F 的左 child,D 轉成 B 的右 child)
對 G 右旋(F 升為 root,G 成 F 的右 child,G 的左子樹變空)
檢查:中序仍為 A B C D E F G H I K;每個 parent 的 priority 都不大於 child。
910%
Hash Functions. (1) (4%) What are the two major characteristics of hash functions to make them be widely adopted in authentication applications? (2) (4%) What is a hash collision? How can you solve this problem by chaining? (3) (2%) What is a perfect hashing?
(1) ① one-way(preimage resistance):由 hash 值無法反推原訊息,存密碼或驗證時不洩漏原文
② collision resistance:找不到兩個不同訊息有相同 hash 值,別人無法拿另一份訊息冒充
另外基本條件:可接受任意長度輸入、輸出固定長度、計算容易。
(2) collision:兩個不同的 key 經 hash function 得到同一個位址
chaining:每個位址(bucket)掛一條 linked list,hash 到該位址的 key 都串在這條 list 上;查詢先算位址,再沿 list 逐一比對,平均 \(O(1+\alpha)\)(\(\alpha=n/m\) 為 load factor)
Heaps. (1) (2%) What is a binomial heap? (2) (4%) Please explain the two binomial-heap properties. (3) (2%) Suppose that two binomial heap H1 and H2 have n1 and n2 nodes, respectively. Then, what is the worst-case running time to unite H1 and H2? (4) (2%) What is a Fibonacci heap?