6 3 PetersonsPertersons Solution n 1 2 3





























- Slides: 29
6. 3 Peterson’s解決方案(Perterson’s Solution) n 1. 2. 3. P 0 要證明 互斥性存在 進行(Progress)的要求能被滿足 有限制的等待要求亦能符合。 i=0, j=1 flag[0]=True; turn=1; while(flag[1] && turn==1); P 1 P 0 P 1 i=1, j=0 flag[1]=True; turn=0; while(flag[0] && turn==0); i=0, 1 Modified by Min-Shiang Hwang (亞大黃明祥) © 2013 4
6. 5 號誌 (Semaphore) 6. 5. 1 用途(Usage) q 計數號誌 (counting semaphore)的值可以不受限制。二元號誌 (binary semaphore)的數值可以是 0或 1。在某些系統,二元號誌稱為互斥鎖 (mutex locks),當它們是鎖而且互斥。 mutex=1 wait(mutex); Modified by Min-Shiang Hwang (亞大黃明祥) © 2013 7
6. 5. 2 製作(Implementation) q 號誌定義都有一個主要的缺點,那就是它們都需要忙碌等待 (busy waiting)。意即當一個行程置於其臨界區間時,其它欲進入它們的臨 界區間之行程必定在入口的程式碼形成循環迴路。 Implementation of wait: wait(semaphore *S) { S->value--; if (S->value < 0) { add this process to S->list; block(); typedef struct{ } int value; } struct process *list; n Implementation of signal: } semaphore signal(semaphore *S) { S->value++; if (S->value <= 0) { remove a process P from S->list; wakeup(P); } } n Modified by Min-Shiang Hwang (亞大黃明祥) © 2013 8
6. 5. 3 死結和飢餓(Deadlock and Starvation) n n Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes Let S and Q be two semaphores initialized to 1 P 0 P 1 wait (S); wait (Q); wait (S); . . . signal (S); signal (Q); signal (S); n Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended. Indefinite blocking may occur if we remove processes from the list associated with a semaphore in LIFO (Last-in, First out) order. 9
Priority inversion is a situation where in lower priority tasks will run blocking higher priority tasks waiting for resource (mutex). priority: A(High) B(Medium) C(Low) C was ready to run. So C starts running. locks mutex A is ready to run. Bcz of A is higher priority than C, A gets the CPU (otherwords C relinguishes CPU). While executing "A" task, it's need of resource(mutex) but it locked by lower priority task "C". so task "A" going to be waiting state(priority inversion) B is ready to run. Bcz B is Higher priority than C, B get the CPU frm C. For task "B", its no need of any resource(mutex). so it wont be waiting for any task to get resource. it will be finished first. Then, Task "C" gets CPU from task "B" and finished its task, and reliese the resource(mutex). Task C (waiting task for resource)then gets mutex finish execution. . Here: B(M) task finished exe first C(L) task finished exe second A(H) task finished exe last even its have higher priority than both B and C Modified by Min-Shiang Hwang (亞大黃明祥) © 2013 11
6. 6 典型的同步問題(Classic Problem of Synchronization) 6. 6. 1 有限緩衝區問題(Bounded-Buffer Problem) n n N buffers, each can hold one item Semaphore mutex initialized to the value 1 Semaphore full initialized to the value 0 Semaphore empty initialized to the value N. Empty 5 Full 0 Mutex 1 Buffer[0] Buffer[1] Buffer[2] Buffer[3] Buffer[4] Modified by Min-Shiang Hwang (亞大黃明祥) © 2013 12
6. 6. 2 讀取者-寫入者問題(Readers and Writers Problem) Allow multiple readers to read at the same time. Only one single writer can access the shared data at the same time. 除非有一Writer已獲得允許使用共用資料,否則Reader不需保持等候狀態 (Reader不用等候其他Reader結束,但Writer需要等候)。Writer可能有Starvation 若有一Writer已在等候存取共用資料,則不能有新Reader去讀取資料。Reader可能 有Starvation Semaphore mutex initialized to 1 Semaphore wrt initialized to 1 Integer readcount initialized to 0 Modified by Min-Shiang Hwang (亞大黃明祥) © 2013 13
6. 6. 3 哲學家進餐的問題(Dining-Philosophers Problem) n Shared data q Bowl of rice (data set) q Semaphore chopstick [5] initialized to 1 Ø 至多允許四位哲學家上座 Ø 只有左右二支筷子可取用時,才允許哲學家拾取 Ø 座次為奇數號取其左邊筷子、為偶數號取其右邊筷子 Modified by Min-Shiang Hwang (亞大黃明祥) © 2013 14
condition x, y; x. wait(); : x. signal(); 可以確保同一時間只有一個Process 在monitor內執行。 16
6. 7. 2 使用監督程式哲學家進餐的解(Dining-Philosophers Monitors) Solution Using Ø 只有左右二支筷子可取用時,才允許哲學家拾取 不會發生deadlock但可能有Starvation Modified by Min-Shiang Hwang (亞大黃明祥) © 2013 17
6. 7. 3 使用號誌製作監督程式(Implementing a Monitor Using Semaphores) q 對每一個監督程式提一個號誌mutex(初始值為 1)。每一個行程在進 入監督程式之前,都必須先執行wait (mutex) 的動作,並在離開監 督程式之後,執行signal (mutex) 的動作。 mutex=1; next=0; x_sem=1; x. count=0; semaphore next: 控制通知等候process next_count紀錄等候next號誌地˙process數 F: external procedure x. wait() Modified by Min-Shiang Hwang (亞大黃明祥) © 2013 x. signal() 18
6. 8. 4. 1 可串列化(Serializability) Conflicting operation Non-Conflicting operation 將non-serial schedule轉換成 Serial schedule l 交換 T 0的read(B)操作和T 1的write(A)。 Modified by Min-Shiang Hwang (亞大黃明祥) © 2013 25
28
29





























