Professional Documents
Culture Documents
Synchronization asad.malik@seecs.edu.pk
SEECS- A 203
(Mutual Exclusion)
Ext - 2126
Chapter-6 (Tanenbaum)
3
Mutual Exclusion
4
Why Mutual Exclusion In The Cloud
5
Why Mutual Exclusion
❖ Bank’s servers in the cloud: Two of your customers make
simultaneous deposits of $10,000 into your bank account
❖ Both read initial of $1000 concurrently from the bank’s cloud
server
❖ Both add $10,000 to this amount
❖ Both write the final amount to the server
❖ You lost $10,000!
❖ Need mutually exclusive access to your account entry at the
server
❖ Or mutually exclusive access to executing the code that modifies
the account entry
6
Problem Statement for Mutual Exclusion
7
Bank Example
Same piece of code
❖ Process - I ❖ Process - II
❖ enter(S); ❖ enter(S);
❖ accessResource ❖ accessResource
❖ obtain bank amount ❖ obtain bank amount
❖ add in deposit ❖ add in deposit
❖ update bank amount ❖ update bank amount
❖ accessResource - end ❖ accessResource - end
❖ exit(S) ❖ exit(S)
8
Processes Sharing An OS: Semaphores
❖ Semaphore == an integer that can only be accessed via
two special functions
❖ Semaphore S: 1
1. wait(S)
while(1) //Execution of loop is atomic
if(S > 0)
S- -;
break;
2. Signal(S)
❖ S++
9
Bank Example using Semaphore
Same piece of code
❖ Semaphore S : 1 ❖ Semaphore S : 1
❖ Process - I ❖ Process - II
Wait (S); Wait (S);
❖ accessResource ❖ accessResource
❖ obtain bank amount ❖ obtain bank amount
❖ add in deposit ❖ add in deposit
❖ update bank amount ❖ update bank amount
❖ accessResource - end ❖ accessResource - end
Signal (S) Signal (S)
10
Bank Example using Semaphore
Same piece of code
❖ Semaphore S : 1 ❖ Semaphore S : 1
❖ Process - I ❖ Process - II
❖ Wait (S); ❖ Wait (S);
❖ accessResource ❖ accessResource
❖ obtain bank amount ❖ obtain bank amount
❖ add in deposit ❖ add in deposit
❖ update bank amount ❖ update bank amount
❖ accessResource - end ❖ accessResource - end
❖ Signal (S) ❖ Signal (S)
11
Approaches to solve distributed mutual
exclusion
❖ Distributed systems
❖ Process communicating by exchange messages
❖ Need to guarantee 3 properties
❖ Safety - At most one process executes in CS at any time
❖ Liveness - Every request for a CS is granted eventually
❖ Ordering - Requests are granted in the order they were
made
12
Mutual Exclusion
13
Design Your Mutual Exclusion - Centralized Algorithm
14
Distributed Mutual Exclusion - System
Model
System Model
❖ Each pair of processes is connected by reliable
channels
❖ Messages are eventually delivered to recipient and in
FIFO order
❖ Process do not fail
15
Central Solution
❖ Elect a central master or leader
Master keeps
❖ A queue of waiting requests from processes who wish to access the CS
❖ A special token which allows its holder to access CS
Actions of any process in group
❖ enter( )
❖ send a request to master
❖ wait for token from master
❖ exit( )
❖ send back token to master
16
Central Solution
17
Analysis of Central Algorithm
❖ Safety - at most one process in CS
❖ Exactly one token
❖ Liveness - every request for CS granted eventually
❖ With N processes in system, queue has at most N
processes
❖ If each process exits CS eventually and no failure,
liveness guaranteed
❖ FIFO Ordering is guaranteed in order of requests received
at master
18
Analysis of Central Algorithm
❖ Bandwidth: the total number of messages sent in each
enter and exit operation
❖ 2 messages for entry
❖ 1 message for exit
❖ Synchronization delay: the time interval between one
process exiting the critical section and the next process
entering it (when there is only one process waiting)
❖ 2 messages
19
Central Solution
20
Token Ring Algorithm
21
Token Ring Algorithm
22
Token Ring
23
Analysis of Ring Based Mutual Exclusion
❖ Safety: Exactly one token
24
Analysis of Ring Based Mutual Exclusion
25
Distributed Algorithm
Think about distributed algorithm where every node
take part in decision
26
Distributed Algorithm
Can we use our total ordering multicast algorithm here
27
Distributed Algorithm -
Ricart Agrawala’s Algorithm
29
Ricart Agrawala’s Algorithm
❖ On receipt of a Request <Tj, Pj> at Pj
❖ If state == Held
❖ Add request to queue
❖ If state == Wanted && (Tj,j) < (Ti,i)
❖ Add request to queue
❖ Else
❖ Send Reply
30
Ricart Agrawala’s Algorithm
Case-I
31
Ricart Agrawala’s Algorithm
Case-I
32
Ricart Agrawala’s Algorithm
Case-II
33
Ricart Agrawala’s Algorithm
Case-II
34
Ricart Agrawala’s Algorithm
Case-II
35
Ricart Agrawala’s Algorithm
Case-II
36
Ricart Agrawala’s Algorithm
Case-II
37
Ricart Agrawala’s Algorithm
Case-II
38
Distributed Algo.
39
Decentralized Algorithm
(Home study)
40
Decentralized Algorithm
41
Issues with Decentralized Alog.
42
Election Algorithms
43
Election Algorithms
44
The Bully Algorithm
46
The Bully Algo.
47
A Token Ring Algorithm
48
A Ring Alog.
49
A Ring Alog.
50
Chang-Roberts algorithm
Algorithms
ü Every process sends an election message with its id to the left if it has
not seen a message with a higher id
ü Forward any message with an id greater than own id to the left
ü If a process receives its own election message it is the leader
ü It is uniform: number of processors does not need to be known to the
algorithm
Chang-Roberts algorithm
1
8 8
Each node sends 1
a message with 5
2
its id
to the left 2
5
neighbor
6
3
6
3
4
7 7
52 4
If: message received id > current node id
Then: forward message
5
8
1
2
5 8
6
7
3
4
7
53 6
If: message received id > current node id
Then: forward message
8
1
7 2
3 8
4
7
54
If: message received id > current node id
Then: forward message
7
8
1
2
3
4
7
55 8
If: message received id > current node id
Then: forward message
8
1
2
3
4
8 7
56
If: a node receives its own message
3
4
7
57
If: a node receives its own message
8
1
leader
2
3
4
7
58
Hirschberg-Sinclair algorithm
1
8 8
1 8
5
2 2
1
2
5
6
3 5 6
4
6
3 3
7
4
7 7
4
61
If: received id > current id
Then: send a reply(OK)
8
1
2
3
4
7
62
If: a node receives both replies
Then: it becomes a temporal leader
and proceed to next phase
8
1
2
3
4
7
63
Phase 1: send(id,1,1) to left and right
adjacent in the 2-neighborhood
8
8
1
8 2
5
5 6
5 6
6
3 7
4
7
7
64
If: received id >
current id
Then: forward(id,1,2)
8 6
1
8 5
2
5 8
7 6
3 7
6
4
5 7
65
At second step: since step counter=2, I’m on
the boundary of the 2-neighborood
3
4
7
66
If: a node receives a reply with another id
Then: forward it
If: a node receives both replies
Then: it becomes a temporal leader
8
1
2
3
4
7 67
Phase 2: send id to 2 -neighborhood
2
8
1
8 2
5
8
7
7
6
3
4
7
68
If: received id> current id
Then: send a reply
8
1
2
3
4
7
69
If: a node receives both replies
Then: it becomes the leader
8
1
2
3
4
7
70
Phase 3: send id to 8-neighborhood
Þ The node with id 8 will receive its own probe message, and
then becomes leader!
8
1
leader
2
3
4
7 71
Election In Wireless Environments
Ø Unreliable, and processes may move
Ø Network topology constantly changing
Ø Consider static ad hoc network
Algorithm
1. Any node starts by sending out an ELECTION message to neighbors
2. When a node receives an ELECTION message for the first time, it
forwards to neighbors, and designates the sender as its parent
3. It then waits for responses from its neighbors
❖ Responses may carry resource information
4. When a node receives an ELECTION message for the second time, it
just OKs it
(b)
(a)
(c) (d)
Elections in Wireless Environment
c c