6 1 Process Synchronization
6 1 Process Synchronization
Chapter 6
Contents of Process Synchronization
• Background
• Peterson’s Solution
• Synchronization Hardware
• Mutex Locks
• Semaphores
• Monitors
• Synchronization Examples
• Alternative Approaches
Objectives
k
6 e
5 r
4 n
e
3 l
2
1
Virtual memory view
5
• During execution, each process can only view its 2
virtual addresses 5
4
• It cannot
3
– View another processes virtual address space
3
– Determine the physical address mapping
1
Executing 6
process 1
Virtual memory map
k
6 e
5 r
4 n
e
3 l
2
1
Inter Process Communication
• Three ways
– Shared memory
– Message Passing
– Signals
Shared Memory
User Space
• One process will create an area in RAM which the
other process can access
Process 1
• Both processes can access shared memory like a
regular working memory
– Reading/writing is like regular reading/writing Shared
– Fast Memory
Shared
Memory
Introduction to Synchronization
Introduction to Synchronization
shared variable
int counter=5;
Program 1
Program 2
{ {
. .
. .
counter++ counter- -
. .
} }
shared variable
int counter=5;
Program 1
Program 2
{ {
. .
. .
counter++ counter- -
. .
} }
int counter=5;
Program 1 Program 2
{ {
. .
. .
counter ++ counter - -
. .
} }
R1 counter
R1 R1+1
counter R1
context ----------------------
switch R2 counter
R2 R2 - 1
counter R2
counter =5
shared variable
int counter=5;
Program 1 Program 2
{ {
. .
. .
counter++ counter- -
. .
} }
R1 counter
R1 counter
---------------------
R1 R1+1
R2 counter
counter R1
context R2 R2 - 1
----------------------
switch counter R2
R2 counter
----------------------
R2 R2 - 1
R1 R1+1
counter R2
counter R1
counter =5 counter =6
shared variable
int counter=5;
Program 1 Program 2
{ {
. .
. .
counter++ counter- -
. .
} }
R1 counter R2 counter
R1 counter ---------------------- ----------------------
R1 R1+1 R2 counter R1 counter
counter R1 R2 R2 - 1 R1 R1+1
context ----------------------
switch R2 counter counter R2 counter R1
---------------------- ----------------------
R2 R2 - 1 R1 R1+1 R2 R2 - 1
counter R2 counter R1 counter R2
{
.
.
counter++ Critical section
.
}
Critical Section Problem
do
{ entry section
critical section
exit section
remainder section
} while(true);
Shared variable
Locks and Unlocks int counter=5;
lock_t L;
Program 1 Program 2
{ {
* *
* *
Lock(L) Lock(L)
counter ++ counter --
Unlock(L) Unlock(L)
* *
} }
Process 1 Process 2
while(1){ while(1){
while(turn==2); //lock while(turn==1); //lock
critical section critical section
turn=2; //unlock turn=1; //unlock
Other code Other code
} }
Process 1 Process 2
while(1){ while(1){
while(p2_inside==true); //lock while(p1_inside==true); //lock
p1_inside=true p2_inside=true
critical section critical section
p1_inside=false; //unlock p2_inside=false; //unlock
other code other code
} }
Process 1 Process 2
while(1){ while(1){
while(p2_inside==true); //lock while(p1_inside==true); //lock
p1_inside=true ; p2_inside=true ;
critical section ; critical section ;
p1_inside=false; //unlock p2_inside=false; //unlock
other code other code
} }
Software solution (attempt 3)
Shared variable p2_wants_to_enter, p1_wants_to_enter
//PROCESS 1 //PROCESS 2
while(1){ while(1){
p1_wants_to_enter=true; p2_wants_to_enter=true;
while(p2_wants_to_enter=true); while(p1_wants_to_enter=true);
critical section; critical section;
p1_wants_to_enter=false; p2_wants_to_enter=false;
other code other code
} }
Endless wait : each process is waiting for the other this is a deadlock
Peterson’s Solution
Software solution
for
critical section problem
counter++; counter--;
Unlock(L); Unlock(L);
. .
. .
} }
Process 1
While(1){
P1_wants_to_enter=true, favored=2;
critical section
P1_wants_to_enter=false;
other codes
}
Prof. Chester Rebeiro, Dept. CSE, II
T Madras
Peterson’s Solution Globally defined
P2_wants_to_enter, P1_wants_to_enter, favored;
Process 1 Process 2
While(1){ While(1){
P1_wants_to_enter=true, favored=2; P2_wants_to_enter=true, favored=1;
P1_wants_to_enter=false; P2_wants_to_enter=false;
lock=0
Process 1 Process 2
while(1){ while(1){
Lock=0
Process 1 Process 2
while(1){ while(1){
Analyze lock and unlock
Lock-=0;
P1: while(Lock != 0);
----------------------------------- context switch
P2: while(Lock != 0);
P2: Lock=1;
----------------------------------- context switch
P1:Lock=1;
…….Both processes in critical section
We would make this operation atomic
Process 1
while(1){ Make Atomic
while(lock != 0);
lock=1;//lock
critical section
lock=0;//unlock
other codes
}
Hardware support
Test & Set Instruction
Why does this work? If two CPUs execute test_and_set at the same time, the
hardware ensures that one test_and_set both its steps before the other starts.
Processor 1 memory
10
int xchg((int * L, int V){
30
int prev = *L;
*L=V;
return prev; Processor 2
} 20
Why does this work? If two CPUs execute xchg at the same time,
the hardware ensures that one xchg completes, only then second
xchg starts.
Intel Hardware support : (xchg Instruction)
Process 1
int xchg (addr, value){
acquire(&locked)
%eax=value
critical section
xchg %eax, (addr)
release (&locked)
}
• One process will acquire the lock void release (int *locked){
locked = 0;
• The other will wait in a loop repeatedly
}
checkering if the lock is available
• The lock becomes available when the
former process releases it
Issues with spinlock
CPU1 CPU2
Cache Coherence
L1 cache Protocol L1 cache
LOCK
memory
X
No useful, when the period of waiting is unpredictable or will take a long time
• eg. Not good to read page from disk.
• use mutex instead
Mutexes int xchg (addr, value){
%eax=value
xchg %eax, (addr)
}
• Can we do better than busy waiting?
void lock ( int *locked){
• If critical section is locked then yield CPU while(1){
• Go to a SLEEP state if(xchg (locked, 1) == 0)
break;
• While unlocking, wakeup sleeping process else
sleep();
}
}
// eat
signal (chopstick[i] );
signal (chopstick[ (i + 1) % 5] );
// think
} while (TRUE);
• Deadlock handling
– Allow at most 4 philosophers to be sitting
simultaneously at the table.
– Allow a philosopher to pick up the forks
only if both are available (picking must be
done in a critical section.
– Use an asymmetric solution -- an odd-
numbered philosopher picks up first the left
chopstick and then the right chopstick. Even-
numbered philosopher picks up first the
right chopstick and then the left chopstick.
Problems with Semaphores
• Abstract data type, internal variables only accessible by code within the procedure
while (true) {
/* produce an item in next produced */
while (true) {
while (counter == 0)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
counter--;
/* consume the item in next consumed */
}
Reference
Prof. Chester Rebeiro,
Dept. CSE, IIT Madras