109 年 國立中山大學資訊工程學系碩士班甲組《作業系統與資料結構》
第 1 題10 分
- (10%) Consider the following set of processes, with the length of the CPU-burst time given in milliseconds:
| Process | Burst Time | Priority |
|---|---|---|
| P1 | 10 | 3 |
| P2 | 1 | 1 |
| P3 | 2 | 4 |
| P4 | 1 | 5 |
| P5 | 5 | 2 |
The processes are assumed to have arrived in the order P1, P2, P3, P4, P5, all at time 0.
(a) (5%) What is the completion time of process P₁ using a nonpreemptive priority (a smaller priority
number implies a higher priority) scheduling?
(b) (5%) What is the average waiting time using shortest-job-first scheduling?
登入後即可作答並保存紀錄。
核心觀念
本題考查兩種 CPU 排程:
- 非搶占式優先權排程(nonpreemptive priority scheduling):優先權數字越小,優先權越高。程序一旦取得 CPU,必須執行至完成。
- 最短工作優先(Shortest-Job-First, SJF):優先選擇 CPU burst time 最短的程序。
相關公式如下:
本題所有程序均於時間 到達,因此:
(a)非搶占式優先權排程下, 的完成時間
優先權數字越小代表優先權越高,因此排程順序為:
甘特圖如下:
| 時間區間 | 執行程序 |
|---|---|
在時間 開始執行,CPU burst time 為 ,所以:
(b)SJF 排程下的平均等待時間
依照 CPU burst time 由短至長排序:
第 2 題10 分
- (10%) A machine has 64-bit virtual addresses and 32-bit physical addresses. Pages are 32 KB. How many
entries are needed for the page table?
登入後即可作答並保存紀錄。
核心觀念
頁表的每一個 entry 對應一個虛擬頁,內容通常記錄該虛擬頁所對應的實體頁框,以及有效位、存取權限等控制資訊。
頁面大小為 :
因此,頁內位移需要 個位元。
解題方法
64 位元虛擬位址可表示的虛擬位址空間共有:
每頁大小為 bytes,所以虛擬頁數為:
每個虛擬頁需要一個頁表 entry,因此頁表需要:
約為 個 entries。
題目給出的 32 位元實體位址用來決定實體頁框數:
第 3 題10 分
- (10%) Consider the two-dimensional array a:
double a[][] = new double [250] [250];
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 < 250; i++)
for (int j = 0; j < 250; j++)
a[i][j] = 0;
(b) (5%)
for (int j = 0; j < 250; j++)
for (int i = 0; i < 250; i++)
a[i][j] = 0;
登入後即可作答並保存紀錄。
核心觀念
本題結合下列概念:
- 分頁系統:頁面大小為 bytes。
- 每個
double佔 bytes,因此一頁可容納
個double。 - LRU(Least Recently Used)頁面置換。
- 陣列的儲存順序會決定連續存取是否落在相鄰頁面。
- 程式指令位於 page 0,且每次指令擷取都會存取 page 0,因此 page 0 會持續留在 frame 0。實際可供陣列資料使用的只有另外兩個 frame。
解題方法與頁面配置
a[0][0] 位於 location ,正好是 page 1 的起始位置。
因為陣列採用 column-major 儲存,元素 a[i][j] 前面共有 個元素,因此其位址為
每頁可放 個元素,所以 a[i][j] 所在的頁面為
由於 ,可化為
因此,每一個 column 佔有:
個頁面,而且每一頁包含該 column 中連續的 個元素。
(a)先固定 ,再掃描
for (int i = 0; i < 250; i++)
for (int j = 0; j < 250; j++)
a[i][j] = 0;
固定某個 時, 由 增加至 。此時
當 每增加 ,頁面號碼增加 ,所以同一個內層迴圈會依序存取 個不同頁面:
第 4 題10 分
- (10%) What would be the output of the following C program that uses the Pthreads API? (Note that the line
numbers are for reference only.)
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <pthread.h>
5 #include <sys/types.h>
6 #include <sys/wait.h>
7
8 static void runner (void param)
9 {
10
11
12}
((int) param)--;
pthread_exit(0);
13
14 int main(int argc, char **argv)
15 {
16
int value = 11;
17
pid_t pid fork();
18
if (pid > 0) {
19
int status;
20
waitpid(-1, &status, 0);
21
printf("A=%d\n", value--);
22
}
23
else if (pid == 0) {
24
25
26
23
28
29
30
31
32
33
34
35
36
int status;
pid_t pid= fork();
if (pid > 0) {
}
int status;
waitpid(-1, &status, 0);
printf("B=%d\n", --value);
else if (pid == 0) {
pid_t pid fork();
pthread_t tid;
pthread_create(&tid, NULL, runner, &value);
pthread join(tid, NULL);
if (pid > 0) {
waitpid(-1, &status, 0);
printf("C=%d\n", value++);
39
}
40
else {
41
printf("D=%d\n", ++value);
42
}
43
}
44
else {
45
return 1;
46
}
47
}
48
else {
49
return 1;
50
}
51
return 0;
52}
登入後即可作答並保存紀錄。
核心觀念
fork()行程複製:呼叫fork()會建立獨立的子行程(Child Process),複製獨立的記憶體位址空間(Address Space),子行程對變數的修改不會影響父行程。pthread_create()執行緒共享:同一個行程內的執行緒(Thread)共享相同的記憶體位址空間,因此runner執行緒對value的修改會反映在該行程的value上。waitpid()同步順序:父行程呼叫waitpid(-1, &status, 0)會暫停執行直到子行程結束,因此輸出順序由最深層的子行程開始由內向外執行。
各行程推導過程
初始主行程設為 ,變數 。
1. 行程 (最外層父行程)
- 執行第 17 行
fork()產生子行程 。 - 進入
if (pid > 0)(第 18 行),呼叫waitpid阻塞等待 結束。 - 結束後,執行第 21 行
printf("A=%d\n", value--)。 - 由於 的 未受子行程影響仍為 11,後綴遞減
value--印出A=11。
2. 行程 ( 之子行程)
- 繼承 。
- 執行第 25 行
fork()產生子行程 。 - 進入
if (pid > 0)(第 26 行),呼叫waitpid阻塞等待 結束。 - 結束後,執行第 30 行
printf("B=%d\n", --value)。 - 前綴遞減
--value將 由 11 減至 10,印出B=10。
第 5 題10 分
- (10%) 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 4 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?
登入後即可作答並保存紀錄。
核心觀念
i-node 以不同層級的指標指向檔案資料區塊:
- 直接指標:直接指向資料區塊。
- 單重間接指標:指向一個存放指標的區塊。
- 雙重間接指標:指向「存放單重間接指標的區塊」。
- 三重間接指標:再多一層指標區塊。
檔案的邏輯大小只計算資料內容,不計算間接指標區塊所占的額外空間。
基本計算
區塊大小為
每個間接區塊可存放的指標數為
(a) 最小檔案大小
UNIX 允許建立空檔案。空檔案不需要配置任何資料區塊,其檔案長度可為
若限定為非空檔案,最小邏輯長度是 byte,但磁碟配置空間會使用一個 -byte 資料區塊。本題以檔案的邏輯大小為準。
(b) 最大檔案大小
各種指標所能涵蓋的資料區塊數如下:
| 指標類型 | 可指向的資料區塊數 | 可存放資料量 |
|---|---|---|
| 10 個直接指標 |
第 6 題20 分
- (20%) Suppose that a disk drive has 1024 cylinders, numbered from 0 to 1023. The drive is currently serving
a request at cylinder 200, and the previous request was at cylinder 125. The queue of pending requests, in
FIFO order, is
50, 500, 250, 800, 350, 550, 400, 600, 100.
Starting from the current head position, what is the total distance (in cylinders) that the disk arm moves to
satisfy all the pending requests for each of the following disk scheduling algorithms?
(a) (4%) SSTF
(b) (4%) SCAN
(c) (4%) LOOK
(d) (4%) C-SCAN
(e) (4%) C-LOOK
登入後即可作答並保存紀錄。
核心觀念
本題考查磁碟排程演算法,以及磁頭移動距離的計算。
磁頭總移動距離為每一段移動距離的總和:
其中 為目前磁頭位置。
目前位置為 ,前一個服務位置為 ,因此磁頭原本是往柱面編號增加的方向移動。故 SCAN、LOOK、C-SCAN、C-LOOK 的初始方向皆為由小柱面往大柱面。
待處理請求排序如下:
題目雖以 FIFO 順序列出請求,但本題所問的五種演算法都不依照 FIFO 服務。
(a) SSTF
SSTF(Shortest Seek Time First)每次選擇距離目前磁頭位置最近的請求。
服務順序如下:
各段移動距離為:
因此:
(b) SCAN
SCAN 又稱電梯演算法。磁頭先沿目前方向移動,直到磁碟實體端點 ,再反向服務請求。
服務順序為:
磁頭先由 移動到最右端 :
再由 反向移動至最左側尚未處理的請求 :
因此:
所以:
(c) LOOK
LOOK 與 SCAN 的服務方向相同,但不會特別移動到磁碟實體端點;當目前方向已沒有請求時,立即反向。
磁頭往增加方向服務至最大請求 ,再反向服務 與 :
向右移動距離:
反向由 移動至 :
總距離為:
因此:
第 7 題5 分
- (5%) Given the following weighted graph, show the order in which the edges are added to the minimum cost
spanning tree using Kruskal's algorithm. (Use weight to represent edges in your answer and just show the
order.)
A
7
1
6
9
5
C
D
4
3
5
E
F
2
登入後即可作答並保存紀錄。
核心觀念
Kruskal 演算法用來求最小生成樹(Minimum Spanning Tree, MST),核心規則如下:
- 將所有邊依權重由小到大排序。
- 依序考慮每條邊。
- 若加入該邊不會形成環,便加入生成樹。
- 若會形成環,便捨棄該邊。
- 若圖有 個頂點,生成樹最後必須包含 條邊。
題圖文字化後缺少部分頂點端點標示,以下依原圖為六個頂點 – 解讀。
解題方法
圖中所有邊的權重排序為
依 Kruskal 演算法處理:
第 8 題5 分
- (5%) Show the Huffman tree created from the following nodes arranged in a priority queue.
ABCDE
J
F GH I
6 9 12 14 18 24 27 29 35 45
登入後即可作答並保存紀錄。
核心觀念
Huffman 演算法使用貪婪法建立最佳前綴碼:
- 將每個原始節點依權重放入最小優先佇列。
- 每次取出權重最小的兩個節點合併。
- 合併節點的權重為兩子節點權重總和,再放回優先佇列。
- 持續合併至只剩根節點。
題面文字版未保留字母與權重的逐項對齊,以下採常見配對:
解題方法
依序合併最小的兩個節點:
| 步驟 | 合併 | 新節點權重 | 合併後的優先佇列 |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | |||
| 6 | |||
| 7 | |||
| 8 | |||
| 9 | 完成 |
固定左子樹標記為 、右子樹標記為 ,且每次將較小權重放在左側,得到 Huffman 樹:
第 9 題10 分
- (10%) For any input of size n and assuming that k₁ denotes the maximum number of digits and k₂ the number
of buckets, what is the average-case running time of each of the following sorting algorithms: (a) selection
sort, (b) merge sort, (c) heap sort, (d) radix sort, and (e) bucket sort?
登入後即可作答並保存紀錄。
核心觀念
本題比較五種排序法的平均時間複雜度。判斷時要掌握:
- 是否需要比較元素大小。
- 每一輪或每一層處理的元素數量。
- 遞迴層數或重複掃描次數。
- 非比較排序是否依賴位數 與桶數 。
其中:
- :輸入資料筆數。
- :每個鍵值的最大位數。
- :桶的數量。
解題方法與各排序法分析
(a) Selection Sort:
Selection sort 每一輪從尚未排序的部分尋找最小值。
第 輪需要比較約 次,因此總比較次數為
所以不論輸入資料原本是否接近排序完成,主要成本都來自完整掃描未排序區域,平均時間複雜度為
(b) Merge Sort:
Merge sort 將資料不斷分成兩半,直到每個子陣列只剩一個元素,再逐層合併。
- 分割深度:約為 層。
- 每一層合併所有元素一次,成本為 。
因此總時間為
合併時的比較次數會因輸入內容略有不同,但每層仍須處理全部元素,因此平均複雜度不變。
(c) Heap Sort:
Heap sort 通常分成兩階段:
- 建立 max-heap,成本為 。
- 重複移除堆頂元素並重新調整 heap。
第 次移除元素時,heap 的高度最多為 ,共需移除 次,因此:
所以平均時間複雜度為
Heap sort 的最好、平均、最壞時間複雜度皆為 。
(d) Radix Sort:
Radix sort 依序處理每一個位數。若每一位使用 counting sort 或類似穩定的桶式排序,處理一位的成本為
原因是:
- 掃描 筆資料:。
- 處理 個桶或計數位置:。
第 10 題10 分
- (10%) The Ackermann function A(m, n) is defined recursively for non-negative integers m and n as follows:
A(m,n) =
{
n+1 if m = 0,
A(m-1, 1) if m > 0 and n = 0,
A(m-1, A(m, n-1)) if m > 0 and n > 0.
}
Its value grows very quickly, even for small values of m and n. For instance, A(4, 1) = 65533. What would
be the values of A(3, 2) and A(2, 4)?
登入後即可作答並保存紀錄。
核心觀念
本題考查遞迴函數的展開與分層計算。Ackermann 函數依照 的值逐層成長,計算時可先推導較低層的通式:
再利用遞迴定義計算 、,最後求得 與 。
解題方法
1. 推導
當 時:
當 時:
由 可得:
因此:
2. 推導
當 時:
當 時:
代入 :
所以:
因此:
3. 計算
先求 :