Ref : http://evotalk.net/blog/?p=324
http://www.im.ntu.edu.tw/~d93001/blog/index.php?entry=entry080305-160553
http://blog.yam.com/swwuyam/article/11459124
# Mutex,Event,Semaphore,Critical Section
Critical Section : 同一個 process 中的 threads。鎖住一個未被擁有的 mutex,比鎖住一個未被擁有的 critical section,需要花費幾乎100倍的時間。因為 critical section 不需進入作業系統核心,直接在 user mode 就可以進行動作。
Mutex : Mutual exclusion。系統中所有的 processes 和 threads。等待一個 mutex 時,可以指定「結束等待」的時間長度,而 critical section 不行。
Semaphore : Mutex 擴充,可決定一次能有幾個執行緒進入
Mutex 和 Semaphore 是Kernal mode的物件,必須由OS支援,通常需要call OS API createMutex的function,程式取得OS回傳的handle,因此速度慢,但可以跨 Process 使用
Critical section是User mode的物件,速度是 Mutex 和 Semaphore 的100倍快。但僅能在一個Process中使用。
Mutex 和 Semaphore 的差別在於後者可以支援Binary Semaphore和Counting Semaphore。若為Counting Semaphore則可以同時控管多的資源。
在程式的撰寫上,常常我們會在Critial section內處理semaphore以達到資源管理的目的。
No comments:
Post a Comment