Operating System Concepts Chapter 6 Synchronization waitingiFALSE lockFALSE

  • Slides: 29
Download presentation
Operating System Concepts 作業系統原理 Chapter 6 同步 (Synchronization)

Operating System Concepts 作業系統原理 Chapter 6 同步 (Synchronization)

waiting[i]=FALSE; lock=FALSE Test. And. Set及Swap由硬體製作(不可分方式執行)

waiting[i]=FALSE; lock=FALSE Test. And. Set及Swap由硬體製作(不可分方式執行)

6. 5. 2 製作 q 號誌定義都有一個主要的缺點,那就是它們都需要忙碌等待 (busy waiting)。意即當一個行程置於其臨界區間時,其它欲進入它們的臨 界區間之行程必定在入口的程式碼形成迴路。 n n n Implementation of

6. 5. 2 製作 q 號誌定義都有一個主要的缺點,那就是它們都需要忙碌等待 (busy waiting)。意即當一個行程置於其臨界區間時,其它欲進入它們的臨 界區間之行程必定在入口的程式碼形成迴路。 n n n Implementation of wait: wait(semaphore *S) { S->value--; if (S->value < 0) { add this process to S->list; block(); } } Implementation of signal: signal(semaphore *S) { S->value++; if (S->value <= 0) { remove a process P from S->list; wakeup(P); } }

6. 5. 3 死結和飢餓(Deadlock and Starvation) n n Deadlock – two or more processes

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); . . . signal (S); signal (Q); n n wait (Q); wait (S); . . . signal (Q); signal (S); Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended Priority Inversion - Scheduling problem when lower-priority process holds a lock needed by higher-priority process

6. 6 典型的同步問題 n 6. 6. 1 有限緩衝區問題(Bounded-Buffer Problem) n n N buffers, each

6. 6 典型的同步問題 n 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.

n 6. 6. 2 讀取者-寫入者問題(Readers and Writers Problem) Allow multiple readers to read at

n 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

n 6. 6. 3 哲學家進餐的問題(Dining-Philosophers Problem) n Shared data q Bowl of rice (data

n 6. 6. 3 哲學家進餐的問題(Dining-Philosophers Problem) n Shared data q Bowl of rice (data set) q Semaphore chopstick [5] initialized to 1