Outline

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

description

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. - PowerPoint PPT Presentation

Transcript of Outline

Page 1: 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

Page 2: Outline

04/19/23 COP4610 2

Bounded-Buffer using Semaphores

Page 3: Outline

04/19/23 COP4610 3

Bounded-Buffer using Semaphores – cont.

Page 4: Outline

04/19/23 COP4610 4

Bounded-Buffer using Semaphores – cont.

Page 5: Outline

04/19/23 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

Page 6: Outline

04/19/23 COP4610 6

First and Second Policy

Page 7: Outline

04/19/23 COP4610 7

Readers-writers with active readers – cont.

Page 8: Outline

04/19/23 COP4610 8

Readers-writers with an active writer – cont.

Page 9: Outline

04/19/23 COP4610 9

Readers-writers with an active writer – cont.

Page 10: Outline

04/19/23 COP4610 10

Dining-Philosophers Problem

• Shared data var chopstick: array [0..4] of semaphore;(=1 initially)

Page 11: Outline

04/19/23 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 …

until false;

Page 12: Outline

04/19/23 COP4610 12

Semaphore Implementation

Page 13: Outline

04/19/23 COP4610 13

Semaphore Implementation – cont.

• Binary semaphore through test-and-set instruction

Page 14: Outline

04/19/23 COP4610 14

Semaphore Implementation – cont.

Page 15: Outline

04/19/23 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

Page 16: Outline

04/19/23 COP4610 16

Semaphore Implementation – cont.

• Define a semaphore as a structuretypedef 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.

Page 17: Outline

04/19/23 COP4610 17

Semaphore Implementation - cont.

• Semaphore operations now defined as

P(S): S.value = S.value – 1;

if S.value < 0

then begin

add this process to S.L;block;

end;

Page 18: Outline

04/19/23 COP4610 18

Semaphore Implementation - cont.

V(S):

S.value = S.value + 1;

if S.value 0

then begin

remove a process P from S.L;wakeup(P);

end;

Page 19: Outline

04/19/23 COP4610 19

Semaphore Implementation - cont.

• Semaphores are resources in the above implementation

Page 20: Outline

04/19/23 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

Page 21: Outline

04/19/23 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 1P0 P1

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.

Page 22: Outline

04/19/23 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

Page 23: Outline

04/19/23 COP4610 23

Active and Passive Semaphores – cont.

Page 24: Outline

04/19/23 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

Page 25: Outline

04/19/23 COP4610 25

Monitors

• High-level synchronization construct that allows the safe sharing of an abstract data type among concurrent processes.

class monitor {

variable declarations semaphore mutex = 1;

public P1 :(…) { P(mutex); ........ V(mutex);

}; ........

}

Page 26: Outline

04/19/23 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.

Page 27: Outline

04/19/23 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; }

Page 28: Outline

04/19/23 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; }}

Page 29: Outline

04/19/23 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 ); }}

Page 30: Outline

04/19/23 COP4610 30

Readers-writers through monitor

Page 31: Outline

04/19/23 COP4610 31

Readers-writers through monitor – cont.

Page 32: Outline

04/19/23 COP4610 32

Readers-writers through monitor – cont.

Page 33: Outline

04/19/23 COP4610 33

Traffic Synchronization

Page 34: Outline

04/19/23 COP4610 34

Dining-philosopher Monitor

Page 35: Outline

04/19/23 COP4610 35

Dining-philosopher Monitor – cont.

Page 36: Outline

04/19/23 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

Page 37: Outline

04/19/23 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