module 9 process synchronization
TRANSCRIPT
Operating systemsModule 9
Process synchronizationPART I
1Tami Sorgente, Florida Atlantic University
MODULE 9 – PROCESS SYNCHRONIZATION
Software solutionso Semaphores
2Tami Sorgente, Florida Atlantic University
SEMAPHORE – SOFTWARE SOLUTION
Three operations: Initialization, wait, and signal
Wait and signal control access: indivisible (atomic) operations
binary, counting, complex
Wait (P – proberen “to test”)wait (S) {
while ( S<= 0); // no-op
S--;
}
Signal (V – verhogen “to increment”)signal (S) {
S++ ;
}
3Tami Sorgente, Florida Atlantic University
COMPLEX SEMAPHOREint semaphore S = 1; //binary semaphore for list of sleeping processes
wait(S){
if (semaphore S == 0) { // semaphore is unavailable-
//add process to list - block itself
//atomically add process to a queue of processes waiting for the semaphore }
else {
semaphore S --; // decrement semaphore
}
}
signal(S){
if (semaphore S == 0 && there is a process on the wait queue){//wake up the first process in the queue of waiting processes}
else {
semaphore S ++; //increment semaphore -
}
} 4Tami Sorgente, Florida Atlantic University
SEMAPHORE CONCEPT
5
Global variable with 3 operations◦ Initialization
◦ wait
◦ signal
Values of semaphores:
◦ Binary (only 0 and 1)
◦ Counting (any numerical value)
Types of semaphores:
◦ Standard – busy waiting
◦ Complex – block while waiting
Tami Sorgente, Florida Atlantic University
SEMAPHORE
ANIMATION
6
wait (S) {
while ( S<= 0); // no-op
S--;
}
Signal (S) {
S++ ;
}
Initialization:
Semphore S = 0;
Dr: Signal (S);
Tami Sorgente, Florida Atlantic University
SEMAPHORE
77
wait (S) {
while ( S<= 0); // no-op
S--;
}
Signal (S) {
S++ ;
}
Dr: Signal (S);
Tami Sorgente, Florida Atlantic University
SEMAPHORE
8
wait (S) {
while ( S<= 0); // no-op
S--;
}
Signal (S) {
S++ ;
}
Dr:
Pn: Wait (S);
Tami Sorgente, Florida Atlantic University
SEMAPHORE
99
wait (S) {
while ( S<= 0); // no-op
S--;
}
Signal (S) {
S++ ;
}
Dr:
Pn: Wait (S);
Tami Sorgente, Florida Atlantic University
SEMAPHORE
10
wait (S) {
while ( S<= 0); // no-op
S--;
}
Signal (S) {
S++ ;
}
Dr:
Pn:
P1(wait) P3(wait) P2(wait)
Tami Sorgente, Florida Atlantic University
SEMAPHORE
1111
wait (S) {
while ( S<= 0); // no-op
S--;
}
Signal (S) {
S++ ;
}
Dr:
Pn:
Pn: Signal (S);
P1(wait) P3(wait) P2(wait)
Tami Sorgente, Florida Atlantic University
SEMAPHORE
12
wait (S) {
while ( S<= 0); // no-op
S--;
}
Signal (S) {
S++ ;
}
Dr:
P1: wait(S)
P3(wait) P2(wait)
P1
Tami Sorgente, Florida Atlantic University
COMPLEX SEMAPHORE
13
wait(S){
if (semaphore S == 0) { // block and queue}
else { semaphore S --; }// decrement semaphore
}
signal(S){
if (semaphore S == 0 && there is a process
{//wake up the first process in the queue }
else {semaphore S ++; } //increment semaphore
}
Initialization:
Semphore S = 0;
Dr: Signal (S);
Tami Sorgente, Florida Atlantic University
COMPLEX SEMAPHORE
1414
Dr: Signal (S);
wait(S){
if (semaphore S == 0) { // block and queue}
else { semaphore S --; }// decrement semaphore
}
signal(S){
if (semaphore S == 0 && there is a process
{//wake up the first process in the queue }
else {semaphore S ++; } //increment semaphore
}
Tami Sorgente, Florida Atlantic University
COMPLEX SEMAPHORE
15
Dr:
Pn: Wait (S);
wait(S){
if (semaphore S == 0) { // block and queue}
else { semaphore S --; }// decrement semaphore
}
signal(S){
if (semaphore S == 0 && there is a process
{//wake up the first process in the queue }
else {semaphore S ++; } //increment semaphore
}
Tami Sorgente, Florida Atlantic University
COMPLEX SEMAPHORE
1616
Dr:
Pn: Wait (S);
wait(S){
if (semaphore S == 0) { // block and queue}
else { semaphore S --; }// decrement semaphore
}
signal(S){
if (semaphore S == 0 && there is a process
{//wake up the first process in the queue }
else {semaphore S ++; } //increment semaphore
}
Tami Sorgente, Florida Atlantic University
COMPLEX SEMAPHORE
17
Dr:
Pn:
wait(S){
if (semaphore S == 0) { // block and queue}
else { semaphore S --; }// decrement semaphore
}
signal(S){
if (semaphore S == 0 && there is a process
{//wake up the first process in the queue }
else {semaphore S ++; } //increment semaphore
}
P1(wait/block) P3(wait/ block) P2(wait/block)
Tami Sorgente, Florida Atlantic University
COMPLEX SEMAPHORE
1818
Pn: Signal (S);
wait(S){
if (semaphore S == 0) { // block and queue}
else { semaphore S --; }// decrement semaphore
}
signal(S){
if (semaphore S == 0 && there is a process
{//wake up the first process in the queue }
else {semaphore S ++; } //increment semaphore
}
P1(wait/block) P3(wait/ block) P2(wait/block)
Tami Sorgente, Florida Atlantic University
COMPLEX SEMAPHORE
19
wait(S){
if (semaphore S == 0) { // block and queue}
else { semaphore S --; }// decrement semaphore
}
signal(S){
if (semaphore S == 0 && there is a process
{//wake up the first process in the queue }
else {semaphore S ++; } //increment semaphore
}
P1
P3(wait/ block) P2(wait/block)
Tami Sorgente, Florida Atlantic University
COUNTING SEMAPHORE
20
Semaphore Sr = 3; //complex, counting semaphore
Semaphore S = 1; // standard, binary semaphore
Flag[3] = {0}; //flag for each resource to indicate busy or available
Wait( Sr ); //Semaphore to indicate if there is an available resource
wait( S ); //binary semaphore to let only one process check the flags
int resourceFree; //local variable of first resource that is available
int i = 0; //counter
do while (flagR[ i ] != 0 ) //loop to find the available resource
i + +;
end while;
flagR [ i ] = 1; //set resource as busy
resource Free = i; //assign a value to the resourceFree local variable
signal ( S ); //allow another process to check resource flags
useResource (resourceFree); //use the resource
flagR [ resourceFree ] = 0; //set the resource available when done
Signal (Sr ); //signal, wake up next process or increment semaphoreTami Sorgente, Florida Atlantic University
MODULE 9 – PROCESS SYNCHRONIZATION
Software solutionso Semaphores
21Tami Sorgente, Florida Atlantic University
Operating systemsModule 9
Process synchronizationPART I1
22Tami Sorgente, Florida Atlantic University
MODULE 9 – PROCESS SYNCHRONIZATION
Software solutionso counting semaphore example
23Tami Sorgente, Florida Atlantic University
COUNTING SEMAPHORE
24
Semaphore Sr = 3; //complex, counting semaphore
Semaphore S = 1; // standard, binary semaphore
Flag[3] = {0}; //flag for each resource to indicate busy or available
Wait( Sr ); //Semaphore to indicate if there is an available resource
wait( S ); //binary semaphore to let only one process check the flags
int resourceFree; //local variable of first resource that is available
int i = 0; //counter
do while (flagR[ i ] != 0 ) //loop to find the available resource
i + +;
end while;
flagR [ i ] = 1; //set resource as busy
resource Free = i; //assign a value to the resourceFree local variable
signal ( S ); //allow another process to check resource flags
useResource (resourceFree); //use the resource
flagR [ resourceFree ] = 0; //set the resource available when done
Signal (Sr ); //signal, wake up next process or increment semaphoreTami Sorgente, Florida Atlantic University
INITIALIZATION
25
Semaphore Sr = 3; //complex, counting semaphore
Semaphore S = 1; // standard, binary semaphore
Flag[3] = {0}; //flag for each resource to indicate busy or available
Semaphore Sr Semaphore S
Tami Sorgente, Florida Atlantic University
WAIT AND SIGNAL
26
Semaphore Sr//counting/complex
Semaphore S//binary/standard
wait(Sr){ if (semaphore Sr == 0) { // block and queue}
else { semaphore Sr --; }// decrement semaphore}
signal(Sr){ if (semaphore Sr == 0 && there is a process
{//wake up the first process in the queue }
else {semaphore Sr ++; } //increment semaphore}
wait(S){ while (semaphore S == 0) ; //no op
S --; } // decrement semaphore
signal(S){ S ++; } //increment
Tami Sorgente, Florida Atlantic University
WAIT AND SIGNAL
27
Semaphore Sr//counting/complex
Semaphore S//binary/standard
wait(Sr){ if (semaphore Sr == 0) { // block and queue}
else { semaphore Sr --; }// decrement semaphore}
signal(Sr){ if (semaphore Sr == 0 && there is a process
{//wake up the first process in the queue }
else {semaphore Sr ++; } //increment semaphore}
wait(S){ while (semaphore S == 0) ; //no op
S --; } // decrement semaphore
signal(S){ S ++; } //increment
Tami Sorgente, Florida Atlantic University
WAIT AND SIGNAL
28
Semaphore Sr//counting/complex
Semaphore S//binary/standard
wait(Sr){ if (semaphore Sr == 0) { // block and queue}
else { semaphore Sr --; }// decrement semaphore}
signal(Sr){ if (semaphore Sr == 0 && there is a process
{//wake up the first process in the queue }
else {semaphore Sr ++; } //increment semaphore}
wait(S){ while (semaphore S == 0) ; //no op
S --; } // decrement semaphore
signal(S){ S ++; } //increment
Tami Sorgente, Florida Atlantic University
WAIT AND SIGNAL
29
Semaphore Sr//counting/complex
Semaphore S//binary/standard
wait(Sr){ if (semaphore Sr == 0) { // block and queue}
else { semaphore Sr --; }// decrement semaphore}
signal(Sr){ if (semaphore Sr == 0 && there is a process
{//wake up the first process in the queue }
else {semaphore Sr ++; } //increment semaphore}
wait(S){ while (semaphore S == 0) ; //no op
S --; } // decrement semaphore
signal(S){ S ++; } //increment
Tami Sorgente, Florida Atlantic University
WAIT AND SIGNAL
30
Semaphore Sr//counting/complex
Semaphore S//binary/standard
wait(Sr){ if (semaphore Sr == 0) { // block and queue}
else { semaphore Sr --; }// decrement semaphore}
signal(Sr){ if (semaphore Sr == 0 && there is a process
{//wake up the first process in the queue }
else {semaphore Sr ++; } //increment semaphore}
wait(S){ while (semaphore S == 0) ; //no op
S --; } // decrement semaphore
signal(S){ S ++; } //increment
Tami Sorgente, Florida Atlantic University
WAIT AND SIGNAL
31
Semaphore Sr//counting/complex
Semaphore S//binary/standard
wait(Sr){ if (semaphore Sr == 0) { // block and queue}
else { semaphore Sr --; }// decrement semaphore}
signal(Sr){ if (semaphore Sr == 0 && there is a process
{//wake up the first process in the queue }
else {semaphore Sr ++; } //increment semaphore}
wait(S){ while (semaphore S == 0) ; //no op
S --; } // decrement semaphore
signal(S){ S ++; } //increment
Tami Sorgente, Florida Atlantic University
WAIT AND SIGNAL
32
Semaphore Sr//counting/complex
Semaphore S//binary/standard
wait(Sr){ if (semaphore Sr == 0) { // block and queue}
else { semaphore Sr --; }// decrement semaphore}
signal(Sr){ if (semaphore Sr == 0 && there is a process
{//wake up the first process in the queue }
else {semaphore Sr ++; } //increment semaphore}
wait(S){ while (semaphore S == 0) ; //no op
S --; } // decrement semaphore
signal(S){ S ++; } //increment
Tami Sorgente, Florida Atlantic University
WAIT AND SIGNAL
33
Semaphore Sr//counting/complex
Semaphore S//binary/standard
wait(Sr){ if (semaphore Sr == 0) { // block and queue}
else { semaphore Sr --; }// decrement semaphore}
signal(Sr){ if (semaphore Sr == 0 && there is a process
{//wake up the first process in the queue }
else {semaphore Sr ++; } //increment semaphore}
wait(S){ while (semaphore S == 0) ; //no op
S --; } // decrement semaphore
signal(S){ S ++; } //increment
Tami Sorgente, Florida Atlantic University
WAIT AND SIGNAL
34
Semaphore Sr//counting/complex
Semaphore S//binary/standard
wait(Sr){ if (semaphore Sr == 0) { // block and queue}
else { semaphore Sr --; }// decrement semaphore}
signal(Sr){ if (semaphore Sr == 0 && there is a process
{//wake up the first process in the queue }
else {semaphore Sr ++; } //increment semaphore}
wait(S){ while (semaphore S == 0) ; //no op
S --; } // decrement semaphore
signal(S){ S ++; } //increment
Tami Sorgente, Florida Atlantic University
WAIT AND SIGNAL
35
Semaphore Sr//counting/complex
Semaphore S//binary/standard
wait(Sr){ if (semaphore Sr == 0) { // block and queue}
else { semaphore Sr --; }// decrement semaphore}
signal(Sr){ if (semaphore Sr == 0 && there is a process
{//wake up the first process in the queue }
else {semaphore Sr ++; } //increment semaphore}
wait(S){ while (semaphore S == 0) ; //no op
S --; } // decrement semaphore
signal(S){ S ++; } //increment
Tami Sorgente, Florida Atlantic University
WAIT AND SIGNAL
36
Semaphore Sr//counting/complex
Semaphore S//binary/standard
wait(Sr){ if (semaphore Sr == 0) { // block and queue}
else { semaphore Sr --; }// decrement semaphore}
signal(Sr){ if (semaphore Sr == 0 && there is a process
{//wake up the first process in the queue }
else {semaphore Sr ++; } //increment semaphore}
wait(S){ while (semaphore S == 0) ; //no op
S --; } // decrement semaphore
signal(S){ S ++; } //increment
Tami Sorgente, Florida Atlantic University
WAIT AND SIGNAL
37
Semaphore Sr//counting/complex
Semaphore S//binary/standard
wait(Sr){ if (semaphore Sr == 0) { // block and queue}
else { semaphore Sr --; }// decrement semaphore}
signal(Sr){ if (semaphore Sr == 0 && there is a process
{//wake up the first process in the queue }
else {semaphore Sr ++; } //increment semaphore}
wait(S){ while (semaphore S == 0) ; //no op
S --; } // decrement semaphore
signal(S){ S ++; } //increment
Tami Sorgente, Florida Atlantic University
WAIT AND SIGNAL
38
Semaphore Sr//counting/complex
Semaphore S//binary/standard
wait(Sr){ if (semaphore Sr == 0) { // block and queue}
else { semaphore Sr --; }// decrement semaphore}
signal(Sr){ if (semaphore Sr == 0 && there is a process
{//wake up the first process in the queue }
else {semaphore Sr ++; } //increment semaphore}
wait(S){ while (semaphore S == 0) ; //no op
S --; } // decrement semaphore
signal(S){ S ++; } //increment
Tami Sorgente, Florida Atlantic University
WAIT AND SIGNAL
39
Semaphore Sr//counting/complex
Semaphore S//binary/standard
wait(Sr){ if (semaphore Sr == 0) { // block and queue}
else { semaphore Sr --; }// decrement semaphore}
signal(Sr){ if (semaphore Sr == 0 && there is a process
{//wake up the first process in the queue }
else {semaphore Sr ++; } //increment semaphore}
wait(S){ while (semaphore S == 0) ; //no op
S --; } // decrement semaphore
signal(S){ S ++; } //increment
Tami Sorgente, Florida Atlantic University
WAIT AND SIGNAL
40
Semaphore Sr//counting/complex
Semaphore S//binary/standard
wait(Sr){ if (semaphore Sr == 0) { // block and queue}
else { semaphore Sr --; }// decrement semaphore}
signal(Sr){ if (semaphore Sr == 0 && there is a process
{//wake up the first process in the queue }
else {semaphore Sr ++; } //increment semaphore}
wait(S){ while (semaphore S == 0) ; //no op
S --; } // decrement semaphore
signal(S){ S ++; } //increment
Tami Sorgente, Florida Atlantic University
WAIT AND SIGNAL
41
Semaphore Sr//counting/complex
Semaphore S//binary/standard
wait(Sr){ if (semaphore Sr == 0) { // block and queue}
else { semaphore Sr --; }// decrement semaphore}
signal(Sr){ if (semaphore Sr == 0 && there is a process
{//wake up the first process in the queue }
else {semaphore Sr ++; } //increment semaphore}
wait(S){ while (semaphore S == 0) ; //no op
S --; } // decrement semaphore
signal(S){ S ++; } //increment
Tami Sorgente, Florida Atlantic University
WAIT AND SIGNAL
42
Semaphore Sr//counting/complex
Semaphore S//binary/standard
wait(Sr){ if (semaphore Sr == 0) { // block and queue}
else { semaphore Sr --; }// decrement semaphore}
signal(Sr){ if (semaphore Sr == 0 && there is a process
{//wake up the first process in the queue }
else {semaphore Sr ++; } //increment semaphore}
wait(S){ while (semaphore S == 0) ; //no op
S --; } // decrement semaphore
signal(S){ S ++; } //increment
Tami Sorgente, Florida Atlantic University
WAIT AND SIGNAL
43
Semaphore Sr//counting/complex
Semaphore S//binary/standard
wait(Sr){ if (semaphore Sr == 0) { // block and queue}
else { semaphore Sr --; }// decrement semaphore}
signal(Sr){ if (semaphore Sr == 0 && there is a process
{//wake up the first process in the queue }
else {semaphore Sr ++; } //increment semaphore}
wait(S){ while (semaphore S == 0) ; //no op
S --; } // decrement semaphore
signal(S){ S ++; } //increment
Tami Sorgente, Florida Atlantic University
Operating systemsModule 9
Process synchronizationPART II1
44Tami Sorgente, Florida Atlantic University
MODULE 9 – PROCESS SYNCHRONIZATION
Software solutionso Monitors
Classic Problems of Synchronizationo Producer/ Consumer
o Dining Philosophers
o Sharing multiple instances of a resource
o Readers and writers
45Tami Sorgente, Florida Atlantic University
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
monitor monitor-name
{
// shared variable declarations
procedure Request (…) { …. }
procedure Release(…) {……}
Initialization code (…) { … }
}
}
46Tami Sorgente, Florida Atlantic University
SCHEMATIC VIEW OF A MONITOR
Monitor:
47Tami Sorgente, Florida Atlantic University
CONDITION VARIABLES
condition x, y;
Two operations are allowed on a
condition variable:
o x.wait() – a process that invokes the
operation is suspended until x.signal()
o x.signal() – resumes one of processes (if
any) that invoked x.wait()
If no x.wait() on the variable, then it has no effect
on the variable
48Tami Sorgente, Florida Atlantic University
MONITOR WITH CONDITION VARIABLES
49Tami Sorgente, Florida Atlantic University
CLASSICAL PROBLEMS OF
SYNCHRONIZATION
Classical problems used to test
synchronization schemes
o Bounded-Buffer Problem
Producer/ consumer
o Readers and Writers Problem
o Dining-Philosophers Problem
50Tami Sorgente, Florida Atlantic University
PRODUCER/ CONSUMER (BOUNDED BUFFER)
PRODUCER:
while (true) {/* produce an item in next produced */
while (counter == BUFFER_SIZE) ;
/* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
counter++;
}
CONSUMER:
while (true) {
while (counter == 0) ;
/* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
/* consume the item in next consumed */
}
51Tami Sorgente, Florida Atlantic University
BOUNDED-BUFFER PROBLEM
n buffers, 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
52Tami Sorgente, Florida Atlantic University
BOUNDED BUFFER PROBLEM (CONT.)
The structure of the producer process
do {
.../* produce an item in next_produced
*/
...
wait(empty);
wait(mutex);
.../* add next produced to the buffer */
...
signal(mutex);
signal(full);
} while (true);
53Tami Sorgente, Florida Atlantic University
BOUNDED BUFFER PROBLEM (CONT.)
The structure of the consumer process
Do {
wait(full);
wait(mutex);
.../* remove an item from buffer to
next_consumed */
...
signal(mutex);
signal(empty);
.../* consume the item in next consumed */
...} while (true);
54Tami Sorgente, Florida Atlantic University
DINING-PHILOSOPHERS PROBLEM
Philosophers spend their lives alternating thinking and
eating
Don’t interact with their neighbors, occasionally try to
pick up 2 chopsticks (one at a time) to eat from bowl
o Need both to eat, then release both when done
In the case of 5 philosophers
o Shared data
Bowl of rice (data set)
Semaphore chopstick [5]
initialized to 1
55Tami Sorgente, Florida Atlantic University
DINING-PHILOSOPHERS PROBLEM
ALGORITHM
The structure of Philosopher :do {
wait (chopstick[i] );
wait (chopStick[ (i + 1) % 5] );
// eat
signal (chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think
} while (TRUE);
What is the problem with this algorithm?
56Tami Sorgente, Florida Atlantic University
Dining-Philosophers Problem
Algorithm
57Tami Sorgente, Florida Atlantic University
MONITOR SOLUTION TO DINING PHILOSOPHERS
monitor DiningPhilosophers
{
enum { THINKING; HUNGRY, EATING) state [5] ;
condition self [5];
void pickup (int i) {
state[i] = HUNGRY;
test(i);
if (state[i] != EATING) self[i].wait;
}
void putdown (int i) {
state[i] = THINKING;
// test left and right neighbors
test((i + 4) % 5);
test((i + 1) % 5);
}
void test (int i) {
if ((state[(i + 4) % 5] != EATING) &&
(state[i] == HUNGRY) &&
(state[(i + 1) % 5] != EATING) ) {
state[i] = EATING ;
self[i].signal () ;
}
}
initialization_code() {
for (int i = 0; i < 5; i++)
state[i] = THINKING;
}
}
58Tami Sorgente, Florida Atlantic University
MODULE 9 – PROCESS SYNCHRONIZATION
Software solutionso Monitors
Classic Problems of Synchronizationo Producer/ Consumer
o Dining Philosophers
o Sharing multiple instances of a resource
o Readers and writers
59Tami Sorgente, Florida Atlantic University