1. Which one of the following is a Correct statement about address space?

    • Logical addresses are sent to the memory unit to fetch program/ data.
    • Physical addresses are generated by CPU during program execution.
    • The size of logical address space need not be the same as the size of physical address space
    • The size of physical address space rather than the size of logical address size limits the address space size of a program execution
  2. Which of the following statement is true?

    • Dynamic loading does not need special support from the OS.
    • Dynamic linking loads partial program into memory when it is needed.
    • Dynamic linking is particularly useful when large amounts of code are infrequently used.
    • Dynamic loading prevents duplicated code.
  3. Consider a tiny paging system that has 16 KB physical mcmory, and the frame size is 1 KB. Let the logical address have 16 bits. The page table uses single-level direct index. Which one of the following is not correct?

    • the frame number has 4 bits
    • the page number has 6 bits
    • there are 16 entries in the page table
    • a page-table entry stores a frame number
    • the page table requires 320 bits, including the valid-invalid bits
詳解

分頁表總位元數: 64 個 entries 中,每個 entries 存放 4-bit 頁框號碼加上 1 bit 有效/無效位元(valid-invalid bit),共 4+1=5 bits4 + 1 = 5\text{ bits},總大小為 64×5=320 bits64 \times 5 = \mathbf{320\text{ bits}}

  1. Which of the following statements is correct?

    • Base register holds the size of a page table.
    • Limit register holds the size of a process.
    • Base and limit registers can be loaded in user mode.
    • Any attempt by a user program to access memory at an address higher than the base register value results in a trap to the OS.
  2. Which of the following ments are correct?

    • Absolute code can be generated if we know where the process will reside in memory at compile time.
    • A logical address is generated by CPU while a physical address is an address seen by the memory unit.
    • Inverted page table is very efficient in searching for a match but inefficient in space used for storing page tables.
    • Paging may suffer from external fragmentation while segmentation may suffer from internal fragmentation.
    • Paging is a memory management scheme that allows the physical address space of a process to be non-continuous.
詳解
  • 第 1 項(正確): 若在編譯時期(compile time)就已知行程將載入記憶體的確切位址,編譯器可直接產生絕對位址碼(absolute code),執行時無需再做位址重定位(relocation)。
  • 第 2 項(正確): CPU 產生的位址稱為邏輯位址(logical address,又稱 virtual address),而記憶體單元(MMU 轉換後)實際看到的位址稱為實體位址(physical address)。
  • 第 3 項(錯誤): 反向分頁表(inverted page table)的特色剛好相反 —— 它在儲存空間上很省(全系統只需一張表,依實體頁框數量建立),但搜尋效率較差(需搜尋整張表或搭配雜湊表來比對 (PID, page number))。
  • 第 4 項(錯誤): 碎片問題剛好相反 ——Paging 固定切分頁框,可能產生內部碎片(internal fragmentation)(最後一頁未填滿),但不會有外部碎片(external fragmentation);Segmentation 依邏輯區段變動配置,容易產生外部碎片。
  • 第 5 項(正確): 分頁(paging)允許行程的實體位址空間(physical address space)分散存放在不連續的實體頁框中,只要透過分頁表建立對應即可。
  1. Choose the correct statements from the multiple choices
    • Given a 2-level page table (and no TLB), exactly 3 memory accesses are needed to fetch an instruction
    • In an unsafe state, the operating system is deadlocked
    • Banker’s algorithm is a deadlock avoidance algorithm.
    • The wait-for graph is used for deadlock detection.
    • None of the above.
詳解
  • 2-level page table 記憶體存取次數(正確): 在沒有 TLB 的情況下,透過 2 層分頁表進行位址轉換需先存取 2 次記憶體(外層分頁表 + 內層分頁表),再加上 1 次記憶體存取來取出實際指令,總共 3 次記憶體存取
  • Unsafe state:僅表示作業系統無法保證所有行程都能順利完成而不陷入死結,後續仍可能透過資源釋放或請求順序避免死結;死結狀態(deadlocked state)只是不安全狀態的子集合
  • 等待圖(wait-for graph)是資源配置圖的簡化形式(適用於每種資源只有單一實例的情況),用來偵測環狀等待以進行死結偵測(deadlock detection)
  1. Given the logical address 0xAEF9 (in hexadecimal) with a page size of 256 bytes, what is the page number?
    • 0xAE
    • 0xF9
    • 0xA
    • 0x00F9
詳解

分頁大小為 256 bytes256\text{ bytes}(28 bytes2^8\text{ bytes}),代表分頁位移量(page offset)佔 8 bits。在十六進位表示法中,每 1 位十六進位數字代表 4 bits4\text{ bits},因此 8 bits 恰好是最後 2 位十六進位數字。

  1. 邏輯位址: 0xAEF9
  2. 分頁位移量(最後 2 位,8 bits): 0xF9(對應 249249 bytes)
  3. 分頁號碼(前面剩餘位數): 0xAE
  1. Which statement(s) is(are) true about page replacement?
    • The optimal page-replacement algorithm can be implemented in the kernel
    • Careful selection of data structures and programming structures can lower page fault rates.
    • When a system is thrashing, the degree of multiprogramming should be increased to increase CPU utilization
    • For the optimal page-replacement algorithm, the page-fault rate may increase as the number of allocated frames increases.
詳解
  • 最佳分頁替換演算法(Optimal / OPT / MIN)需要預知未來完整的記憶體存取參考串(reference string),在一般執行時期無法得知未來存取順序,因此無法在核心中實作,主要作為衡量其他演算法效能的理論基準。
  • 系統發生顛簸(thrashing)時,行程因頁框不足而不斷發生分頁錯誤與換頁,CPU 使用率會驟降。此時應降低多工度(degree of multiprogramming,例如暫停部分行程或進行掛起/換出),釋出頁框讓剩餘行程有足夠工作集(working set),才能提升 CPU 使用率。
  • 最佳分頁替換演算法屬於堆疊演算法(stack algorithm),滿足包含性質(inclusion property),不會發生 Belady’s anomaly
  1. Which are correct for virtual-memory management?
    • Virtual memory makes the task of programming much easier, because the programmer no longer needs to worry about the amount of physical memory available.
    • When the copy-on-write technique is used, only the pages that are modified by either process are copied; all unmodified pages can be shared by the parent and child processes.
    • The LRU page-replacement algorithm can be though as the optimal page- replacement one looking backward in time, rather than forward.
    • A large page size is preferable to solve the internal fragmentation.
詳解
  • 在寫入時複製(Copy-on-Write, CoW)機制下,父行程與子行程初始時共用相同的分頁並標記為唯讀;只有當某一方嘗試寫入修改特定分頁時,作業系統才會為該分頁配置新頁框並複製內容,未修改的分頁則持續共用,大幅降低 fork () 建立行程的成本。
  • 最佳替換演算法(OPT)替換「未來最久不會被使用的分頁」,而最近最少使用(LRU)替換「過去最久未被使用的分頁」;因此常以「往回看時間的 OPT」作為直覺理解的比喻(雖然兩者仍各有特性與限制)。
  • 分頁大小越大,最後一個分頁平均未填滿的比例通常越高,內部碎片(internal fragmentation)反而更嚴重;若要降低內部碎片,通常應採用較小的分頁大小(但分頁太小會使分頁表變大,需在兩者間權衡)。
  1. Which are correct for virtual-memory management?
  • The three major componcnts of the page-fault service time include (1) service the page-fault interrupt, (2) read in the page, and (3) restart the process
  • When the copy-on-write technique is used, only the pages that are modified by either process are copied.
  • The optimal page-replacement algorithm is dificult to implement, because it requires future knowledge of the reference string.
  • The cost of using prepaging is always less than the cost of servicing the corresponding page faults.
詳解
  1. 在作業系統中,處理一次分頁錯誤(page-fault service time)的三大主要階段正是:(1)處理分頁錯誤中斷(service the page-fault interrupt)、(2)從次要儲存裝置讀入所需分頁(read in the page),以及(3)重新啟動受影響的行程(restart the process)。
  2. 寫入時複製(Copy-on-Write, CoW)在行程共享分頁時,僅當某個行程嘗試修改特定分頁時才會配置新頁框並複製該分頁,未修改的分頁則維持共用。
  3. 最佳分頁替換演算法(Optimal / OPT)會挑選「未來最長時間不會被使用」的分頁進行替換,由於實際執行時無法預知未來的記憶體參考串(reference string),因此難以在一般系統中實作。
  4. 若預先載入的分頁在後續並未被使用(或很快就被替換掉),就會浪費磁碟 I/O 時間與記憶體頁框,反而造成額外成本。
  1. Which are correct for virtual memory?
  • FIFO page replacement is a stack algorithm
  • The LRU strategy is the optimal page-replacement algorithm looking backward in time, rather than forward
  • The accuracy of the working set depends on the selection of Δ\Delta (i.e., the working-set window). If is too small, it will encompass the entire locality.
  • If I/O is done to or from user virtual memory, these pages do not need to be locked in memory when demand paging is used
詳解
  • FIFO 分頁替換演算法不是堆疊演算法(not a stack algorithm)。它不滿足包含性質(inclusion property),因此可能出現配置更多頁框反而增加分頁錯誤的 Belady’s anomaly;而 LRU 與 OPT 才是堆疊演算法。
  • 工作集視窗大小 Δ\Delta 的選擇會影響工作集的準確度,但若 Δ\Delta 太小,就無法涵蓋完整的局部性(locality),容易遺漏仍在使用的分頁;若 Δ\Delta 太大,則可能涵蓋多個局部性而包含過多不再需要的分頁(若 Δ→∞\Delta \to \infty,才會涵蓋整個程式執行期間的所有分頁)。
  • 當對使用者虛擬記憶體進行 I/O(例如 DMA 傳輸)時,相關分頁通常需要鎖定/固定在實體記憶體中(pin /lock in memory)。否則在 I/O 進行期間,若該分頁被換出或重新分配,I/O 可能會寫入錯誤的記憶體位置或發生存取衝突。
  1. Consider the page replacement algorithms FIFO (first in first out), IRU (least recently used), and OPT optimal. Which of the following cases are possible?
  • FIFO outperforms LRU
  • IRU outperforms FIFO
  • FIFO, IRU, and OPT have the same number of page faults
  • LRU causes more page faults with a larger number of free frames
  • Implementing OPT in real operating systems
詳解
  • FIFO outperforms LRU(可能): 雖然一般程式具有時間局部性,LRU 通常表現較好,但在特定參考串(例如循環存取模式,像 3 個頁框下存取 1, 2, 3, 4, 1, 2, 3, 4)下,FIFO 的分頁錯誤次數確實可能少於 LRU
  • LRU outperforms FIFO(可能): 這是實務與教科書中最常見的情況。當參考串重複存取近期使用過的分頁時,LRU 會保留熱資料,分頁錯誤次數會比 FIFO 更少。FIFO, LRU, and OPT have the same number of
  • page faults(可能): 當配置的頁框數量大於或等於參考串中不重複的分頁總數時(例如總共只有 3 種分頁 1, 2, 3,而可用頁框數 ≥3\ge 3),三種演算法都只會在第一次載入時發生冷啟動分頁錯誤(cold misses),之後全數命中,分頁錯誤次數完全相同。LRU causes more page faults with a larger number of free
  • frames(不可能): 這描述的是 Belady’s anomaly。LRU 屬於堆疊演算法(stack algorithm),滿足包含性質(inclusion property),當可用頁框數量增加時,分頁錯誤次數只會減少或維持不變,絕不會增加。
  1. Consider a two-level page table scheme . A 32 bit logical address is divided into a 12 bits of outer page number and 12 bits of inner page number and 8 bits of page offset. If we trace a particular process and record the following address sequence with hexadecimal representation:
    Ox07EF9812, 0x07EF9823, 0x07EF98DE, 0x00000012, 0x00000045, 0x00000089, 0x01AABB99, Ox01AABB54, 0x01 AABB77, 0x02989876, 0x00000078, 0x03DFDF89, 0x03DFDF77, 0x00000098 0x048890FF, 0x048890EE, 0x02989874, 0x03DFDFDE, Ox00000099, 0x03DFDF78, Ox03DFDF24, 0x02989822, 0x02989845, 0x01AABB88, 0x01 AABB70, 0x02989899, 0x02989845, 0x00000011, 0x01AABB23, Ox07EF9888, 0x00000099, 0x01AABB34, 0x01AABB87. Which of the following statements is incorrect?
  • The number of page faults for the LRU replacement algorithms, with four page frames is 8
  • The number of page fauits for the optimal replacement algorithms, with three page frames is 9
  • If we set working-set window to 10 memory references, the working set size at the 10th memory references is 5
  • The number of page faults for the FIFO replacement algorithis, with five page frames is 9.
詳解

32-bit 邏輯位址切分為:12 bits 外層頁碼 + 12 bits 內層頁碼 + 8 bits 分頁位移量。
在十六進位中,最後 2 位(8 bits)是分頁位移量(page offset),前面 6 位(24 bits)共同決定唯一的一個邏輯分頁(outer page + inner page)。

將題目給定的 33 個十六進位位址去掉最後 2 位後,各分頁代號對應如下:

  • A = 0x07EF98
  • B = 0x000000
  • C = 0x01AABB
  • D = 0x029898
  • E = 0x03DFDF
  • F = 0x048890
  1. Choose the correct statements from the multiple choices
  • TLBs are more beneficial with multi-level page tables than with single-leve page tables.
  • Given a constant number of bits in a virtual address, the size of a linear page table decreases with larger pages.
  • The longer the time slice, the more a RR scheduler gives similar results to a FIFO scheduler.
  • An SJF scheduler may preempt the currently running job.
  • None of the above.
詳解
  • 在單層分頁表中,TLB miss 只需額外存取 1 次記憶體查詢分頁表;但在 kk 層多層分頁表中,TLB miss 需要連續存取 kk 次記憶體進行 page table walk。因此,一旦 TLB hit 省下的記憶體存取成本在多層分頁表下大得多,效益更顯著
  • 當虛擬位址總位元數 NN 固定時,若分頁大小 PP 變大,分頁位移量(page offset)所佔位元數 log⁡2P\log_2 P 增加,導致虛擬分頁號碼(VPN)的位元數 N−log⁡2PN - \log_2 P 減少,線性分頁表(linear page table)的總項目數 2N−log⁡2P2^{N - \log_2 P} 隨之減少,分頁表整體大小也就縮小
  • 循環排班(Round-Robin, RR)的時間配額(time slice /quantum)越長,行程在時間配額用完前就自行執行完畢或進入等待的機率越高;當 time slice 趨近無窮大(大於所有行程的 CPU burst)時,RR 的排班行為就完全退化為先進先出(FIFO / FCFS)。
  1. Which of the following is a benefit of allowing a program that is only partially in memory to execute?
  • Programs can be written to use more memory than is available in physical memory.
  • CPU utilization and throughput is ncreased
  • Less 1O is needed to load or swap each user program into memory.
  • All of the above
詳解
  • 邏輯位址空間與實體記憶體大小脫鉤,即使程式總大小大於實際實體記憶體,只要當前執行的分頁在記憶體中就能運作。
  • 每個行程佔用的實體記憶體變少,記憶體中可同時容納更多行程(提高 multiprogramming degree);當某個行程等待 I/O 時,CPU 更容易切換到其他就緒行程,進而提升 CPU 使用率與系統吞吐量(throughput)。
  • 啟動程式或換入行程時,只需載入目前需要的分頁,不必一次把整個程式或所有未使用區段讀進記憶體,減少不必要的磁碟 I/O 量與載入延遲。
  1. Choose the correct statements from the multiple choices.
  • Dirty bit is used to indicate that the associated page has been modified.
  • The scheme of modify bit can reduce the time required to service a page fault.
  • The operating system sets the valid-invalid bit for each page to allow or disallow access to the page.
  • When the valid-invalid bit is set to valid, the associated page is the system’s physical address space.
  • None of the above.
  1. Which one(s) of the following can cause threshing in a virtual memory system?
  • Decreasing the degree of multiprogramming
  • Increasing the size of the page file (or swap partition)
  • Under clocking the CPU
  • Removing one half of the memory chips for the system
詳解
  • Decreasing the degree of multiprogramming(不會導致 thrashing): 顛簸(thrashing)是指系統花在分頁錯誤與換頁的時間遠大於實際執行程式的時間,通常是因為多工度太高、每個行程分到的頁框不足。降低多工度反而能釋出頁框給其他行程,是緩解 thrashing 的做法,不是成因。
  • Increasing the size of the page file (or swap partition)(不會直接導致 thrashing): 擴大 swap 空間只增加次要儲存裝置的換出容量,本身不會直接減少實體頁框或引發 thrashing。
  • Under clocking the CPU(不會直接導致 thrashing): 降低 CPU 時脈會讓整體運算變慢,但不會直接造成行程工作集(working set)無法容納於實體記憶體而引發頻繁缺頁。
  • Removing one half of the memory chips for the system(會導致 thrashing): 拔除一半的實體記憶體晶片會使可用頁框(physical frames)大幅減少,各行程能分配到的頁框變少,容易小於其工作集大小,導致分頁錯誤率暴增與頻繁換頁,進而引發 thrashing(題目中的 threshing 為 thrashing 的拼字誤植)。
  1. About virtual memory, which of the following is (are) true?
  • A memory protection violation is raised by the MMU unit.
  • Swap space is the portion of virtual memory that is on the hard disk or SSD, used when RAM is full
  • A program can be invoked by the operating system into different instances of processes.
  • The space on the disk or flash memory reserved for the full physical memory space of a process is called swap space.
詳解
  1. 記憶體管理單元(MMU)在進行位址轉換時,會比對分頁表或區段表項目中的保護權限(如唯讀、可讀寫、可執行);若行程嘗試進行不允許的操作(例如寫入唯讀分頁),MMU 會觸發記憶體保護違規中斷(segmentation fault /protection fault),交由作業系統處理。
  2. 交換空間(swap space /backing store)是磁碟或 SSD 上用來支援虛擬記憶體的區域,當實體記憶體(RAM)吃緊或需要換出閒置分頁時,作業系統可將分頁移至 swap space 以釋放實體頁框。
  3. 程式(program)是磁碟上的被動實體(如執行檔),行程(process)是執行中的主動實體;同一個程式可被作業系統載入多次,建立多個彼此獨立的行程實例(例如同時開啟多個相同的終端機或瀏覽器視窗)。
  4. 在需求分頁(demand paging)與虛擬記憶體系統中,swap space 並不一定要為每個行程預留「完整實體記憶體空間」大小的容量(swap space 甚至可能小於所有行程的總邏輯/實體需求,僅存放被換出的分頁或依動態需求配置)

===
錯過的觀點總結:

  • Dynamic loading / linking
    • Dynamic loading 不需要 OS 的特別支援
  • 演算法
    • 甚麼是堆疊演算法(stack algorithm)?:若演算法滿足包含性質(inclusion property),則為堆疊演算法。OPT 與 LRU 是堆疊演算法,FIFO 不是。
  • Copy-on-Write
    • 父行程與子行程初始時共用相同的分頁並標記為唯讀;只有當某一方嘗試寫入修改特定分頁時,作業系統才會為該分頁配置新頁框並複製內容,未修改的分頁則持續共用,大幅降低 fork () 建立行程的成本。
  • TLB
    • TLB hit 省下的記憶體存取成本在多層分頁表下大得多,效益更顯著
更新於