Lecture 05 - Synchronization (Part 1)
Lecture 05 - Synchronization (Part 1)
Synchronization
Objectives Outline
• To introduce the critical-section problem • Background
• The Critical-Section Problem
• Critical section solutions can be used to ensure • Peterson’s Solution
the consistency of shared data • Synchronization Hardware
• Semaphores
• To present both software and hardware
solutions of the critical-section problem
2
Background
Shared Data
3
Producer Consumer Problem Revisited
• Below is a solution to the consumer-producer problem that fills all the slots of the shared buffer.
• Use an integer count to keep track of the number of full slots.
• Initially, count is set to 0. It is incremented by the producer after it puts a new item and is
decremented by the consumer after it retrieves an item from the buffer.
count
Producer Consumer
Shared Buffer
Producer ConSUMER
5
A possible Problem: race condition
– Assume the consumer has just retrieved an item from buffer and is about the decrement the
count.
– Namely: Assume producer and consumer is now about to execute count++ and count--
statements.
6
Producer Consumer
or
Producer Consumer
7
Race Condition
8
Race Condition
register1 Count
PRODUCER (count++)
6
5 5
4
6
register1 = count
register1 = register1 + 1
register2 count = register1
5
4
CONSUMER (count--)
register2 = count
register2 = register2 – 1
CPU count = register2
6
Main Memory
9
Interleaved Execution sequence
10
• Count value may be 4, 6, or 5 in various runs.
• But it should be 5 (as a result of one increment, one decrement operation)
• Interleaved access to count causes data inconsistency: 4, 5, 6.
• For consistent results (5), either count++ should be executed and finished first, or count– should
be executed and finished. Not interleaved.
• We need to enforce non-interleaved access (atomic) in this case. This means: synchronization
required.
11
Programs and critical sections
• The part of the program (process) that is accessing and changing shared data is called its
critical section
Process 1 Code Process 2 Code Process 3 Code
Change X
Change X
Change Y
Change Y
Change Y
Change X
• Considering a process:
– It may be executing critical section code from time to time
– It may be executing non critical section code (remainder section) other times.
• We should not allow more than one process to be in their critical regions where they are
manipulating the same shared data.
13
Structuring Programs
do {
do {
entry section
critical section
critical section
remainder section
exit section
Entry section will allow only one process to enter and execute critical section code.
14
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 processes that will enter the critical
section next cannot be postponed indefinitely // no deadlock
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 // no starvation of a process
15
Applications and Kernel
• Multi-process applications sharing a file or shared memory segment may face critical section
problems.
• Multi-threaded applications sharing global variables may also face critical section problems.
• Similarly, kernel itself may face critical section problem. It is also a program. It may have critical
sections.
16
Kernel Critical Sections
• While kernel is executing a function x(), a hardware interrupt may arrive and interrupt handler h()
can be run. Make sure that interrupt handler h() and x() do not access the same kernel global
variable. Otherwise race condition may happen.
• While a process is running in user mode, it may call a system call s(). Then kernel starts running
function s(). CPU is executing in kernel mode now. We say the process is now running in kernel
mode (even though kernel code is running).
17
Kernel Critical Sections
• Therefore, we need to solve synchronization and critical section problem for the kernel itself as
well. The same problem appears there as well.
18
Peterson’s Solution
19
Algorithm for Process Pi
do {
flag[i] = TRUE;
turn = j; entry section
} while (1)
20
Two processes executing concurrently
flag[]
Shared Variables turn
21
Synchronization Hardware
• We can use some hardware support (if available) for protecting critical section code
– 1) Disable interrupts? maybe
• Sometimes only (kernel)
• Not on multi-processors
22
Solution to Critical-section Problem Using Locks
Only one process can acquire lock. Others has to wait (or busy loop)
23
Synchronization Hardware
Thread 1 Thread 2
• Therefore we need to use special machine instructions that can do testing and setting atomically or
something like that (like swapping)
– CompareAndSwap instruction
• They can be executed atomically in a multi-processor environment as well (one CPU at a time
executes the instruction: it involves memory access; memory is shared)
25
TestAndSet Instruction
• Is a machine/assembly instruction.
– But here we provide definition of it using a high level language code.
26
Solution using TestAndSet
Solution
We use a shared Boolean variable lock, initialized to false.
do {
while ( TestAndSet (&lock )) entry section
; // do nothing
// critical section
exit_section
lock = FALSE;
// remainder section
} while (TRUE);
27
In assembly
entry_section:
TestAndSet REGISTER, LOCK;
CMP REGISTER, #0 entry section code
JNE entry_section;
RET
exit_section:
move LOCK, #0 exit section code
RET
main:
..
call entry_section;
execute criticial region;
call exit_section;
28
CompareAndSwap Instruction
Definition
29
Solution using CompareAndSwap
Solution
We use a shared Boolean variable lock initialized to FALSE.
entry_section
exit_section
30
Comments
• Use of TestAndSet and CompareAndSwap as explained provides mutual exclusion: 1st property
satisfied
• A process X may be waiting, but we can have the other process Y going into the critical region
repeatedly.
31
Bounded-waiting Mutual Exclusion with TestandSet()
do {
waiting[i] = TRUE;
key = TRUE;
while (waiting[i] && key) entry section code
key = TestAndSet(&lock);
waiting[i] = FALSE;
// critical section
j = (i + 1) % n;
while ((j != i) && !waiting[j])
j = (j + 1) % n;
if (j == i)
exit section code
lock = FALSE;
else
waiting[j] = FALSE;
// remainder section
} while (TRUE);
32
Mutex Locks
acquire_lock (lock);
// Critical section
release_lock (lock);
33
Semaphore
• Less complicated entry and exit sections when semaphores are used
34
Meaning (semantics) of operations
• wait (S):
if S positive
S-- and return
else
block/wait (until somebody wakes you up; then return)
• signal(S):
if there is a process waiting
wake it up and return
else
S++ and return
35
Comments
• Wait body and signal body have to be executed atomically: one process at a time. Hence the
body of wait and signal are critical sections to be protected by the kernel.
• Not that when wait() causes the process to block, the operation is nearly finished (wait body
critical section is done).
• That means another process can execute wait body or signal body
36
Semaphore as General Synchronization Tool
• Binary semaphore – integer value can range only between 0 and 1; can be simpler to implement
– Also known as mutex locks
– Binary semaphores provides mutual exclusion; can be used for the critical section problem.
37
Usage
• A semaphore variable (lets say mutex) can be shared by N processes, and initialized to 1.
do {
wait (mutex);
// Critical Section
signal (mutex);
// remainder section
} while (TRUE);
38
usage: mutual exclusion
Process 0 Process 1
do {
do {
wait (mutex);
wait (mutex);
// Critical Section
// Critical Section
signal (mutex);
signal (mutex);
// remainder section
// remainder section
} while (TRUE);
} while (TRUE);
39
usage: other synchronization problems
P0 P1
… …
S1; Assume we definitely want to
S2;
…. have S1 executed before S2.
….
semaphore x = 0; // initialized to 0
P0 P1
… …
S1; wait (x);
Solution: signal (x); S2;
…. ….
40
Uses of Semaphore: synchronization
Buffer is an array of BUF_SIZE Cells (at most BUF_SIZE items can be put)
Producer Consumer
do { do {
// produce item wait (Full_Cells);
… ….
put item into buffer remove item from buffer
.. ..
signal (Full_Cells); …
} while (TRUE);
} while (TRUE);
41
Consumer/Producer is Synchronized
Full_Cells
BUF_SIZE
Producer
Sleeps
0 time
Consumer
Sleeps
42
Consumer/Producer is Synchronized
BUF_SIZE
times
all items consumed (Ct)
43
usage: resource allocation
• Assume we have a resource that has 5 instances. A process that needs that type of resource
will need to use one instance. We can allow at most 5 process concurrently using these 5
resource instances. Another process (processes) that want the resource need to block. How can
we code those processes?
• Solution:
one of the processes creates and initializes a semaphore to 5.
semaphore x = 5; // semaphore to access resource
wait (x);
…
Each process has to be
….use one instance of the resource…
coded in this manner.
…
signal (x);
44
Semaphore Implementation
• Must guarantee that for a semaphore, no two processes can execute wait() and signal()
critical sections at the same time.
– Critical section in wait / signal: part that is updating the integer value
• Kernel can guarantee this.
– By disabling interrupts in a single CPU system
– By use of spin locks in a multi-processor system
• Semaphore data structure:
typedef struct {
int value; // semaphore value
struct process *list; // semaphore wait queue
} semaphore;
45
Semaphore Implementation
typedef struct {
int value; // semaphore value
struct process *list; // semaphore wait queue
} semaphore;
46
Semaphore Implementation
Implementation of wait:
wait(semaphore *S) {
S->value--;
if (S->value < 0) {
add this process to S->list;
block the process;
Implementation of signal:
} signal(semaphore *S) {
} S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup the process;
}
}
47
Kernel Implementing wait and signal
• The wait and signal operations must be atomic. The integer value is updated.
• No two processes can execute wait() and signal() critical sections at the same time.
• Critical section in wait / signal: part that is updating the integer value
• Critical section is short
• How can the kernel ensure that? It can NOT use semaphores to implement semaphores.
• Kernel can guarantee this.
– By disabling interrupts in a single CPU system
– By use of spin locks in a multi-processor system
48
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);
• Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it
is suspended
• Priority Inversion - Scheduling problem when lower-priority process holds a lock needed by higher-
priority process
49
References
• The slides here are adapted/modified from the textbook and its slides: Operating System
Concepts, Silberschatz et al., 7th & 8th editions, Wiley.
• Operating System Concepts, 7th and 8th editions, Silberschatz et al. Wiley.
• Modern Operating Systems, Andrew S. Tanenbaum, 3rd edition, 2009.
50
End of Chapter