109 年 國立中山大學資訊工程學系碩士班甲組《作業系統》
第 1 題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 burst 完成。
- Shortest-Job-First, SJF(最短工作優先):每次選擇 CPU burst time 最短的程序執行。本題所有程序皆在時間 到達,因此可直接依 burst time 排序。
相關定義如下:
- Completion time:
- Waiting time:
由於所有程序皆在時間 到達,因此某程序的等待時間就是它開始執行前,前面所有程序 burst time 的總和。
(a) 非搶先式優先權排程中, 的完成時間
優先權表如下:
| 程序 | Burst Time | Priority |
|---|---|---|
| 10 | 3 | |
| 1 | 1 | |
| 2 | 4 | |
| 1 | 5 | |
| 5 | 2 |
優先權數字越小越優先,因此執行順序為:
甘特圖:
在 與 完成後才開始:
因此:
第 2 題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?
登入後即可作答並保存紀錄。
核心觀念
頁表的每一筆 Page Table Entry(PTE) 對應一個虛擬頁。因此,頁表所需的 entries 數量為:
虛擬位址可分為:
- 虛擬頁號(Virtual Page Number, VPN)
- 頁內位移(Page Offset)
頁面大小決定位移欄位的位元數;剩餘的虛擬位址位元則用來表示虛擬頁號。
解題方法
依一般考題的位元組定址假設:
1. 求頁內位移位元數
頁面大小為 :
因此,頁內位移需要 bits。
2. 求虛擬頁號位元數
虛擬位址長度為 bits,所以:
3. 求頁表 entries 數量
bits 的虛擬頁號可表示:
個不同的虛擬頁,因此單層頁表需要:
第 3 題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;
登入後即可作答並保存紀錄。
基礎設定與記憶體配置
- 矩陣規格: 個
double元素,每個占 8 bytes。 - 頁面容量:每頁大小 200 bytes,可容納 個元素。
- 起始頁面:
a[0][0]位址為 200,恰好為 Page 1 的起始位置。陣列整體占據 Page 1 至 Page 2500。 - 資料排列:以欄為主(Column-major)。每一欄有 250 個元素,連續占據 個頁面。
- 可用框頁:共 3 個 Page Frame。Frame 0 固定存放 Page 0(程式碼),僅剩 2 個 Frame 可供資料頁面做 LRU 置換。
(a) 詳解
- 存取順序:外層為列 index ,內層為欄 index 。存取順序為
a[0][0], a[0][1], a[0][2], ...。 - 頁面跳躍:因為陣列為 Column-major 儲存,固定 時,相鄰 的元素在記憶體中相隔一整欄(250 個元素 = 10 個頁面)。內迴圈每前進一步,存取的頁面號碼即增加 10。
第 4 題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 (* (int*) param)--;
11 pthread_exit(0);
12 }
13
14 int main(int argc, char **argv)
15 {
16 int value = 11;
17 pid_t pid;
18 int status;
19
20 pid = fork();
21
22 if (pid < 0) {
23 perror("fork failed");
24 exit(1);
25 } else if (pid == 0) { /* child process */
26 printf("A: ");
27 pthread_t tid;
28 pthread_create(&tid, NULL, runner, &value);
29 pthread_join(tid, NULL);
30 printf("%d\n", value);
31 } else { /* parent process */
32 printf("B: ");
33 pid_t pid_t = fork();
34
35 if (pid_t < 0) {
36 perror("fork failed");
37 exit(1);
38 } else if (pid_t == 0) { /* grandchild process */
39 printf("C: ");
40 pthread_t tid;
41 pthread_create(&tid, NULL, runner, &value);
42 pthread_join(tid, NULL);
43 printf("%d\n", value);
44 } else { /* parent process (of pid_t) */
45 printf("D: ");
46 waitpid(pid, NULL, 0); /* wait for child (pid) */
47 waitpid(pid_t, NULL, 0); /* wait for grandchild (pid_t) */
48 printf("%d\n", value);
49 }
50 }
51
52 return 0;
53 }
登入後即可作答並保存紀錄。
核心觀念
本題綜合考查 fork()、Pthreads、程序記憶體隔離,以及程序間同步。
fork()會複製目前程序的位址空間。一般變數value在各程序中各自擁有一份,後續修改不會互相影響。- Pthreads 建立的執行緒與原程序共享同一份位址空間,因此執行緒修改
value,同一程序的主執行緒可看見修改結果。 pthread_join()確保runner執行完畢後,主執行緒才繼續印出value。waitpid()只負責等待指定子程序結束,不會把子程序的變數值傳回父程序。- 第一次
fork()後,只有原始父程序會執行第二次fork(),因此總共是 個程序,不是 個。
解題方法
程序流程如下:
原始父程序 P0:value = 11
├── 第一次 fork 的子程序 P1:value = 11
│ └── 執行緒將 value 減為 10,輸出 A: 10
└── 原始父程序 P0:value = 11
├── 第二次 fork 的子程序 P2:value = 11
│ └── 執行緒將 value 減為 10,輸出 C: 10
└── 原始父程序 P0:value = 11
└── 等待 P1、P2 結束後,輸出 B: D: 11
逐一分析:
| 程序 | 執行緒操作 | 最後的 value | 輸出 |
|---|---|---|---|
P1:第一次 fork() 的子程序 | A: 10 | ||
P2:第二次 fork() 的子程序 | C: 10 | ||
| P0:原始父程序 | 沒有建立執行緒修改 value | B: D: 11 |
第一個子程序在第 10 行透過 &value 修改的是自己的 value:
原始父程序之後執行第二次 fork(),第二個子程序取得的是父程序當時的副本:
第二個子程序的執行緒只修改自己的副本:
因此不會改變原始父程序的 value:
父程序最後先等待兩個子程序結束,再執行第 48 行,所以父程序輸出的數字一定是 ,且位於兩個子程序完成之後。
輸出順序
題目沒有指定 stdout 的緩衝模式與程序排程,因此完整字串的排列不唯一。
若採用考題常見的抽象方式,將同一程序的輸出視為一筆記錄,則兩個子程序的先後順序不受保證:
A: 10
C: 10
B: D: 11
或:
第 5 題10 分
(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?
登入後即可作答並保存紀錄。
題幹前提
完整題幹給定:UNIX i-node 有 10 個直接區塊,以及 single、double、triple 三層間接區塊;指標大小為 8 bytes、區塊大小為 4 Kbytes。官方試題 PDF
以下採用 。
核心觀念
每個間接區塊只能存放區塊指標,因此一個間接區塊可容納的指標數為
因此:
- 直接區塊:直接指向資料區塊。
- single indirect:可指向 個資料區塊。
- double indirect:可指向 個資料區塊。
- triple indirect:可指向 個資料區塊。
需區分「檔案的邏輯大小」與「實際配置的磁碟空間」:
- 邏輯大小是檔案實際包含的資料 bytes。
- 磁碟配置以區塊為單位;非空檔案即使只有 1 byte,也至少配置 1 個 4096-byte 區塊。
(a) 最小檔案大小
UNIX 允許建立空檔案。空檔案只需要 inode,不需要配置任何資料區塊,因此其邏輯檔案大小為
若題意限定「非空檔案」,則最小邏輯大小為 1 byte;但其實際配置空間為
(b) 最大檔案大小
各層可容納的資料量如下:
第 6 題20 分
(a) (4%) What would be the total distance (in cylinders) that the disk arm moves to satisfy the following pending requests for a disk with 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. When a request at cylinder 125. When the queue of pending requests is FIFO order, 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?
50, 250, 800, 350, 400, 100, 600
(b) (4%) SSTF
(c) (4%) SCAN
(d) (4%) C-SCAN
(e) (4%) C-LOOK
登入後即可作答並保存紀錄。
核心觀念
本題評量作業系統中**磁碟排程演算法(Disk Scheduling Algorithms)**的運作機制與尋軌距離(Seek Distance / Total Head Movement)計算。
-
基本參數設定:
- 磁柱(Cylinders)範圍:(共 1024 個磁柱)。
- 目前磁頭位置(Current Position):。
- 前一次服務位置(Previous Position):。
- 磁頭移動方向:由 ,表示磁頭目前正朝**磁柱編號增加(往大號碼/向外/向內依定義,即遞增方向)**移動。
- 待處理請求佇列(Pending Requests):。
- 請求排序後為:。
-
各排程演算法定義:
- FIFO (First-In, First-Out / FCFS):按照請求抵達佇列的先後順序依序服務。
- SSTF (Shortest Seek Time First):每次挑選與當前磁頭位置距離最近(尋軌距離最小)的請求優先服務;若距離相同則依約定方向或抵達先後處理。
- SCAN(電梯演算法):磁頭沿目前方向持續移動並服務沿途請求,**必須移動至該方向的最外端邊界( 或 )**後,才反向移動並服務反向沿途的請求。
- C-SCAN (Circular SCAN):磁頭沿目前方向服務至最外端邊界()後,直接折返至另一端邊界()(折返過程不服務請求),再沿原方向繼續服務其餘請求。
- C-LOOK (Circular LOOK):C-SCAN 的改良版,磁頭只需移動到該方向的最遠請求即可折返,折返時直接跳至另一端的最遠請求(不走到邊界 與 )。
解題方法與步驟推導
(a) FIFO (First-In, First-Out)
服務順序即為題目給定佇列順序:
各段移動距離計算:
總移動距離:
(b) SSTF (Shortest Seek Time First)
從磁頭位置 開始,每次選擇最近的請求:
- 目前在 ,候選點距離: 最近 移至
- 目前在 ,候選點距離: 最近 移至
- 目前在 ,候選點距離: 最近 移至
- 目前在 ,候選點距離:(比較 ) 移至
- 目前在 ,候選點距離:(比較 ) 移至
- 目前在 ,剩餘 ,距離: 最近 移至
- 目前在 ,剩餘 移至
服務路徑:
總移動距離:
第 7 題10 分
What are the necessary conditions for a deadlock to occur in a system?
登入後即可作答並保存紀錄。
核心觀念
本題考查死結(deadlock)發生的四個必要條件,又稱 Coffman 四條件。死結是指一組行程彼此永久等待,導致任何行程都無法繼續執行。
死結發生必須同時滿足:
- 互斥(Mutual Exclusion)
- 持有並等待(Hold and Wait)
- 不可剝奪(No Preemption)
- 循環等待(Circular Wait)
可表示為:
只要破壞其中任一條件,就能避免死結發生。
解題方法
題目問的是「死結發生的必要條件」,直接套用 Coffman 四條件,逐一說明其意義即可。
1. 互斥
至少有一項資源屬於不可共享資源,同一時間只能由一個行程使用。
例如:
- 印表機
- 磁帶機
- 某些鎖定中的檔案
- 互斥鎖
若所有資源都能被多個行程同時使用,行程便不會因資源被占用而互相阻塞,因此不會形成死結。
2. 持有並等待
一個行程已經持有至少一項資源,同時又等待取得其他資源。
例如,行程 已持有資源 ,但還需要 才能繼續執行:
若系統規定行程必須一次取得所有所需資源,或在要求新資源前先釋放手上的資源,就能破壞此條件。
3. 不可剝奪
行程已取得的資源不能被系統強制收回,只能由持有該資源的行程自行使用完畢後釋放。
例如,若行程 持有 ,系統不能直接將 剝奪並交給其他行程, 就可能一直占用 。
第 8 題20 分
- [Security: 20%]
(a) (6%) Explain what "Confidentiality", "Integrity", and "Availability" means.
(b) (4%) Explain what "Botnet" and "Malware" means.
(c) (5%) Please explain what is "Digital Envelope" and how it works.
(d) (5%) Explain the main techniques used to construct "Bitcoin" and its main network, and what is "Proof-of-Work".
登入後即可作答並保存紀錄。
(a) Confidentiality, Integrity, and Availability
觀念說明:
CIA 為資訊安全的三大核心目標(CIA Triad):
- 機密性(Confidentiality):確保敏感資料僅能被經授權的使用者存取,防止資料洩漏給未授權的第三方。
- 完整性(Integrity):確保資料與系統在儲存與傳輸過程中未被未授權地篡改、刪除或偽造,維護資料的正確性與一致性。
- 可用性(Availability):確保經授權的使用者在需要時,能夠及時且可靠地存取系統、網路與資料服務,防止服務中斷(如抵禦 DDoS 攻擊)。
【答案】
- Confidentiality(機密性):僅允許授權使用者讀取資料,防止未授權洩密。
- Integrity(完整性):確保資料未經未授權的變更或破壞,維護資料正確性。
- Availability(可用性):確保授權使用者隨時可順暢存取與使用系統服務。
(b) Botnet and Malware
觀念說明:
- 惡意軟體(Malware):為 Malicious Software 的簡稱,泛指任何旨在對電腦、伺服器、用戶端或網路造成損害、破壞、竊密或取得未授權存取的程式碼(如病毒 Virus、蠕蟲 Worm、木馬 Trojan、勒索軟體 Ransomware)。
- 殭屍網路(Botnet):由眾多受惡意軟體感染並受駭客(Botmaster)遠端控制的裝置(Bots/Zombies)所組成的網路,常被用於發動大規模 DDoS 攻擊或發送垃圾郵件。
【答案】
- Malware:旨在破壞、竊密或非法存取系統的所有惡意程式總稱。
- Botnet:由受駭客遠端操控的廣大受感染裝置(殭屍電腦)所構成的網路,常用於發動分散式阻斷服務(DDoS)攻擊。
(c) Digital Envelope
觀念說明:
**數位信封(Digital Envelope)**是一種結合「對稱式加密」的高效能與「非對稱式加密」金鑰管理優點的混合加密技術(Hybrid Encryption)。
運作機制:
- 傳送端加密:
- 傳送方產生一把一次性的對稱金鑰(Session Key)。
- 使用 Session Key 對明文(Plaintext)進行對稱加密,生成密文(Ciphertext)。