CSC520 Chapter 6
CSC520 Chapter 6
principles of
operating
systems
(chapter 6)
Dr. Mohamed Imran Mohamed Ariff
Email : moham588@uitm.edu.my
Tel : +60127031179
Chapter 6: Process
Synchronization
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Chapter 5: Process Synchronization
Background
The Critical-Section Problem
Peterson’s Solution
Synchronization Hardware
Mutex Locks
Semaphores
Classic Problems of Synchronization
Monitors
Synchronization Examples
Alternative Approaches
Operating System Concepts – 9th Edition 5.3 Silberschatz, Galvin and Gagne ©2013
Objectives
Operating System Concepts – 9th Edition 5.4 Silberschatz, Galvin and Gagne ©2013
Background
Processes can execute concurrently
May be interrupted at any time, partially completing
execution
Concurrent access to shared data may result in data
inconsistency
Maintaining data consistency requires mechanisms to ensure
the orderly execution of cooperating processes
Illustration of the problem:
Suppose that we wanted to provide a solution to the
consumer-producer problem that fills all the buffers. We can
do so by having an integer counter that keeps track of the
number of full buffers. Initially, counter is set to 0. It is
incremented by the producer after it produces a new buffer
and is decremented by the consumer after it consumes a
buffer.
Operating System Concepts – 9th Edition 5.5 Silberschatz, Galvin and Gagne ©2013
Producer: consumer problem
Operating System Concepts – 9th Edition 5.6 Silberschatz, Galvin and Gagne ©2013
6.1 Critical Section Problem
Consider system of n processes {p0, p1, … pn-1}
Each process has critical section segment of code
Process may be changing common variables, updating
table, writing file, etc
When one process in critical section, no other may be in its
critical section
Critical section problem is to design protocol to solve this
Each process must ask permission to enter critical section in
entry section, may follow critical section with exit section,
then remainder section
Operating System Concepts – 9th Edition 5.7 Silberschatz, Galvin and Gagne ©2013
Solution to Critical-Section Problem
(in order to solve the critical section problem,
3 requirements needs to be satisfied)
1. Mutual Exclusion - If process Pi is executing in its critical
section, then no other processes can be executing in their
critical sections
2. Progress - If no process is executing in its critical section and
there exist some processes that wish to enter their critical
section, then the selection of the processes that will enter the
critical section next cannot be postponed indefinitely
3. Bounded Waiting - A bound must exist on the number of
times that other processes are allowed to enter their critical
sections after a process has made a request to enter its critical
section and before that request is granted
Assume that each process executes at a nonzero speed
No assumption concerning relative speed of the n
processes
Operating System Concepts – 9th Edition 5.8 Silberschatz, Galvin and Gagne ©2013
Critical-Section Handling in OS
Two approaches depending on if kernel is preemptive or non-
preemptive
Preemptive – allows preemption of process when running
in kernel mode
Non-preemptive – runs until exits kernel mode, blocks, or
voluntarily yields CPU
Essentially free of race conditions in kernel mode
Operating System Concepts – 9th Edition 5.9 Silberschatz, Galvin and Gagne ©2013
Peterson’s Solution (software)
Good algorithmic description of solving the problem (critical
section problem)
ONLY Two process solution
Assume that the load and store machine-language
instructions are atomic; that is, cannot be interrupted
The two processes share two variables:
int turn; -> Which Process’s turn
Boolean flag[2] → Is the process interested to use
(or enter the critical section)
Operating System Concepts – 9th Edition 5.10 Silberschatz, Galvin and Gagne ©2013
6.2 Synchronization Hardware
Many systems provide hardware support for implementing the
critical section code.
All solutions below based on idea of locking
Protecting critical regions via locks
Uniprocessors – could disable interrupts
Currently running code would execute without preemption
Generally too inefficient on multiprocessor systems
Operating systems using this not broadly scalable
Modern machines provide special atomic hardware instructions
Atomic = non-interruptible
Either test memory word and set value
Or swap contents of two memory words
Operating System Concepts – 9th Edition 5.11 Silberschatz, Galvin and Gagne ©2013
6.3 Semaphore Usage
(to solve Consumer & Producer problem)
Operating System Concepts – 9th Edition 5.12 Silberschatz, Galvin and Gagne ©2013
Semaphore Implementation
Must guarantee that no two processes can execute the wait()
and signal() on the same semaphore at the same time
Thus, the implementation becomes the critical section problem
where the wait and signal code are placed in the critical
section
Could now have busy waiting in critical section
implementation
But implementation code is short
Little busy waiting if critical section rarely occupied
Note that applications may spend lots of time in critical sections
and therefore this is not a good solution
Operating System Concepts – 9th Edition 5.13 Silberschatz, Galvin and Gagne ©2013
Semaphore Implementation with no Busy waiting
Operating System Concepts – 9th Edition 5.14 Silberschatz, Galvin and Gagne ©2013
Deadlock and Starvation
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
P0 P1
wait(S); wait(Q);
wait(Q); wait(S);
... ...
signal(S); signal(Q);
signal(Q); signal(S);
Operating System Concepts – 9th Edition 5.15 Silberschatz, Galvin and Gagne ©2013
6.4 Classical Problems of Synchronization
Operating System Concepts – 9th Edition 5.16 Silberschatz, Galvin and Gagne ©2013
Bounded-Buffer Problem
Operating System Concepts – 9th Edition 5.17 Silberschatz, Galvin and Gagne ©2013
Readers-Writers Problem
(a file shared by both reader and writer process)
Operating System Concepts – 9th Edition 5.18 Silberschatz, Galvin and Gagne ©2013
Readers-Writers Problem Variations
Operating System Concepts – 9th Edition 5.19 Silberschatz, Galvin and Gagne ©2013
Dining-Philosophers Problem
Operating System Concepts – 9th Edition 5.20 Silberschatz, Galvin and Gagne ©2013
Problems with Semaphores
Operating System Concepts – 9th Edition 5.21 Silberschatz, Galvin and Gagne ©2013
6.5 Monitors
A high-level abstraction that provides a convenient and effective mechanism for
process synchronization
Abstract data type, internal variables only accessible by code within the
procedure
Only one process may be active within the monitor at a time
But not powerful enough to model some synchronization schemes
monitor monitor-name
{
// shared variable declarations
procedure P1 (…) { …. }
Operating System Concepts – 9th Edition 5.22 Silberschatz, Galvin and Gagne ©2013
Schematic view of a Monitor
Operating System Concepts – 9th Edition 5.23 Silberschatz, Galvin and Gagne ©2013
Monitor with Condition Variables
Operating System Concepts – 9th Edition 5.24 Silberschatz, Galvin and Gagne ©2013
Condition Variables Choices
If process P invokes x.signal(), and process Q is suspended in
x.wait(), what should happen next?
Both Q and P cannot execute in paralel. If Q is resumed, then P
must wait
Options include
Signal and wait – P waits until Q either leaves the monitor or it
waits for another condition
Signal and continue – Q waits until P either leaves the monitor or it
waits for another condition
Both have pros and cons – language implementer can decide
Monitors implemented in Concurrent Pascal compromise
P executing signal immediately leaves the monitor, Q is
resumed
Implemented in other languages including Mesa, C#, Java
Operating System Concepts – 9th Edition 5.25 Silberschatz, Galvin and Gagne ©2013
Resuming Processes within a Monitor
Operating System Concepts – 9th Edition 5.26 Silberschatz, Galvin and Gagne ©2013
End of Chapter 6
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013