Module 7a: Classic Synchronization
Background
The Critical-Section Problem
Synchronization Hardware
Semaphores
Classical Problems of Synchronization
Monitors
Background
Concurrent access to shared data may result in
data inconsistency
Maintaining data consistency requires mechanisms
to ensure the orderly execution of cooperating
processes
(inconsistence data into consistence form is called
process synchronization)
Race condition
Several processes access and manipulate the same
data concurrently in different order.
we require synchronization of processes.
Race condition
Race condition
Race condition
Race Condition
The Producer calls
while (true) {
while (count == BUFFER_SIZE);
// do nothing
// produce an item and put in
next Produced
buffer[in] = next Produced;
in = (in + 1) % BUFFER_SIZE;
counter++;
}
Race Condition
The Consumer calls
while (true) {
while (count == 0)
; // do nothing
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
// consume the item in nextConsumed
}
Race Condition
count++ could be implemented as
register1 = count
register1 = register1 + 1
count = register1
count-- could be implemented as
register2 = count
register2 = register2 - 1
count = register2
Consider this execution interleaving:
S0: producer execute register1 = count {register1 = 5}
S1: producer execute register1 = register1 + 1 {register1 = 6}
S2: consumer execute register2 = count {register2 = 5}
S3: consumer execute register2 = register2 - 1 {register2 = 4}
S4: producer execute count = register1 {count = 6 }
S5: consumer execute count = register2 {count = 4}
Critical-Section
In concurrent programming a critical section is a
piece of code that accesses a shared resource that
must not be concurrently accessed by more than
one thread of execution.
That part of the program where the shared
memory is accessed is called the Critical Section.To
avoid race conditions and flawed results, one must
identify codes in Critical Sections  in each thread.
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
only those processes that are not executing in their remainder
sections can participate in the decision on which will enter its
critical section next, and this selection cannot be postponed
indefinitely.
3.BoundedWaiting - 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.
Algorithm 1
Threads share a common integer variable turn
If turn==i, thread i is allowed to execute
Does not satisfy progress requirement
Why?
Algo #1 FOR PROCES P0 & P1
Do{
while (turn != 0);
critical section
turn = 1;
remainder section
}while(true);
Do{
while (turn != 1);
critical section
turn = 0;
remainder section
}while(true);
Let the processes share common variable turn. If turn = 0 process 0 will enter if
turn = 1 process 1 will enter the critical section
CODE FOR PROCESS i
Two-task Solution
ALGORITHM NO. 1
Let the processes share common variable turn. If turn = 0 process 0 will
enter if turn = 1 process 1 will enter the critical section
CODE FOR PROCESS i
Int turn;
Do{
while (turn != i);
critical section
turn = j;
remainder section
}while(true);
MUTUAL EXCLUSION IS PRESERVED
PROGRESS REQUIREMENT NOT SATISFIED
THERE ISA STRICT ALTERNATION
Algorithm 2
Add more state information
 Boolean flags to indicate thread’s interest in entering critical section
Progress requirement still not met
 Why?
ALGO #2 FOR 2 PROCESS. PO& P1
Boolean
flag[2]={false,false};
Do{
flag [0]= true;
while (flag[ I]);
critical section
flag [0] =false;
remainder section
}while (true);
Do{
flag [1]= true;
while (flag[ 0]);
critical section
flag [1] =false;
remainder section
}
while (true);
Two-task Solution
ALGORITHM NO. 2
CODE FOR PROCESS i
Boolean flag[2]={false, false};
Do{
flag [i]= true;
while (flag[ j]);
critical section
flag [i] =false;
remainder section
}while (true);
 MUTUAL EXCLUSION IS PRESERVED
 PROGRESS REQUIREMENT NOT SATISFIED
 DEAD LOCK IS POSSIBLE
 SWITCHINGTHE ORDER OF SETTING ANDTESTINGWILL NOT SOLVETHE PROBLEM
RATHER MUTUAL EXCLUSIONWILL BEVIOLATED
T0: Po sets flag [0]=true
T1: P1 sets flag[1]= true
Note that now Po and P1 are looping forever in their
respective while statements
Algorithm 3
Combine ideas from 1 and 2
Does it meet critical section requirements?
Peterson’s algo
While
{
Flag[0]=T
Turn=1
While(turn==1 && flag[1]==T);
Critical section
Flag[0]=f
}
While
{
Flag[1]=T
Turn=0
While(turn==0 && flag[0]==T);
Critical section
Flag[1]=f
}
Synchronization Hardware
Many systems provide hardware support for critical
section code
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-interruptable
 Either test memory word and set value
 Or swap contents of two memory words
boolean TestAndSet(Boolean
&target)
{
boolean rv=target;
target=true;
return rv;
}
Synchronization Hardware
TEST AND SET INSTRUCTION
SETSTHE ARGUMENTTOTRUE AND
RETURNSTHE OLDVALUE
Boolean lock=false;
Do{
while (Test And Set (lock ) );
critical section
lock = false;
remainder section
}while (true)
 BOUNDEDWAITING
REQUIREMENT IS NOT SATISFIED
Do{
while (Test And Set (lock ) );
critical section
lock = false;
remainder section
}while (true)
boolean Swap(boolean &a, boolean &b)
{
boolean temp=a;
a=b;
b=temp;
}
Synchronization Hardware
SWAP INSTRUCTION
SWAPSTHE CONTENTS OFTWO
MEMORYWORDS
Boolean lock=false;
Do{
Key =true;
while ( key == true)
swap ( lock , key );
critical section
lock = false;
remainder section
}while (true)
 BOUNDEDWAITING
REQUIREMENT IS NOT SATISFIED
Do{
Key =true;
while ( key == true)
swap ( lock , key );
critical section
lock = false;
remainder section
}while (true)
Synchronization Hardware
TEST AND SET INSTRUCTION
Boolean waiting [ n]; // initialized to false
Boolean lock ; // initialized to false
Do{
waiting [i] =true;
key = true;
while ( waiting [ i ] && key )
key =TestAndSet ( lock );
waiting [ i ]= false;
critical section
j = (i+1) % n;
while ( ( j!=i) && !wating [k] */[j]/*)
j= (j +1) % n;
if ( j==i)
lock =false;
else
waiting [ j]=false;
remainder section
}while (true)
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
acquire(S); acquire(Q);
acquire(Q); acquire(S);
. .
. .
. .
release(S); release(Q);
release(Q); release(S);
Starvation – indefinite blocking. A process may never be removed
from the semaphore queue in which it is suspended.

Ch7

  • 1.
    Module 7a: ClassicSynchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization Monitors
  • 2.
    Background Concurrent access toshared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes (inconsistence data into consistence form is called process synchronization)
  • 3.
    Race condition Several processesaccess and manipulate the same data concurrently in different order. we require synchronization of processes.
  • 4.
  • 5.
  • 7.
  • 8.
    Race Condition The Producercalls while (true) { while (count == BUFFER_SIZE); // do nothing // produce an item and put in next Produced buffer[in] = next Produced; in = (in + 1) % BUFFER_SIZE; counter++; }
  • 9.
    Race Condition The Consumercalls while (true) { while (count == 0) ; // do nothing nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; // consume the item in nextConsumed }
  • 10.
    Race Condition count++ couldbe implemented as register1 = count register1 = register1 + 1 count = register1 count-- could be implemented as register2 = count register2 = register2 - 1 count = register2 Consider this execution interleaving: S0: producer execute register1 = count {register1 = 5} S1: producer execute register1 = register1 + 1 {register1 = 6} S2: consumer execute register2 = count {register2 = 5} S3: consumer execute register2 = register2 - 1 {register2 = 4} S4: producer execute count = register1 {count = 6 } S5: consumer execute count = register2 {count = 4}
  • 11.
    Critical-Section In concurrent programminga critical section is a piece of code that accesses a shared resource that must not be concurrently accessed by more than one thread of execution. That part of the program where the shared memory is accessed is called the Critical Section.To avoid race conditions and flawed results, one must identify codes in Critical Sections  in each thread.
  • 12.
    Solution to Critical-SectionProblem 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 only those processes that are not executing in their remainder sections can participate in the decision on which will enter its critical section next, and this selection cannot be postponed indefinitely. 3.BoundedWaiting - 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.
  • 13.
    Algorithm 1 Threads sharea common integer variable turn If turn==i, thread i is allowed to execute Does not satisfy progress requirement Why?
  • 14.
    Algo #1 FORPROCES P0 & P1 Do{ while (turn != 0); critical section turn = 1; remainder section }while(true); Do{ while (turn != 1); critical section turn = 0; remainder section }while(true); Let the processes share common variable turn. If turn = 0 process 0 will enter if turn = 1 process 1 will enter the critical section CODE FOR PROCESS i
  • 15.
    Two-task Solution ALGORITHM NO.1 Let the processes share common variable turn. If turn = 0 process 0 will enter if turn = 1 process 1 will enter the critical section CODE FOR PROCESS i Int turn; Do{ while (turn != i); critical section turn = j; remainder section }while(true); MUTUAL EXCLUSION IS PRESERVED PROGRESS REQUIREMENT NOT SATISFIED THERE ISA STRICT ALTERNATION
  • 16.
    Algorithm 2 Add morestate information  Boolean flags to indicate thread’s interest in entering critical section Progress requirement still not met  Why?
  • 17.
    ALGO #2 FOR2 PROCESS. PO& P1 Boolean flag[2]={false,false}; Do{ flag [0]= true; while (flag[ I]); critical section flag [0] =false; remainder section }while (true); Do{ flag [1]= true; while (flag[ 0]); critical section flag [1] =false; remainder section } while (true);
  • 18.
    Two-task Solution ALGORITHM NO.2 CODE FOR PROCESS i Boolean flag[2]={false, false}; Do{ flag [i]= true; while (flag[ j]); critical section flag [i] =false; remainder section }while (true);  MUTUAL EXCLUSION IS PRESERVED  PROGRESS REQUIREMENT NOT SATISFIED  DEAD LOCK IS POSSIBLE  SWITCHINGTHE ORDER OF SETTING ANDTESTINGWILL NOT SOLVETHE PROBLEM RATHER MUTUAL EXCLUSIONWILL BEVIOLATED
  • 19.
    T0: Po setsflag [0]=true T1: P1 sets flag[1]= true Note that now Po and P1 are looping forever in their respective while statements
  • 20.
    Algorithm 3 Combine ideasfrom 1 and 2 Does it meet critical section requirements?
  • 21.
    Peterson’s algo While { Flag[0]=T Turn=1 While(turn==1 &&flag[1]==T); Critical section Flag[0]=f } While { Flag[1]=T Turn=0 While(turn==0 && flag[0]==T); Critical section Flag[1]=f }
  • 22.
    Synchronization Hardware Many systemsprovide hardware support for critical section code 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-interruptable  Either test memory word and set value  Or swap contents of two memory words
  • 23.
  • 24.
    Synchronization Hardware TEST ANDSET INSTRUCTION SETSTHE ARGUMENTTOTRUE AND RETURNSTHE OLDVALUE Boolean lock=false; Do{ while (Test And Set (lock ) ); critical section lock = false; remainder section }while (true)  BOUNDEDWAITING REQUIREMENT IS NOT SATISFIED Do{ while (Test And Set (lock ) ); critical section lock = false; remainder section }while (true)
  • 25.
    boolean Swap(boolean &a,boolean &b) { boolean temp=a; a=b; b=temp; }
  • 26.
    Synchronization Hardware SWAP INSTRUCTION SWAPSTHECONTENTS OFTWO MEMORYWORDS Boolean lock=false; Do{ Key =true; while ( key == true) swap ( lock , key ); critical section lock = false; remainder section }while (true)  BOUNDEDWAITING REQUIREMENT IS NOT SATISFIED Do{ Key =true; while ( key == true) swap ( lock , key ); critical section lock = false; remainder section }while (true)
  • 27.
    Synchronization Hardware TEST ANDSET INSTRUCTION Boolean waiting [ n]; // initialized to false Boolean lock ; // initialized to false Do{ waiting [i] =true; key = true; while ( waiting [ i ] && key ) key =TestAndSet ( lock ); waiting [ i ]= false; critical section j = (i+1) % n; while ( ( j!=i) && !wating [k] */[j]/*) j= (j +1) % n; if ( j==i) lock =false; else waiting [ j]=false; remainder section }while (true)
  • 28.
    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 acquire(S); acquire(Q); acquire(Q); acquire(S); . . . . . . release(S); release(Q); release(Q); release(S); Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended.