【正文】
限制的整型值) . ? Binary semaphore – integer value can range only between 0 and 1。 V(Q) V(S)。 ? ? V(S)。 P(Q)。 Semaphore as a General Synchronization Tool ? Execute B in Pj only after A executed in Pi ? Use semaphore flag initialized to 0 ? Code: Pi Pj ? ? A wait(flag) signal(flag) B Deadlock and Starvation ? Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes(死鎖 – 兩個或多個進(jìn)程無限期地等待一個事件的發(fā)生,而該事件正是由其中的一個等待進(jìn)程引起的) . ? Let S and Q be two semaphores initialized to 1( S和 Q是兩個初值為 1的信號量) P0 P1 P(S)。 wakeup(P)。 signal(S): ++。系統(tǒng)將調(diào)度另一進(jìn)程占用處理機(jī)。 block。 ? Assume two simple operations: ? block suspends the process that invokes it. ? wakeup(P) resumes the execution of a blocked process P. Implementation ? Semaphore operations now defined as wait(S): 。 } semaphore。 Semaphore Implementation ? Define a semaphore as a record typedef struct { int value。 critical section signal(mutex)。 Critical Section of n Processes ? Shared data: semaphore mutex。 S。 critical section lock = false。 ? Process Pi do { key = true。 } ? Compare the value of a memory location with an old value If the same, replace with a new value Mutual Exclusion with Swap ? Shared data (initialized to false): boolean lock。 a = b。a, boolean amp。 critical section lock = false。 } ? Testandset does two things Test a lock Set the lock Mutual Exclusion with TestandSet ? Shared data: boolean lock = false。 tqrget = true。 Synchronization Hardware ? Test and modify the content of a word atomically . boolean TestAndSet(boolean amp。 } critical section number[i] = 0。amp。 j++) { while (choosing[j]) 。 for (j = 0。 number[i] = max(number[0], number[1], …, number [n – 1])+1。 int number[n]。 else Pj is served first. ? The numbering scheme always generates numbers in increasing order of enumeration。 ? Meets all three requirements。 critical section flag [i] = false。 turn = j。 remainder section } while (1)。 while (flag[j]) 。 ? Satisfies mutual exclusion, but not progress Algorithm 2 ? Shared variables ? boolean flag[2]。 critical section turn = j。 ? Processes may share some mon variables to synchronize their actions. Algorithm 1 ? Shared variables: ? int turn。臨界區(qū)雖沒有進(jìn)程執(zhí)行,但有些進(jìn)程需要進(jìn)入臨界區(qū),不能無限期地延長下一個要進(jìn)入臨界區(qū)進(jìn)程的等待時(shí)間) . 3. Bounded Waiting. A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted(有限等待。 counter。 counter。 /* do nothing */ nextConsumed = buffer[out]。 } BoundedBuffer ? Consumer process item nextConsumed。 in = (in + 1) % BUFFER_SIZE。 while (1) { while (counter == BUFFER_SIZE) 。 int counter = 0。 int in = 0。Module 7: Process Synchronization ? Background(背景) ? The CriticalSection Problem (臨界區(qū)問題) ? Synchronization Hardware (同步的硬件實(shí)現(xiàn)) ? Semaphores (信號量) ? Classical Problems of Synchronization( 經(jīng)典同步問題) ? Monitors (管程) ? Synchronization in Solaris 2 ( Solaris 2的同步機(jī)制) ? Synchronization in Windows NT ( Windows NT的同步機(jī)制) Background ? Concurrent access to shared data may result in data inconsistency(對共享數(shù)據(jù)的并發(fā)訪問可能導(dǎo)致數(shù)據(jù)的不一致性) . ? Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes(要保持?jǐn)?shù)據(jù)的一致性,就需要一種保證并發(fā)進(jìn)程的正確執(zhí)行順序的機(jī)制) . ? Sharedmemory solution to boundedbutter problem (Chapter 4) has a race condition on the class data count ( [第 4章中 ]解決有界緩沖區(qū)問題的共享內(nèi)存方法的數(shù)據(jù)count 為 競爭條件) BoundedBu