Study Material
Study Material
Study Material
SPRING 2013
Lecture 7: Synchronization
Prof. Alan Mislove ([email protected])
Background
Concurrent access to shared data may result in data inconsistency
Maintaining data consistency requires mechanisms to ensure the orderly execution
of cooperating processes
Suppose that we wanted to provide a solution to the consumer-producer problem
that fills all the buers. We can do so by having an integer count that keeps track of
the number of full buers. Initially, count is set to 0. It is incremented by the
producer after it produces a new buer and is decremented by the consumer after it
consumes a buer.
Producer
Consumer
while (true) {
while (true)
while (counter == 0)
/*
produce an item */
; // do nothing
nextConsumed =
; // do nothing
buffer[out];
counter--;
in = (in + 1) % BUFFER_SIZE;
counter++;
/*
Race Condition
counter++ could be implemented as
register1 = counter
register1 = register1 + 1
counter = register1
counter-- could be implemented as
register2 = counter
register2 = register2 - 1
count = register2
Consider this execution interleaving with count = 5 initially:
S0: producer execute register1 = counter {register1 = 5}
S1: producer execute register1 = register1 + 1 {register1 = 6}
S2: consumer execute register2 = counter {register2 = 5}
S3: consumer execute register2 = register2 - 1 {register2 = 4}
S4: producer execute counter = register1 {count = 6 }
S5: consumer execute counter = register2 {count = 4}
Critical Section
General structure of process pi is
Petersons Solution
Two process solution
Assume that the LOAD and STORE 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!
Provable that
1.
2.
3.
Synchronization Hardware
Many systems provide hardware support for critical section code
Uniprocessors could disable interrupts
Currently running code would execute without preemption
Generally too inecient on multiprocessor systems
Operating systems using this not broadly scalable
Modern machines provide special atomic hardware instructions
Atomic = non-interruptable
Either test memory word and set value
Or swap contents of two memory words
10
Solution to Critical-section
Problem Using Locks
do {
acquire lock
critical section
release lock
remainder section
} while (TRUE);
11
TestAndSet Instruction
Definition:
boolean TestAndSet (boolean *target)
{
boolean rv = *target;
*target = TRUE;
return rv:
}
12
critical section
lock = FALSE;
//
remainder section
} while (TRUE);
13
Swap Instruction
Definition:
void Swap (boolean *a, boolean *b)
{
boolean temp = *a;
*a = *b;
*b = temp:
}
14
critical section
lock = FALSE;
//
remainder section
} while (TRUE);
15
16
Semaphore
Synchronization tool that does not (necessarily) require busy waiting
Semaphore S integer variable
Two standard operations modify S: wait() and signal()
Originally called P() and V()
Less complicated
Can only be accessed via two indivisible (atomic) operations
wait (S) {
while S <= 0
; // no-op
S--;
}
signal (S) {
S++;
}
17
Semaphore as
General Synchronization Tool
Counting semaphore integer value can range over an unrestricted domain
Binary semaphore integer value can range only between 0
and 1; can be simpler to implement
Also known as mutex locks
Can implement a counting semaphore S as a binary semaphore
Provides mutual exclusion
Semaphore mutex;
//
initialized to 1
do {
wait (mutex);
// Critical Section
signal (mutex);
// remainder section
} while (TRUE);
18
Semaphore Implementation
Must guarantee that no two processes can execute wait () and signal () on the same semaphore at the same
time
Thus, 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
19
20
Implementation of signal:
signal(semaphore *S) {
S->value++;
if (S->value <= 0) {
remove a process P from S->list;
wakeup(P);
}
}
21
Bounded-Buer Problem
N buers, each can hold one item
Semaphore mutex initialized to the value 1
Semaphore full initialized to the value 0
Semaphore empty initialized to the value N
22
{
//
produce an item
wait (empty);
wait (mutex);
//
buffer
signal (mutex);
signal (full);
} while (TRUE);
23
buffer
signal (mutex);
signal (empty);
//
} while (TRUE);
24
Monitors
A high-level abstraction that provides a convenient and eective 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 () { . }
procedure Pn () {}
Initialization code () { }
}
25
P1
wait (S);
wait (Q);
wait (Q);
wait (S);
signal (S);
signal (Q);
signal (Q);
signal (S);
26