1 / 37

Outline

Outline. Basic Synchronization Principles - continued Review Bounded-Buffer problem Readers-writers problem Dining-philosophers problem Semaphore implementations Issues with semaphores Monitors. - Please pick up Homework #2 from the front. Bounded-Buffer using Semaphores.

Télécharger la présentation

Outline

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Outline • Basic Synchronization Principles - continued • Review • Bounded-Buffer problem • Readers-writers problem • Dining-philosophers problem • Semaphore implementations • Issues with semaphores • Monitors - Please pick up Homework #2 from the front

  2. Bounded-Buffer using Semaphores COP4610

  3. Bounded-Buffer using Semaphores – cont. COP4610

  4. Bounded-Buffer using Semaphores – cont. COP4610

  5. The Readers-Writers Problem • A resource is shared among readers and writers • A reader process can share the resource with any other reader process but not with any writer process • A writer process requires exclusive access to the resource whenever it acquires any access to the resource COP4610

  6. First and Second Policy COP4610

  7. Readers-writers with active readers – cont. COP4610

  8. Readers-writers with an active writer – cont. COP4610

  9. Readers-writers with an active writer – cont. COP4610

  10. Dining-Philosophers Problem • Shared data varchopstick: array [0..4] ofsemaphore; (=1 initially) COP4610

  11. Dining-Philosophers Problem - cont. • Philosopher i: repeat wait(chopstick[i]) wait(chopstick[i+1 mod 5]) … eat … signal(chopstick[i]); signal(chopstick[i+1 mod 5]); … think … untilfalse; COP4610

  12. Semaphore Implementation COP4610

  13. Semaphore Implementation – cont. • Binary semaphore through test-and-set instruction COP4610

  14. Semaphore Implementation – cont. COP4610

  15. Semaphore Implementation – cont. • Semaphores implemented using interrupt disabling and test-and-set require busy waiting • This type of semaphores is often called spinlock COP4610

  16. Semaphore Implementation – cont. • Define a semaphore as a structure typedef struct { int value; queue L; } semaphore; • Assume two simple operations: • block suspends the process that invokes it. • wakeup(P) resumes the execution of a blocked process P. COP4610

  17. Semaphore Implementation - cont. • Semaphore operations now defined as P(S): S.value = S.value– 1; ifS.value < 0 then begin add this process to S.L;block; end; COP4610

  18. Semaphore Implementation - cont. V(S): S.value = S.value + 1; ifS.value  0 then begin remove a process P from S.L;wakeup(P); end; COP4610

  19. Semaphore Implementation - cont. • Semaphores are resources in the above implementation COP4610

  20. Constraints of Acceptable Solutions • An acceptable solution to the critical section problem needs to meet the following constraints • Mutual exclusion • Progress: If a critical section is free, a set of processes are trying to enter the critical section, only those processes participate in the selection of the next process to enter the critical section and the selection cannot be postponed indefinitely • Bounded waiting: After a process requests entry into its critical section, only a bounded number of other processes may be allowed to enter their related critical sections before the original process enters its critical section COP4610

  21. Issues Using Semaphores • 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 P0P1 P(S); P(Q); P(Q); P(S);   V(S); V(Q); V(Q) V(S); • Starvation – indefinite blocking • A process may never be removed from the semaphore queue in which it is suspended. COP4610

  22. Active and Passive Semaphores • There is a subtle problem related to semaphore implementation • Bounded waiting may not be satisfied • when the passive V operation is used where the implementation increments the semaphore with no opportunity for a context switch COP4610

  23. Active and Passive Semaphores – cont. COP4610

  24. Active and Passive Semaphores – cont. • Active semaphores • In contrast to passive semaphores, a yield or similar procedure will be called after incrementing the semaphore for a context switch in a V operation implementation COP4610

  25. Monitors • High-level synchronization construct that allows the safe sharing of an abstract data type among concurrent processes. classmonitor { variable declarations semaphore mutex = 1; public P1 :(…) { P(mutex); ........ V(mutex); }; ........ } COP4610

  26. Monitors – cont. • To allow a process to wait within the monitor, a condition variable must be declared, as condition x, y; • Condition variable can only be used with the operations wait and signal. • The operation x.wait;means that the process invoking this operation is suspended until another process invokes x.signal; • The x.signal operation resumes exactly one suspended process. If no process is suspended, then the signal operation has no effect. COP4610

  27. Bounded buffer monitor monitor BoundedBufferType {private: BufferItem * buffer; int NumberOfBuffers; int next_in, nextout; int current_size; condition NotEmpty, NotFull;public: BoundedBufferType( int size ) { buffers = new BufferItem[size]; NumberOfBuffers = size; next_in = 0; next_out = 0; current_size = 0; } COP4610

  28. Bounded buffer monitor – cont. void Put( BufferItem item ) { if( current_size == NumberOfBuffers ) wait( NotFull ); buffer[next_in] = item; next_in = (next_in+1) % NumberOfBuffers; if( ++current_size == 1 ) signal( NotEmpty ); } BufferItem Get( void ) { if( current_size == 0 ) wait( NotEmpty ); BufferItem item = buffer[next_out]; next_out = (next_out+1) % NumberOfBuffers; if( --current_size == NumberOfBuffers-1 ) signal( NotFull ); return item; }} COP4610

  29. Using a bounded buffer monitor BoundedBufferType BoundedBuffer;int main() { // the Producer while( 1 ) { BufferItem item = ProduceItem(); BoundedBuffer.Put( item ); }}int main() { // the Consumer while( 1 ) { BufferItem item = BoundedBuffer.Get(); ConsumeItem( item ); }} COP4610

  30. Readers-writers through monitor COP4610

  31. Readers-writers through monitor – cont. COP4610

  32. Readers-writers through monitor – cont. COP4610

  33. Traffic Synchronization COP4610

  34. Dining-philosopher Monitor COP4610

  35. Dining-philosopher Monitor – cont. COP4610

  36. Thread Synchronization in Java • Synchronized • Only one synchronized method for a particular object can be called • Each object contains a monitor, which is automatically part of an object COP4610

  37. Summary • Processes that share data need to be synchronized • Otherwise, a race condition may exist • Semaphores are the basic mechanism underlying synchronization • can be used to solve process synchronization problems • need to be implemented carefully • Monitor is an abstract data type • For mutual exclusion COP4610

More Related