Download presentation
Presentation is loading. Please wait.
Published byGustavo Kittredge Modified over 9 years ago
1
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
2
Quick Review What is a race condition? What is the name of the place in a program where race conditions are possible? 4/21/20152Dr Alain Beaulieu
3
Outline Mutual Exclusion without Busy Waiting: sleep() and wakeup() The Producer-Consumer Problem Semaphores Solving the Producer-Consumer problem with Semaphores Mutexes 4/21/20153Dr Alain Beaulieu
4
sleep() and wakeup() Recall: Peterson’s Solution 4/21/20154Dr Alain Beaulieu
5
sleep() and wakeup() Peterson’s solution (as well as the TSL instruction we examined) uses Busy Waiting and wastes many CPU cycles that could be applied to other work Other, unexpected effects possible: Priority Inversion: process L is in its critical region. Process H becomes ready to run and tries to enter its critical region. As a higher priority process, it busy waits forever! 4/21/20155Dr Alain Beaulieu
6
sleep() and wakeup() Instead of busy waiting, what if a process can be suspended while waiting to enter its critical region? Create two system calls : sleep() and wakeup() sleep() causes the process to block until awakened by another process wakeup(procID) wakes up another process with the specified identification 4/21/20156Dr Alain Beaulieu
7
The Producer-Consumer Problem What problem exists here? 4/21/20157Dr Alain Beaulieu
8
(Square for notes) 4/21/20158Dr Alain Beaulieu
9
The Producer-Consumer Problem A way exists to fix this problem: add a wakeupwaiting bit that is set when an already awake process is told to wake up A process that tries to sleep will instead decrement this bit instead of sleeping if the bit is set Fixes this particular problem. What about with 2 consumers. 10? 1000? A trillion trillion? 4/21/20159Dr Alain Beaulieu
10
Semaphores A new variable type, a semaphore, was introduced in 1965 (by Dijkstra) to save wakeups for future use The semaphore can have the value 0, indicating no wakeups are saved, or some positive value, indicating the number of wakeups accumulated Use two operations called down() and up() to operate on the semaphores 4/21/201510Dr Alain Beaulieu
11
Semaphores Semaphore Operation: The down() operation checks to see if the semaphore is greater than 0. If so, decrements the value and continues If not, the process that called down() is put to sleep Checking the value, changing it, and going to sleep (if required) is an indivisible atomic action This indivisibility is essential in solving the race condition! 4/21/201511Dr Alain Beaulieu
12
Semaphores Semaphore Operation: Conversely, the up() operation attempts to increment the semaphore. If any processes were sleeping on the semaphore, one is awakened at random and allowed to continue Note that the semaphore value will still be 0, but there will be less processes sleeping! If no processes were sleeping then the value is incremented; no process blocks on an up() How can semaphores solve the Producer- Consumer problem? 4/21/201512Dr Alain Beaulieu
13
4/21/201513Dr Alain Beaulieu
14
Solving Producer-Consumer Note we have used semaphores for two different purposes: The mutex semaphore is to guarantee mutual exclusion in the access of a shared resource A semaphore with only two states is also called a binary semaphore The full/empty semaphores are used for synchronization; they guarantee the occurrence or non-occurrence of certain even sequences ie: The producer stops running when the buffer is full and the consumer stops running when the buffer is empty 4/21/201514Dr Alain Beaulieu
15
Solving Producer-Consumer To work, the functions up() and down() must be implemented as atomic actions... everything is based on this premise Typically achieved by implementing them as system calls where the OS disables all interrupts This is fine as the operation should be only a few instructions...just the semaphore is being updated, rather than executing an entire critical region as was previously discussed 4/21/201515Dr Alain Beaulieu
16
Mutexes If the counting ability of the semaphore is not needed, then a simpler form called the mutex can be used Good only for managing mutual exclusion; they don’t communicate other information This simplicity requires only user space commands if a TSL instruction is available A mutex has two states: locked and unlocked A thread that wants access to a critical region calls mutex_lock() 4/21/201516Dr Alain Beaulieu
17
Mutexes As the code shows, if the mutex is not locked, the thread returns and enters its critical region. Otherwise it yields the thread to allow another thread CPU access 4/21/201517Dr Alain Beaulieu
18
Mutexes Wait, isn’t that the same as enter_region() ? What are the two major advantages of the mutex over enter_region() ? 4/21/201518Dr Alain Beaulieu
19
Quiz Time! Questions? 4/21/201519Dr Alain Beaulieu
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.