免費詳解 / 中興 / 資訊概論 / 104

104 中興資工所考古題解答|資訊概論逐題詳解

104 中興資工所資訊概論考古題的逐題詳解,共 15 題,免費公開。題目不公開,這裡只放解答。

題目下載

中興的題目不公開下載

購買完整詳解後會一併提供題目。

逐題詳解

P1-a 13 分

(a) selection:每輪找到最小的,和未排序區第一個「一對一交換」一次 → F

(b) bubble:由後往前反覆比較相鄰兩個,最小的一路「冒」到未排序區最前面,不是直接和第一個對調 → 對

(c) insertion:是把元素插進已排序區,方向不同 → F

(d) shell:insertion 的分組版本 → F

(b)

P1-a 23 分

等大小的 page + 不必同時都在記憶體(用到才載入) → demand paging

paging 只說切成 page、要求整個程式都載入;demand segmentation 切的是不等長的 segment

(d)

P1-a 33 分

相鄰兩個 node 之間的 frame → data-link

transport:process-to-process;network:source-to-destination 的 packet;TCP/IP 沒有 session 層(OSI 才有)

(c)

P1-a 43 分

old master 與 transaction file 合併,輸出 new master → new master 是最新的

(a)

P1-a 53 分

圖片與圖形用 JPEG,視訊用 MPEG

DCT 是 JPEG/MPEG 裡用的轉換工具,不是壓縮標準

(c)

P1-b 16 分

卷面這題只有題幹一行,「the following code segment」沒有印出來;第 1 頁後面、第 2 頁、第 3 頁都沒有對應的程式碼

沒有程式碼就無從追蹤輸出,無法作答

考場上遇到這種情形可以舉手問監考,或在答案卷上註明「題目缺程式碼」

P1-b 25 分

讀進字串,頭尾對調到中間

#include <stdio.h>
#include <string.h>

int main(void)
{
    char s[256];
    int n, i;

    if (scanf("%255s", s) != 1) return 1;   /* 讀一個單字 */
    n = strlen(s);
    for (i = 0; i < n / 2; i++) {            /* 第 i 個與倒數第 i 個對調 */
        char t = s[i];
        s[i] = s[n - 1 - i];
        s[n - 1 - i] = t;
    }
    printf("%s\n", s);
    return 0;
}

輸入 abcdefg(\(n=7\)):對調 \((0,6),(1,5),(2,4)\),中間的 d 不動 → gfedcba

若輸入是含空格的一整行,把 scanf 換成 fgets(並去掉結尾的 \n);反過來「只反轉每個單字、單字順序不變」就是對每個以空白分開的單字各做一次這個迴圈

P1-b 36 分(每小題 3 分)

1 bit sign、8 bits exponent(bias 127)、23 bits fraction(隱含前導 1)

(a) \(12=1100_2\);\(0.640625=\tfrac{41}{64}=\tfrac12+\tfrac18+\tfrac1{64}=0.101001_2\)

\(12.640625=1100.101001_2=1.100101001_2\times2^3\)

sign \(=1\);exponent \(=3+127=130=10000010_2\);fraction \(=100101001\) 後補 14 個 0

\(1\ |\ 10000010\ |\ 10010100100000000000000\) = 0xC14A4000

(b) \(0.375=\tfrac14+\tfrac18=0.011_2=1.1_2\times2^{-2}\)

sign \(=1\);exponent \(=-2+127=125=01111101_2\);fraction \(=1\) 後補 22 個 0

\(1\ |\ 01111101\ |\ 10000000000000000000000\) = 0xBEC00000

P1-b 412 分(每小題 4 分)

(a) 64 GB \(=2^{36}\) bytes,byte-addressable → 實體位址 36 bits

page \(=4\text{KB}=2^{12}\)(offset 12 bits)

frame 數 \(=2^{36}/2^{12}=2^{24}\) ; page 數 \(=2^{32}/2^{12}=2^{20}\)

36 bits;最多 \(2^{24}\) 個 frame;一個 process 最多 \(2^{20}\) 個 page

(b) 一個 page 放 \(4096/8=512=2^9\) 個 entry → 每層吃 9 bits。page number 共 20 bits:\(\lceil20/9\rceil=3\) (9+9+2)

TLB hit(0.99):\(20+100=120\) ns

TLB miss(0.01):\(20+3\times100+100=420\) ns(查 TLB、走 3 層 page table、再取資料)

EMAT \(=0.99\times120+0.01\times420=118.8+4.2=123\) ns

3 層;EMAT = 123 ns

(c) \(\text{EAT}=(1-p)\times100\text{ ns}+p\times15\text{ ms}\),\(15\text{ ms}=1.5\times10^7\) ns

\(=0.9999996\times100+0.0000004\times1.5\times10^7=99.99996+6\approx106\) ns

約 106 ns

P1-b 56 分

performance:striping 把一筆資料切開放到多顆磁碟,讀寫時多顆同時動作,bandwidth 隨磁碟數成長;多筆獨立的小 I/O 也能分到不同磁碟上同時做,throughput 變大

dependability:加入 redundancy — mirroring(RAID 1)保留完整副本,或 parity(RAID 3/4/5)保留校驗資料。一顆磁碟壞掉,用其餘磁碟的資料加 parity 重建,array 在降級模式下照常服務,換上新磁碟後 rebuild

代價:額外的磁碟容量,以及寫入時要更新 parity(RAID 4/5 的 small write 要多幾次 I/O)

P2-110 分(每項 2 分)

(a) critical section process/thread 中存取共享資源的那段程式。同一時間只能有一個在裡面,否則產生 race condition。解法要滿足 mutual exclusion、progress、bounded waiting

(b) thrashing process 分到的 frame 太少,page fault 率很高,CPU 大部分時間在換頁、不是在執行指令,CPU utilization 反而掉下來。用 working set model 或 page-fault frequency 控制每個 process 的 frame 數;必要時 swap out 整個 process

(c) fork() UNIX 建立 process 的 system call。複製呼叫者的 address space 產生 child;回傳值在 parent 是 child 的 pid、在 child 是 0(失敗回 \(-1\)),程式靠回傳值分辨自己是誰。現代系統用 copy-on-write,不真的整份複製

(d) TLB MMU 裡的小型高速 associative cache,存最近用過的「page number → frame number」對應。hit 時不用查 page table,只需一次記憶體存取;miss 才回頭走 page table。沒有 ASID 的話 context switch 要 flush

(e) PCB OS 為每個 process 保存的資料結構:process state、program counter、CPU registers、scheduling 資訊(priority、queue 指標)、memory-management 資訊(page table、base/limit)、accounting、I/O status。context switch 就是把 CPU 狀態存回舊 PCB、從新 PCB 載入

P2-25 分((a) 2、(b) 3)

(a) 10 個 thread 同時對共享變數 acc 做 acc++,沒有任何保護;而 acc++ 不是 atomic,編譯後是三步:load 到 register、加 1、store 回記憶體

兩個 thread 交錯:A load(5)、B load(5)、A 存 6、B 存 6 → 兩次遞增只讓 acc 加 1,更新被蓋掉。結果取決於排程的先後,這就是 race condition

最後印出的 acc 不保證是 100000,每次執行可能不同

(b) 用初值 1 的 semaphore 當 mutex,包住 critical section acc++

#include <pthread.h>
#include <semaphore.h>
#include <stdio.h>
#define THREADS 10
int acc = 0;
sem_t sem_temp;                          /* 新增 */

void *operation (void *a)
{
    int j;
    for (j = 0; j < 10000; j++) {
        sem_wait(&sem_temp);             /* 進 critical section */
        acc++;
        sem_post(&sem_temp);             /* 離開 */
    }
    pthread_exit(NULL);
}
int main (int argc, char *argv[])
{
    pthread_t thread[THREADS];
    int i;

    sem_init(&sem_temp, 0, 1);           /* 初值 1:binary semaphore,要在建 thread 之前 */
    for ( i = 0; i < THREADS; i++)
        pthread_create(&thread[i], NULL, operation, (void *) i);
    for ( i = 0; i < THREADS; i++)
        pthread_join(thread[i], NULL);
    printf("acc = %d", acc);
    sem_destroy(&sem_temp);
    return 0;
}

任何時刻只有一個 thread 能通過 sem_wait,其餘排隊等 sem_post → acc 一定是 100000

把 sem_wait/sem_post 移到 for 迴圈外面也正確,鎖的次數少、比較快,但整段迴圈變成序列執行;題目的 pthread_join 只有一個參數,實際 API 要第二個參數 NULL

P2-315 分(每小題 3 分)

reference string 8 筆,每筆的 page 都還不在記憶體 → 全是 page fault,依序從 free frame list 取最前面的 frame(0 到 7),剛好用完

順序存取內容配到的 frame
1\(P_a\) page 4IJ0
2\(P_b\) page 1OP1
3\(P_b\) page 3ST2
4\(P_a\) page 3GH3
5\(P_a\) page 2EF4
6\(P_b\) page 5WX5
7\(P_a\) page 0AB6
8\(P_a\) page 5KL7

(1) \(P_a\) 的 page table(0–5);沒用到的 page 1 為 invalid

pageframevalid
061
1—0
241
331
401
571

(2) \(P_b\) 的 page table(0–6)

pageframevalid
0—0
111
2—0
321
4—0
551
6—0

(3) physical memory

frame01234567
內容IJOPSTGHEFWXABKL

(4) inverted page table:一個 frame 一筆,記「這個 frame 現在是哪個 process 的哪一頁」,整個系統只有一張

frame01234567
process\(P_a\)\(P_b\)\(P_b\)\(P_a\)\(P_a\)\(P_b\)\(P_a\)\(P_a\)
page41332505

(5) \(P_b\) 存取 page 2:不在記憶體(invalid)→ page fault;8 個 frame 已滿,global replacement 從全部 frame 裡挑 victim

LRU:8 筆存取都不重複,最久沒用的就是最早存取的第 1 筆,\(P_a\) page 4,在 frame 0 → 換掉它,載入 \(P_b\) page 2(QR)

frame01234567
內容QROPSTGHEFWXABKL

同時更新:\(P_a\) page table 的 page 4 改成 invalid;\(P_b\) page table 的 page 2 → frame 0、valid;inverted page table 的 frame 0 改成 \((P_b,\,2)\)

P2-410 分(每題 2 分)

(a) control hazard 是 branch 的結果要到後面 stage 才知道;forwarding 只能把已經算出的值提早送,變不出還沒算出的結果。要靠 branch prediction、flush、delayed branch → F

(b) set-associative:每個 block 可放在一個 set 內固定數量(至少 2)的位置;2-way 就是 2 個位置,「至少兩個」成立 → T

若把句子讀成「恰好」,2-way 是恰好 2 個、不是「至少」,那就 F;這句與教科書對 set-associative 的定義用語相同,照字面取 T

(c) L1 要快(hit time 小),L2 容量大、重點在降低 miss rate → T

(d) RAID 3(byte-interleaved parity)一次 I/O 動用所有資料磁碟,適合大量連續資料(multimedia、科學計算) → T

(e) multiprocessor 除了加速單一程式的平行化(要 concurrent),也可以同時跑彼此獨立的循序程式(job-level parallelism),提升 throughput,不需要單一應用程式本身 concurrent → F

(a) F (b) T (c) T (d) T (e) F

P2-510 分

同一支程式,instruction count 相同,比每個指令的時間:CPU time \(=\text{IC}\times\text{CPI}\times\text{cycle time}\)

A:\(2.2\times\dfrac1{1\text{ GHz}}=2.2\times1\text{ ns}=2.2\) ns/指令

B:\(1.2\times\dfrac1{500\text{ MHz}}=1.2\times2\text{ ns}=2.4\) ns/指令

\(\dfrac{T_B}{T_A}=\dfrac{2.4}{2.2}=\dfrac{12}{11}\approx1.09\)

B 的 CPI 低,但 clock cycle 是 A 的兩倍,抵銷後 A 還是比較快;只看 CPI 或只看 clock rate 都會判錯

Machine A 較快,約 1.09 倍

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

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

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

其他年度與考科

同一年的其他科

同一科的其他年度

聯絡我們

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

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

william.chou.123123@gmail.com