CH 4 Process Synchronization
CH 4 Process Synchronization
Synchronization
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013
Objectives
To present the concept of process synchronization.
To introduce the critical-section problem, whose solutions
can be used to ensure the consistency of shared data
To present both software and hardware solutions of the
critical-section problem
To examine several classical process-synchronization
problems
To explore several tools that are used to solve process
synchronization problems
Operating System Concepts – 9th Edition 5.2 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.3 Silberschatz, Galvin and Gagne ©2013
PROCESS The Producer
Consumer Problem
SYNCHRONIZATION
A producer process "produces" information "consumed" by a consumer process.
Here are the variables needed to define the problem:
item buffer[BUFFER_SIZE];
int in = 0; // Location of next input to buffer
int out = 0; // Location of next removal from buffer
int counter = 0; // Number of buffers currently full
Operating System Concepts – 9th Edition 5.4 Silberschatz, Galvin and Gagne ©2013
PROCESS The Producer
Consumer Problem
SYNCHRONIZATION #define BUFFER_SIZE 10
A producer process "produces" typedef struct {
information "consumed" by a consumer process. DATA data;
} item;
item nextProduced; PRODUCER item buffer[BUFFER_SIZE];
int in = 0;
while (TRUE) { int out = 0;
while (counter == BUFFER_SIZE); int counter = 0;
buffer[in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
item nextConsumed;
counter++; CONSUMER
} while (TRUE) {
while (counter == 0);
nextConsumed = buffer[out]; out
= (out + 1) %
BUFFER_SIZE;
counter--;
produce consume }
r r
5
Operating System Concepts – 9th Edition 5.5 Silberschatz, Galvin and Gagne ©2013
Race Condition
counter++ could be implemented as
register1 = counter
register1 = register1 + 1
counter = register1
counter-- could be implemented as
register2 = counter
register2 = register2 - 1
counter = register2
Operating System Concepts – 9th Edition 5.6 Silberschatz, Galvin and Gagne ©2013
PROCESS SYNCHRONIZATION
Critical Sections
A section of code, common to n cooperating processes, in which the
processes may be accessing common variables.
Critical Section Code in which only one process can execute at any one time.
Exit Section The end of the critical section, releasing or allowing others in.
Operating System Concepts – 9th Edition 5.7 Silberschatz, Galvin and Gagne ©2013
Critical Section
Operating System Concepts – 9th Edition 5.8 Silberschatz, Galvin and Gagne ©2013
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 that the processes can
use to cooperate and solve this problem.
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.9 Silberschatz, Galvin and Gagne ©2013
Solution to Critical-Section Problem
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 process 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
Operating System Concepts – 9th Edition 5.10 Silberschatz, Galvin and Gagne ©2013
Peterson’s Solution
Good algorithmic description of solving the critical section problem
Two process solution
it does not require any special hardware
it uses busy waiting (a spinlock)
a simple algorithm that can be run by two processes to ensure mutual
exclusion for one resource (say one variable or data structure)
Assume that the load and store machine-language instructions
are atomic; that is, cannot be interrupted
The two processes share two variables:
int turn;
Boolean flag[2]
The variable turn indicates whose turn it is to enter the critical section
The flag array is used to indicate if a process is ready to enter the
critical section. flag[i] = true implies that process Pi is ready!
To enter its critical section.
Operating System Concepts – 9th Edition 5.11 Silberschatz, Galvin and Gagne ©2013
Algorithm for Process Pi
do {
flag[i] = true;
turn = j;
while (flag[j] && turn = = j); /* do no-op; */
critical section
flag[i] = false;
remainder section
} while (true);
Operating System Concepts – 9th Edition 5.12 Silberschatz, Galvin and Gagne ©2013
Peterson's Algorithm
Shared variables are created and initialized before either process
starts. The shared variables flag[0] and flag[1] are initialized to
FALSE because neither process is yet interested in the critical
section. The shared variable turn is set to either 0 or 1 randomly (or
it can always be set to say 0).
var flag: array [0..1] of Boolean;
turn: 0..1;
% flag[k] means that process[k] is interested in the critical section
flag[0] := FALSE;
flag[1] := FALSE;
turn := random(0..1)
Operating System Concepts – 9th Edition 5.13 Silberschatz, Galvin and Gagne ©2013
Peterson's Algorithm
After initialization, each process, which is called process i in the code (the
other process is process j), runs the following code:
repeat
flag[i] := TRUE;
turn := j;
j
while (flag[ ] and turn=j) do no-op;
/* else*/
Critical section
flag[i] := FALSE;
Remainder section
until FALSE;
Information common to both processes:
turn = 0
flag[0] = FALSE
flag[1] = FALSE
Operating System Concepts – 9th Edition 5.14 Silberschatz, Galvin and Gagne ©2013
Peterson's Algorithm example
Process 0 Process 1
i = 0, j = 1 i = 1, j = 0
flag[0] := TRUE
turn := 1
check (flag[1] = TRUE and turn = 1)
- Condition is false because flag[1] = FALSE
- Since condition is false, no waiting in while loop
- Enter the critical section
- Process 0 happens to lose the processor
flag[1] := TRUE
turn := 0
check (flag[0] = TRUE and turn = 0)
- Since condition is true, it keeps busy waiting until it
loses the processor
- Process 0 resumes and continues until it
finishes in the critical section
- Leave critical section
flag[0] := FALSE
- Start executing the remainder (anything else a
process does besides using the critical section)
- Process 0 happens to lose the processor
check (flag[0] = TRUE and turn = 0)
- This condition fails because flag[0] = FALSE
- No more busy waiting
- Enter the critical section
Operating System Concepts – 9th Edition 5.15 Silberschatz, Galvin and Gagne ©2013
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.16 Silberschatz, Galvin and Gagne ©2013
TestAndndSet Instruction
Definition:
Operating System Concepts – 9th Edition 5.17 Silberschatz, Galvin and Gagne ©2013
Solution using TestAndSet
while (true) {
while ( TestAndSet (&lock ))
; /* do nothing
// critical section
lock = FALSE;
// remainder section
}
Operating System Concepts – 9th Edition 5.18 Silberschatz, Galvin and Gagne ©2013
Swap Instruction
Definition:
Operating System Concepts – 9th Edition 5.19 Silberschatz, Galvin and Gagne ©2013
Solution using Swap
Shared Boolean variable lock initialized to FALSE; Each
process has a local Boolean variable key.
Solution:
while (true) {
key = TRUE;
while ( key == TRUE)
Swap (&lock, &key );
// critical section
lock = FALSE;
// remainder section
}
Operating System Concepts – 9th Edition 5.20 Silberschatz, Galvin and Gagne ©2013
Mutex Locks
Previous solutions are complicated and generally inaccessible
to application programmers
OS designers build software tools to solve critical section
problem
Simplest is mutex lock
Protect a critical section by first acquire() a lock then
release() the lock
Boolean variable indicating if lock is available or not
Calls to acquire() and release() must be atomic
Usually implemented via hardware atomic instructions
But this solution requires busy waiting
This lock therefore called a spinlock
Operating System Concepts – 9th Edition 5.21 Silberschatz, Galvin and Gagne ©2013
Semaphore
A semaphore S is an integer variable that, apart from initialization, is accessed
only through two standard atomic operations: wait() and signal().
i.e., when one process modifies the semaphore value, no other process can
simultaneously modify that same semaphore value.
Synchronization tool that provides more sophisticated ( advanced or complex)
ways (than Mutex locks) for process to synchronize their activities.
Semaphore S – integer variable
Can only be accessed via two indivisible (atomic) operations
wait() and signal()
Originally called P() and V()
Operating System Concepts – 9th Edition 5.22 Silberschatz, Galvin and Gagne ©2013
Semaphore
Definition of the wait() operation
wait(S) {
while (S <= 0)
; // busy wait
S--;
}
Definition of the signal() operation
signal(S) {
S++;
}
Operating System Concepts – 9th Edition 5.23 Silberschatz, Galvin and Gagne ©2013
Semaphore Usage
Counting semaphore – integer value can range over an unrestricted
domain
Counting semaphores can be used to control access to a given resource
consisting of a finite number of instances.
The semaphore is initialized to the number of resources available.
Each process that wishes to use a resource performs a wait() operation
on the semaphore (thereby decrementing the count). When a process
releases a resource, it performs a signal() operation (incrementing the
count).
When the count for the semaphore goes to 0, all resources are being
used.
After that, processes that wish to use a resource will block until the count
becomes greater than 0.
Operating System Concepts – 9th Edition 5.24 Silberschatz, Galvin and Gagne ©2013
Semaphore Usage
Binary semaphore – integer value can range only between 0 and 1
Same as a mutex lock
Can solve various synchronization problems
Consider P1 and P2 that require S1 to happen before S2
Create a semaphore “synch” initialized to 0
P1:
S1;
signal(synch);
P2:
wait(synch);
S2;
Can implement a counting semaphore S as a binary semaphore
Because synch is initialized to 0, P2 will execute S2 only after P1 has invoked
signal(synch), which is after statement S1 has been executed.
Operating System Concepts – 9th Edition 5.25 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);
Suppose that P0 executes wait(S) and then P1 executes wait(Q).When P0
executes wait(Q), it must wait until P1 executes signal(Q).
Similarly, when P1 executes wait(S), it must wait until P0 executes signal(S).
Since these signal() operations cannot be executed, P0 and P1 are
deadlocked.
Operating System Concepts – 9th Edition 5.26 Silberschatz, Galvin and Gagne ©2013
Classic Problems of Synchronization
The Bounded-Buffer Problem
The Readers–Writers Problem
Suppose that a database is to be shared among several concurrent
processes.
Some of these processes may want only to read the database, whereas
others may want to update (that is, to read and write) the database.
Obviously, if two readers access the shared data simultaneously, no
adverse effects will result. However, if a writer and some other process
(either a reader or a writer) access the database simultaneously, chaos
may ensue.
The Dining-Philosophers Problem
Operating System Concepts – 9th Edition 5.27 Silberschatz, Galvin and Gagne ©2013
PROCESS
SYNCHRONIZATION Some Interesting
Problems
THE DINING PHILOSOPHERS PROBLEM:
5 philosophers with 5 chopsticks sit around a circular table. They each want to
eat at random times and must pick up the chopsticks on their right and on their left.
Clearly deadlock is rampant ( and starvation possible.) Several solutions are possible:
Lab assignments
Implement producer-consumer
and dining-philosophers problems.
28
Operating System Concepts – 9th Edition 5.28 Silberschatz, Galvin and Gagne ©2013
Synchronization Examples
Windows
Linux
Operating System Concepts – 9th Edition 5.29 Silberschatz, Galvin and Gagne ©2013
Windows Synchronization
Operating System Concepts – 9th Edition 5.30 Silberschatz, Galvin and Gagne ©2013
Linux Synchronization
Linux:
Prior to kernel Version 2.6, disables interrupts to
implement short critical sections
Version 2.6 and later, fully preemptive
Linux provides:
Semaphores
atomic integers
spinlocks
reader-writer versions of both
On single-cpu system, spinlocks replaced by enabling and
disabling kernel preemption
Operating System Concepts – 9th Edition 5.31 Silberschatz, Galvin and Gagne ©2013
على حضوركم.. شكرا لكم
وحسن تعاونكم
Operating System Concepts – 9th Edition 5.32 Silberschatz, Galvin and Gagne ©2013
End of Chapter 4
Operating System Concepts – 9th Edition Silberschatz, Galvin and Gagne ©2013