6.1 Silberschatz, Galvin and Gagne ©2009 perating System Concepts with Java – 8 th Edition Module 6: Process Synchronization
Jan 19, 2018
6.1 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Module 6: Process Synchronization
6.2 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Semaphore Implementation with no Busy waiting
Processes block themselves instead of busy waiting Unblocked when some other process releases
semaphore Maintain a waiting queue for each semaphore
value (of type integer) pointer to next record in the list
Two operations: block – place the process invoking the operation on the
appropriate waiting queue. wakeup – remove one of processes in the waiting queue and
place it in the ready queue.
6.3 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Semaphore Implementation with no Busy waiting (Cont.)
Implementation of acquire():
Implementation of release():
6.4 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Semaphore Implementation with no Busy waiting (Contd.)
Semaphore value can be negative Can use any order for waking up blocked processes
Some might cause unbounded wait Important to ensure atomicity of acquire() and
release() Disable interrupts during acquire() and release()
Works for single processor environments
Use software solutions to the critical sections problem
6.5 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Deadlocks
Deadlock -- Two or more processes are waiting indefinitely for an event that can be triggered by only one of the waiting processes
Let S and Q be two semaphores initialized to 1
Solutions to be discussed in next chapter Starvation – Process waiting indefinitely within a
semaphore
6.6 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Priority Inversion
Semaphores can mess up process priorities A lower priority process may hold a semaphore that a higher
priority process needs A more complex scenario
Low, medium and high priority processes Low priority process holds a semaphore, high priority process needs
to acquire Low priority process is pre-empted by medium priority process that
does not need the semaphore
Priority-Inheritance protocol A process holding a semaphore will inherit the highest priority that is
waiting on the resource
6.7 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Classical Problems of Synchronization
Bounded-Buffer Problem
Readers and Writers Problem
Dining-Philosophers Problem
6.8 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Bounded-Buffer Problem
Buffer with ‘N’ slots Each slot holds one item Producers continuously generate items and want to
insert into the buffer Must wait if buffer has no empty slots
Consumers continuously remove items from the buffer Must wait if buffer has no items
Only one producer/consumer should be accessing the buffer at any given time
6.9 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Bounded-Buffer Solution Outline
Semaphore mutex initialized to the value 1mutex is for synchronization (prevent simultaneous
access to shared buffer)
Semaphore full initialized to the value 0 full indicates how many items are there in the buffer
Semaphore empty initialized to the value Nempty indicates number of empty slots in buffer
6.10 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Bounded-buffer producer
6.11 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Bounded-buffer consumer
6.12 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Bounded-buffer factory
6.13 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Bounded-Buffer
6.14 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Bounded-Buffer insert()
6.15 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Bounded-buffer remove()
6.16 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Readers-Writers Problem
A data set is shared among a number of concurrent processes Readers – only read the data set; they do not perform any
updates Writers – can both read and write
Problem Allow multiple readers to read at the same time Only single writer can access the shared data at the
same time No simultaneous reads and writes allowed
6.17 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Readers-Writers Solution
readerCount – Integer indicating how many readers are currently reading the shared data Manipulated only by readers Initialized to zero
Semaphore mutex initialized to 1 Synchronizing access to the readerCount variable (ensuring only
one reader is manipulating readerCount) Semaphore db initialized to 1
Synchronizing access to the data item Ensuring only one writer is in the critical region or any number of
readers
6.18 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Readers-Writers Problem
Interface for read-write locks
6.19 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Writer Structure
6.20 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Reader Structure
6.21 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Shared DB
6.22 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
ReadLock Methods
6.23 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
WriteLock Methods
6.24 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Dining Philosophers Problem
5 philosopher seated on a round table Each philosopher is in a loop of alternate thinking
and eating 5 chopsticks
Each philosopher has one on each side Philosopher can eat when she has two chopsticks
Picks up one chopstick after anotherPuts them back one after another
Array of semaphores (1 for each chopstick)
6.25 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Dining-Philosophers Problem
Semaphore chopStick [5] initialized to 1
6.26 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Philosopher Structure
6.27 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Deadlocks
What if all philosophers acquire their right (or left) chopstick
Nobody can progress – Deadlock !!! Solutions:
Allow philosopher to pick up chopsticks if both are available (i.e., guard the array of semaphores in a critical section)
Asymmetric solution – Odd philosophers acquire left chopstick first whereas even philosophers acquire right chopstick first
6.28 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Problems with Semaphores
Correct use of semaphore operations:
Correct mutex.acquire() …. mutex.release()
Incorrect mutex.acquire () or mutex.release() (or both)
Omitting either mutex.acquire() or mutex.release()
6.29 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
Monitors
Refer to the handout given in class. If you do not have the handout, please see the instructor.
6.30 Silberschatz, Galvin and Gagne ©2009Operating System Concepts with Java – 8th Edition
End of Chapter 6