Lecture 16,17, 18 Mutual Exclusion Algorithms
Lecture 16,17, 18 Mutual Exclusion Algorithms
(Mutual Exclusion)
Algorithms
Shah Khalid
Shah.khalid{@}seecs.edu.pk
National University of Sciences and Technology (NUST)
Last Session
❏ Sychronization Algorithms
2
Today
❏ Example- why ME
❏ Performance Metrics
❏ Algorithms
4
Mutual Exclusion Algorithms
❖ Create an algorithm to allow a process to obtain exclusive access to a resource
6
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
7
Problem Statement for Mutual
Exclusion
❖ Critical Section: Piece of code (at all processes) for
which we need to ensure there is at most one process
executing it at any point of time
❖ Each process can call three functions
❖ enter - to enter the critical section
❖ AccessResource - to run the critical section code
❖ exit - to exit the critical section
8
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)
9
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)
if(S > 0)
S- -;
break;
2. 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)
12
Approaches to solve distributed
mutual exclusion
❖ Distributed systems
❖ Process communicating by exchange messages
❖ Need to guarantee 3 properties
❖ Safety - At any instant, only one process can execute the
critical section.
❖ Liveness - Every request for a CS is granted eventually
❖ Fairness - Each process gets a fair chance to execute the
CS. Fairness property generally means the CS execution
requests are executed in the order of their arrival (time is
determined by a logical clock) in the system
13
Performance Metrics
❖ The performance is generally measured by the following
four metrics:
❖ Message complexity: The number of messages
required per CS execution by a site.
❖ Synchronization delay: After a site leaves the CS, it is
the time required and before the next site enters the CS
(see Figure 1).
Performance Metrics
❖ Response time: The time interval a request waits for its
CS execution to be over after its request messages
have been sent out (see Figure 2).
Centralized Algorithm
16
Distributed Mutual Exclusion -
System Model
System Model
❖ N sites - S1, S2, ……, SN
❖ Each pair of processes is connected by reliable
channels
❖ Messages are eventually delivered to recipient and in
FIFO order
17
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
18
Central Solution
19
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
20
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
21
Advantages of CA
Shortcomings
❖ Master - single point of failure, bottleneck
❖ Unable to detect dead coordinator
23
Today…
❖ Last Session:
❖ Mutual Exclusion, properties, performance metrics
❖ Centralized algorithm,
❖ Today’s Session:
❖ Lamport’s algorithm
❖ Ricart Agrawala’s Algorithm
❖ Token –based
❖ Suzuki
25
Distributed Algorithm
Can we use our total ordering multicast algorithm here
26
What is Lamport’s Algorithm
• is a permission based algorithm
proposed by Lamport as an
illustration of his synchronization
scheme for distributed systems.
• Intended to improve the safety in the
usage of shared resources
S2
(1,2)
S3
S2
(1,2),(2,1)
(1,2)
S3
(1,2) (1,2),(2,1)
Site S2 enters CS
(2,1) (1,2),(2,1)
S1
S2 exits CS
S2
(1,2),(2,1)
(1,2)
S3
(1,2) (1,2),(2,1)
S2
(1,2),(2,1)
(1,2)
S3
Site S1 entersCS
Performance Analysis
• Total No. of REQUEST (N-1)
• Total No. Of REPLY (N-1)
• Total No. Of RELEASE (N-1)
• Total = 3(N-1)
40
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
41
Ricart Agrawala’s Algorithm
Case-I
42
Ricart Agrawala’s Algorithm
Case-I
43
Ricart Agrawala’s Algorithm
Case-II
44
Ricart Agrawala’s Algorithm
Case-II
45
Ricart Agrawala’s Algorithm
Case-II
46
Ricart Agrawala’s Algorithm
Case-II
47
Ricart Agrawala’s Algorithm
Case-II
48
Ricart Agrawala’s Algorithm
Case-II
49
Ricart-Agrawala: Example(2)
Step 1:
S1 (2,1)
S2
(1,2)
S3
Step 2:
S1
S2 enters CS
S2
(2,1)
S3
Ricart-Agrawala: Example…
Step 3:
S1
S1 enters CS
S2
(2,1)
S2 leaves CS
S3
Ricart-Agrawala: Example(3)
Performance
❖ Number of messages required per entry is 2(n-1), more traffic
❖ Unreliable approach: failure of any one of node in the system can halt
the progress of the system. In this situation, the process will starve
forever.
The problem of failure of node can be solved by detecting failure after
some timeout.
❖ Performance bottleneck again like centralized algorithm
❖ Slower
53
Token-based Algorithm
54
Token-based Algorithm
❖ Completely different approach
❖ In token-based algorithms, a unique token is shared
55
Token Ring
56
Token-passing Algorithms
Suzuki-Kasami Algorithm
Example-1 of SK Algorithm
Example-1 of SK Algorithm
Example-1 of SK Algorithm
Example-1 of SK Algorithm
Example-1 of SK Algorithm
Example-1 of SK Algorithm
Example-1 of SK Algorithm
Example-1 of SK Algorithm
Example-1 of SK Algorithm
Example-1 of SK Algorithm
Example-1 of SK Algorithm
Example-1 of SK Algorithm
Example-1 of SK Algorithm
Example-2
Example-2
Example-2
Example-2
Example-2
Example-2
Analysis of Ring Based Mutual
Exclusion (Performace)
80
Analysis of Ring Based Mutual
Exclusion
81
Maekawa’s Algorithm
• Maekawa’s algorithm was the first quorum-based
mutual exclusion algorithm.
S3 = {0, 3, 4}
Let i and j attempt to enter their Critical Sections
S4 = {1, 4, 6}
Si ∩ Sj ≠ ∅implies there is a process k ∊Si ⋂Sj
S5 = {0, 5, 6}
Process k will never send reply to both. S6 = {2, 3, 6}
Algorithm
1. Requesting CS:
– Si sends REQUEST(i) to sites in Ri.
– Sj sends REPLY to Si if Sj has NOT sent a REPLY
message to any site after it received the last
RELEASE message.
• Otherwise, queue up Si’s request.
2. Executing CS: after getting REPLY from all sites in Ri.
3. Releasing CS:
– send RELEASE(i) to all sites in Ri
– Any Sj after receiving RELEASE message,
send REPLY message to the next request in
queue.
– If queue empty, update status indicating receipt of
RELEASE.
Performance
Next
• Election Algorithms