海大作業系統(下)
內容節錄於:林致宇教授的課程。
ch6:如何處理多行程
多個Process共同存取同一個資料,
會因為Process的執行順序不同、context switch的發生時間不同,
而造成結果不同。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
每個Process有屬於自己的critical section。
Process會把共同存取到的程式碼,放入Critical section中。
同一時間只能讓一個Process進入自己的Critical section,來避免Race Condition。
- Context switch

context switch的發生時間不同,造成Race Condition。
- Peterson’s Solution

flag 有沒有process想進入critical section
turn 有沒有process在Critical section
讓Critical section同時只會有一個程序執行。
不過在multi processor,會受到Reordering影響。
- test_and_set
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
true return true, lock改為true
false return false, lock改為true
- bounded waiting mutual exclusion (test_and_set)
利用了Peterson’s Solution的概念。
- Mutual exclusion(互斥):任一時間點,只允許一個 process 進入他自已的 critical section。
- Bound waiting:自 process 提出進入 critical section 的申請到獲准進入 critical section 的等待時間是有限的。即若有 n 個 processes 想進入,則任一 process 至多等待 n-1 次即可進入,隱含 No starvation。
- compare_and_swap

true return true, 跟 expected 不一樣改new_value
false return false, 跟 expected 不一樣改new_value
防止程序同時使用同一個記憶體資源。
- acquire() 等待資源釋放
- release() 釋放資源
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
問題:Busy waiting (Spinlock) 無謂的等待,瞎忙的迴圈(佔用CPU資源)。
S是號誌(可用資源的數量):>0就可用,<=0就進入等待
- wait() 等待資源釋放
- signal() 釋放資源
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
透過sleep()暫停自己,而避免Busy waiting的問題。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- wait():希望使用資源的行程,對semaphore 執行wait()
- signal():當行程釋放資源時,執行signal()
- Mutex Locks的問題:Busy waiting (Spinlock) 無謂的等待。
- Semaphores的解法:紀錄可用資源的數量,用sleep()暫停自己,避免Busy waiting。
- 多核處理器,在Busy waiting下不需切換執行緒(減少切換的資源浪費),反而可以降低功耗。
ch8:多行程的問題Deadlock
- Deadlock
兩個以上的行程,等待一項只能等待的行程,導致系統資源無法使用。
- 只發生在有兩個或兩個以上優先權的系統。
- 優先權繼承協定:讓優先度高的先執行。
- 哲學家晚餐問題。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- Mutual exclusion(mutex):筷子一次只能一人使用(至少一個資源不共享)
- Hold and wait:拿著一隻筷子,等待另一隻筷子(需要拿到兩個資源才能執行)
- No preemption:不可以搶別人的筷子(不可搶佔資源)
- Circular wait:大家都互相等待其他人的筷子,成為一個閉環()
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- Prevention預防:解決其中一個發生條件
- Mutual exclusion:如果資源可共享,就能避免。
- Hold and wait:哲學家只能拿一雙筷子或不拿(執行前,不佔用資源)。
有可能Starvation,資源使用率低。
- No Preemption:哲學家拿不到另一隻筷子,就把筷子禮讓給別人。
- Circular Wait:讓使用筷子的順序不是閉環。
-
Avoidance避免:要求OS給出請求資源的額外資訊。
-
Detection/Recovery偵測並回復:發生後再解決。
- Detection去偵測死結。
- Recovery(搶佔或終止)去回復正常。
- Ignorance無知:重開機。
ch9:Main Memory
Program Codes:利用program counter紀錄執行的指令。
Data:利用指令,將加載、儲存到特定位址。
- 整個 process 的資料連續存放在記憶體中。
- 新來的 process,被放入 hole 中。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- 如何插入hole中:
First-fit, Best-fit, and Worst-fit
- First-fit:分配第一個足夠大的hole
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- Best-fit:足夠大的最小hole(lower_bound)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- Worst-fit:最大的hole
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- Fragmentation(記憶體的浪費)
分為程序內部與外部的斷裂。
- External fragmentation
process來去的過程中,會在Process跟process間留下可用的記憶體片段

破碎的記憶體總和足夠讓新程序進來,但因為不連續無法放入。
可以用 compaction 壓縮(將程序全部往前挪)。
- Internal fragmentation
程序放入hole中,仍要將整個單位的空間取走所形成的小塊空間。
這些小塊因為太小而無法容納任何資料。

可以用 Paging 減緩問題(無法解決)。
- 把Memory分成多個frames。
- 把Process分成多個pages,來放入可用的記憶體片段。

- 透過Paging 減緩Internal fragmentation

We expect internal fragmentation to average one-half page per process.
調小page size可以減緩內部斷裂,但維護page/frame tables的開銷大。
- 如何更有效率地使用Paging與設計page table。
- Hierarchical Paging

- 因為page太大,所以不去連續分配page。
- 透過階層式向下尋找:
從最上層的page table找到下一層的page table,直到最下層的記憶體位址。
- Hashed Page Tables

- Virtual page number
- Mapped page frame
- 下一個element的指標
- Inverted Page Tables(反轉分頁表)

對physical address建立page table,去紀錄被哪個process的page所使用。
- Optimal Page Replacement(預測未來)
- Least Recently Used(LRU)(以古鑑今)
- Global Replacement:可以取代所以有的frame。
- Local Replacement:只能取代自己記憶體中的frame。