140 likes | 349 Views
CMT603. Lecture 6 Process Interaction-2. Recap. Critical Section problem Caused by preemptive scheduling These days also caused by concurrent processing on multi CPU machines. Contents. OS solutions Semaphores Uses Classical problem. Semaphores. An Integer – ‘S’ Two atomic operations
E N D
CMT603 Lecture 6 Process Interaction-2
Recap • Critical Section problem • Caused by preemptive scheduling • These days also caused by concurrent processing on multi CPU machines
Contents • OS solutions • Semaphores • Uses • Classical problem
Semaphores • An Integer – ‘S’ • Two atomic operations • Acquire (s) • Many names • P() - ‘proberen’ (to test) • Wait() • Release (s) • V() -’verhogen’ (to increment) • Signal()
Classical Semaphore Operations Acquire (S) while ( S <=0 ) {wait} S--; Release(S) S++; • Busy wait • No fair ordering for waiting processes
Fair Semaphore Operations • Semaphore has • An Integer – ‘S’ • An associated queue Acquire (S) S--; If (S < 0 ){ add process to queue and block process } Release (S) S++; If (S<= 0) { remove process from queue and wakeup the process }
Uses of Semaphores • The value of a mutex lock semaphore is less than or equals to 1. • It is used to control access to the critical section for a process • The value of a counting semaphore can range over an unrestricted domain • It is used to control access to a given resource consisting a finite number of instances
Critical Section SolutionUsing Semaphores Acquire (S) S--; If (S < 0 ){ add process to queue and block process } Release (S) S++; If (S<= 0) { remove process from queue and wakeup the process } Process 1 acquire (S); Critical section; release (S); Process 2 acquire (S); Critical section; release (S); Look up Edsger Dijkstra
Multiple Resource Instances • A resource with 3 instances • Initialise s = 3 Process n acquire (S); Critical section; release (S);
The Bounded-Buffer Problem(A Classic) • Producer and Consumer Problem • Assume we have a bounded-buffer with fixed size of N. The producer adds items to the buffer and the consumer takes items from the buffer out in Count 1 2 3 N-2 N-1
A Solution Using Semaphores • EmptyBuffers • FullBuffers • Mutex Producer acquire(emptyBuffers) acquire(mutex) add an item to the buffer pool release(mutex) release(fullBuffers) Consumer acquire(fullBuffers) acquire(mutex) remove item from buffer pool release(mutex) release(emptyBuffers)
An Example Consumer acquire(fullBuffers) acquire(mutex) remove item from buffer pool release(mutex) release(emptyBuffers) Producer acquire(emptyBuffers) acquire(mutex) add an item to the buffer pool release(mutex) release(fullBuffers) • fullBuffers • emptyBuffers • mutex
Summary OS solutions • Semaphores • Uses • Classical problem • P.S. Assembly language CW due Week 11 in this lecture. • & Matt may be a bit late for lab 15:10.
Next lecture • Multiple resources Process n acquire (R1); acquire (R2); Critical section; release (R2); release (R1);