Mutual Exclusion
 Ensuring mutual exclusion means that the shared resource(critical region) is
exclusively available to one process at any given time.
 Requirements of Mutual ExclusionAlgorithms :-
 1. Safety Property :At most one process may execute in CR at a time
 Liveness Property: a process requesting entry to CR is eventually granted it.There
should not be deadlock and starvation.
 Fairness : Each process should get a fair chance to execute the CR
 Performance Metrics:-
 Synchronization delay: - time interval between CR exit and new entry by any process
 System throughput :- Rate at which requests for the CR get executed
 Message complexity:- Number of messages that are required per CR execution by
process
 Response time:-Time interval from a request send to its CR execution completed.
Classification of Mutual Exclusion Algorithms
 Centralized mutual exclusion algorithms- use coordinator to guarantee safety,
liveness and fairness properties
 Distributed mutual exclusion algorithms- do not use centralized arbitrator. Further
classified as
 Non token based- exchange messages amongst the processes to determine which
process gets to enter the CR.The request for grant of permission to enter may be to all
or a subset(quorum) of processes.When the conditions as specified by the algorithm are
satisfied , only one process enters the CR at a given time is guaranteed.
 Token Based – a unique token is shared among the process.A process is allowed to
enter its CR if it possess the token and it continues to hold the token until the
execution of the CR is over.
Distributed Mutual Exclusion
 Distributed Mutual Exclusion algorithm does not use coordinator but uses logical
clock for event ordering.The processes coordinate amongst themselves to
guarantee mutual exclusion.
 1. Non-Token Based
 Lamport’s Distribution MutualAlgo
 Ricart-Agrawala Algo
 Maekawa’sAlgo
 2.Token Based
o Suzuki-Kasami’s Broadcast Algo
o Singhal’s Heuristic Algo
Centralized Algorithm
 Process send REQUEST to the centralized ,elected coordinator to enter CR
 Coordinator acts as an arbitrator and serialization of the requests is guaranteed by it
 If no other process is currently executing the CR, variable Grant=F(alse) & Request
Queue, Req_Q=E(mpty)
 Coordinator grants permission to requesting process by sending REPLY message and
granting the permission (variable GRANT=T(rue)).This Process is not allowed to enter
the CR.
 If CR is being executed by another process when the request for permission arrives , this
could be queued (blocking the process) by coordinator or it could reply back to the
requesting process with‘permission denied’ message so that process could send the
request at some other time.
 When process executing CR exits , it sends RELEASE message to coordinator
Lamport’s Distributed Mutual Exclusion Algo
 Uses Lamport’s timestamp logical clock synchronization mechanism to establish priority
amongst the processes/nodes present in the distributed system.
 The serialization of the order of processes entering CR(Critical Region) is maintained by
FIFO request queue, RQ of each process.
 A process Pi Broadcast locally timestamped REQUEST message along with its own
identifier.
 On receiving the REQUEST, every process inserts this request in its Rqi, which contains
all incoming requests.
 The requests get inserted at the correct place(based on increasing value of the
timestamp).
 Pi also inserts this value in its own Rqi.
 The queue Rqi at any process Pi maintains the order of processes entering the CR.
Performance Parameters
 Has message overhead of total 3(N-1): (N-1) Request messages, (N-1) Reply
messages, (N-1) Release messages per CR invocation
 Can be optimized by reducing the number of RELEASE messages sent.
Recart-Agrawala Algorithm
 Based on distributed agreement using timestamps
 Uses Lamport’s logical clock to synchronize and assign timestamps to its messages
 Is similar to Lamport’s Distributed Mutual Exclusion except difference in sending of REPLY messages
 Pi sends its REQUEST message with its timestamp and identifier indicating its wish to enter CR
 If process receiving request doen not want to enter the CR (nor it is currently executing in the CR), it sends
a REPLY message back to the process requesting , granting it, its permission to enter.
 If REPLY comes back from all processes , only then the requesting process enters the CR.
 If the receiving process itself wants to enter the critical region , the timestamp of REQUEST message and
its own timestamp are compared to establish the priority.The requesting process’ REPLY is deferred if its
own timestamp is lower.
Performance Parameters
 The algorithm does not use explicit RELEASE messages.The dequeuing is done on
the receipt of REPLY itself.Thus total message overhead would be 2(N-1)
messages, i.e. for entering a CR, (N-1) request and exiting (N-1) replies.
 The failure of any process almost halts the algorithm as it requires all replies.
Maekawa’s Algorithm
 Is a quorum or voting based exclusion algorithm.
 Pi does not require to request all processes but only to a subset of processes i.e
quorum called Ri.
 Each process Pi in the quorum set Ri gives permission to at most one process at a
time.
Maekawa’s Quorums
 Data structures used by each process Pi,
1. The request deferred queue, Rdi (of processes REQUESTing and not REPLIED
to)
2. A variable called‘Voted’=F(ALSE) is set initially (T(RUE) when a reply is sent
indicating that it has already granted permission to process in its quorum)
Deadlock in Maekawa’s Algorithm
 Process P1 and Process P3 want to enter CR. Both these processes send their respective
REQUEST messages to their quorum set R1and R3 waiting for REPLY from all the set
members.
 Let’s say the REQUEST message of P1 reaches P4 earlier and as there is no other
REQUEST message prior to this message at P4(RD4=empty), theVoted4 variable is False.
So REPLY is sent back to P1,nowVoted4 in P4 is set toTRUE.
 Meanwhile the REQUEST message of P3 also reaches P2 and in the same way, P2 sends
REPLY to P3 and sets its ownVoted2 variable toTRUE. Now waits for a REPLY from P2,
which cannot be sent (VOTED2 isTRUE)
 Thus the request of P1 is deferred and the deferred queue of P2 now has an entry of
P1(RD2[1]=P1). Similarly the REPLY can’t be sent to process P3 by P4 and deferred
queue of process P3 is RD4[1]=P3. so now P1 is waiting for P2’s REPLY,P2 for P3’s
RELEASE,P3 for P4’s REPLY and P4 for P1’s RELEASE,thus resulting in deadlock.
Performance Parameters
 An execution of CR requires √N REQUEST, √N REPLY and √N RELEASE
messages thus requiring total 3 √N messages per CR execution.
 Synchronization delay is 2T
 M=K= √N works best.
Suzuki-Kasami Broadcast Algo
 Token Base Algorithm
 Process holding theTOKEN can enter CR.
 Basic idea is to contact every process in completely connected network of P processes,
and gather the grant REPLYs from all to establish mutual exclusion.
 Process not having token , wants to enter the CR , it has to broadcast its request for
granting permission to all processes using primitive REQUEST for token.
 REQUEST(i,n)  i is the process identifier and n sequence number of the request it is
sending. E.g. REQUEST(3,4)  process 3 requesting and this is its fourth request.
Data structures used
 By process Pi
 Request array Ri[j]; j=1 to N processes
e.g. R2[3]=5 Process 2 knows that process 3 has sent 5 requests for entering CR
to Process 2.
When Process Pi receives REQUEST(j,n) message , it sets Ri[j]=max (Ri[j],n).
The current request is termed outdated, if the valueTi[j] is greater than n. e.g P2
receives REQUEST(3,4, n-4 indicating its 4th request. IfR2[3]=5, meaning that P2 has
already seen 5 requests earlier fromP3.As R2[3]=5>n=4, this 4th request is termed as
outdated request.
Data structures used
 By token
 Token arrayToken[i]  maintains the number of times the ith process has executed critical region. e.g
Token[1]=6 means that token was given to process 1 and its sixth REQUEST of entering CR.
Previously 5 times request was present. Once process Pi holding the token exits the CR, the value of
this array for the process holding token is updated with knowledge of process.Also if Ri[j]=token[j]+1
, then process Pi is currently requesting a token.
 Queue QT  contains queue of the processes requesting token.The process at the top os this is the
one to which token is sent next.
Singhal’s Heuristic Algorithm
 Suzuku-Kasami’s algo having N messages for just obtaining the token
 This method proposes to reduce this number by applying heuristic method , i.e. having knowledge about
each process’ current state and sending the token request accordingly.
 The process could be in any of the following 
E Executing and having the token
H Holding the token and not executing
R Requesting the token
N Neutral, none of the above
 The algorithm selects the processes which have more probability of having the token and requesting process
then sends the request to only selected ones rather than broadcasting reducing number of messages
 This is called heuristic algo as processes are heuristically selected for sending token request messages,
according to their current states.
 For example, if k is chosen to be 2, then the processes having IDs 2 and above(i>=k)
are in neutral state, process P1 (i<k) would be only process in R state.
 Thus if process P2, now wishes to enter the critical region , the REQUEST message
requires to be sent only to 1 as there is no need to send the REQUEST for the
permission grant to others as they never requested to enter in CR in the first place.
Raymond’s Tree-Based Algorithm
 Use minimal spanning tree for sending the REQUEST message to the process
which is holding token
 The spanning tree– N nodes/process is viewed as a graph; nodes/processes as
vertices and links as edges of the network
 Path from every node to every other node; the shortest path is the minimal
spanning tree
 Tree arranged as rooted tree; the process holding token has privilege to be root
 Each process maintains HOLDER variable H, which points to the root or its own
neighbor process that will lead it towards the root.
 Each process has variable‘Asked’ which is set to FALSE initially. It is setTRUE only
if REQUEST is sent (for itself ot for neighbors) and not received. If PRIVILEGE
message is received , then it is reset to FALSE.
 Each process Pi maintains request queue called RQi  consists of identities of those
immediate neighbors that have requested for the privilege but have not yet been sent
the token. Maximum size of RQ of node is the number of immediate neighbors +1.
7. Mutual Exclusion
7. Mutual Exclusion
7. Mutual Exclusion

7. Mutual Exclusion

  • 1.
    Mutual Exclusion  Ensuringmutual exclusion means that the shared resource(critical region) is exclusively available to one process at any given time.  Requirements of Mutual ExclusionAlgorithms :-  1. Safety Property :At most one process may execute in CR at a time  Liveness Property: a process requesting entry to CR is eventually granted it.There should not be deadlock and starvation.  Fairness : Each process should get a fair chance to execute the CR
  • 2.
     Performance Metrics:- Synchronization delay: - time interval between CR exit and new entry by any process  System throughput :- Rate at which requests for the CR get executed  Message complexity:- Number of messages that are required per CR execution by process  Response time:-Time interval from a request send to its CR execution completed.
  • 3.
    Classification of MutualExclusion Algorithms  Centralized mutual exclusion algorithms- use coordinator to guarantee safety, liveness and fairness properties  Distributed mutual exclusion algorithms- do not use centralized arbitrator. Further classified as  Non token based- exchange messages amongst the processes to determine which process gets to enter the CR.The request for grant of permission to enter may be to all or a subset(quorum) of processes.When the conditions as specified by the algorithm are satisfied , only one process enters the CR at a given time is guaranteed.  Token Based – a unique token is shared among the process.A process is allowed to enter its CR if it possess the token and it continues to hold the token until the execution of the CR is over.
  • 4.
    Distributed Mutual Exclusion Distributed Mutual Exclusion algorithm does not use coordinator but uses logical clock for event ordering.The processes coordinate amongst themselves to guarantee mutual exclusion.  1. Non-Token Based  Lamport’s Distribution MutualAlgo  Ricart-Agrawala Algo  Maekawa’sAlgo  2.Token Based o Suzuki-Kasami’s Broadcast Algo o Singhal’s Heuristic Algo
  • 5.
    Centralized Algorithm  Processsend REQUEST to the centralized ,elected coordinator to enter CR  Coordinator acts as an arbitrator and serialization of the requests is guaranteed by it  If no other process is currently executing the CR, variable Grant=F(alse) & Request Queue, Req_Q=E(mpty)  Coordinator grants permission to requesting process by sending REPLY message and granting the permission (variable GRANT=T(rue)).This Process is not allowed to enter the CR.  If CR is being executed by another process when the request for permission arrives , this could be queued (blocking the process) by coordinator or it could reply back to the requesting process with‘permission denied’ message so that process could send the request at some other time.  When process executing CR exits , it sends RELEASE message to coordinator
  • 8.
    Lamport’s Distributed MutualExclusion Algo  Uses Lamport’s timestamp logical clock synchronization mechanism to establish priority amongst the processes/nodes present in the distributed system.  The serialization of the order of processes entering CR(Critical Region) is maintained by FIFO request queue, RQ of each process.  A process Pi Broadcast locally timestamped REQUEST message along with its own identifier.  On receiving the REQUEST, every process inserts this request in its Rqi, which contains all incoming requests.  The requests get inserted at the correct place(based on increasing value of the timestamp).  Pi also inserts this value in its own Rqi.  The queue Rqi at any process Pi maintains the order of processes entering the CR.
  • 12.
    Performance Parameters  Hasmessage overhead of total 3(N-1): (N-1) Request messages, (N-1) Reply messages, (N-1) Release messages per CR invocation  Can be optimized by reducing the number of RELEASE messages sent.
  • 13.
    Recart-Agrawala Algorithm  Basedon distributed agreement using timestamps  Uses Lamport’s logical clock to synchronize and assign timestamps to its messages  Is similar to Lamport’s Distributed Mutual Exclusion except difference in sending of REPLY messages  Pi sends its REQUEST message with its timestamp and identifier indicating its wish to enter CR  If process receiving request doen not want to enter the CR (nor it is currently executing in the CR), it sends a REPLY message back to the process requesting , granting it, its permission to enter.  If REPLY comes back from all processes , only then the requesting process enters the CR.  If the receiving process itself wants to enter the critical region , the timestamp of REQUEST message and its own timestamp are compared to establish the priority.The requesting process’ REPLY is deferred if its own timestamp is lower.
  • 16.
    Performance Parameters  Thealgorithm does not use explicit RELEASE messages.The dequeuing is done on the receipt of REPLY itself.Thus total message overhead would be 2(N-1) messages, i.e. for entering a CR, (N-1) request and exiting (N-1) replies.  The failure of any process almost halts the algorithm as it requires all replies.
  • 17.
    Maekawa’s Algorithm  Isa quorum or voting based exclusion algorithm.  Pi does not require to request all processes but only to a subset of processes i.e quorum called Ri.  Each process Pi in the quorum set Ri gives permission to at most one process at a time.
  • 19.
  • 20.
     Data structuresused by each process Pi, 1. The request deferred queue, Rdi (of processes REQUESTing and not REPLIED to) 2. A variable called‘Voted’=F(ALSE) is set initially (T(RUE) when a reply is sent indicating that it has already granted permission to process in its quorum)
  • 22.
  • 23.
     Process P1and Process P3 want to enter CR. Both these processes send their respective REQUEST messages to their quorum set R1and R3 waiting for REPLY from all the set members.  Let’s say the REQUEST message of P1 reaches P4 earlier and as there is no other REQUEST message prior to this message at P4(RD4=empty), theVoted4 variable is False. So REPLY is sent back to P1,nowVoted4 in P4 is set toTRUE.  Meanwhile the REQUEST message of P3 also reaches P2 and in the same way, P2 sends REPLY to P3 and sets its ownVoted2 variable toTRUE. Now waits for a REPLY from P2, which cannot be sent (VOTED2 isTRUE)  Thus the request of P1 is deferred and the deferred queue of P2 now has an entry of P1(RD2[1]=P1). Similarly the REPLY can’t be sent to process P3 by P4 and deferred queue of process P3 is RD4[1]=P3. so now P1 is waiting for P2’s REPLY,P2 for P3’s RELEASE,P3 for P4’s REPLY and P4 for P1’s RELEASE,thus resulting in deadlock.
  • 24.
    Performance Parameters  Anexecution of CR requires √N REQUEST, √N REPLY and √N RELEASE messages thus requiring total 3 √N messages per CR execution.  Synchronization delay is 2T  M=K= √N works best.
  • 25.
    Suzuki-Kasami Broadcast Algo Token Base Algorithm  Process holding theTOKEN can enter CR.  Basic idea is to contact every process in completely connected network of P processes, and gather the grant REPLYs from all to establish mutual exclusion.  Process not having token , wants to enter the CR , it has to broadcast its request for granting permission to all processes using primitive REQUEST for token.  REQUEST(i,n)  i is the process identifier and n sequence number of the request it is sending. E.g. REQUEST(3,4)  process 3 requesting and this is its fourth request.
  • 26.
    Data structures used By process Pi  Request array Ri[j]; j=1 to N processes e.g. R2[3]=5 Process 2 knows that process 3 has sent 5 requests for entering CR to Process 2. When Process Pi receives REQUEST(j,n) message , it sets Ri[j]=max (Ri[j],n). The current request is termed outdated, if the valueTi[j] is greater than n. e.g P2 receives REQUEST(3,4, n-4 indicating its 4th request. IfR2[3]=5, meaning that P2 has already seen 5 requests earlier fromP3.As R2[3]=5>n=4, this 4th request is termed as outdated request.
  • 27.
    Data structures used By token  Token arrayToken[i]  maintains the number of times the ith process has executed critical region. e.g Token[1]=6 means that token was given to process 1 and its sixth REQUEST of entering CR. Previously 5 times request was present. Once process Pi holding the token exits the CR, the value of this array for the process holding token is updated with knowledge of process.Also if Ri[j]=token[j]+1 , then process Pi is currently requesting a token.  Queue QT  contains queue of the processes requesting token.The process at the top os this is the one to which token is sent next.
  • 31.
    Singhal’s Heuristic Algorithm Suzuku-Kasami’s algo having N messages for just obtaining the token  This method proposes to reduce this number by applying heuristic method , i.e. having knowledge about each process’ current state and sending the token request accordingly.  The process could be in any of the following  E Executing and having the token H Holding the token and not executing R Requesting the token N Neutral, none of the above  The algorithm selects the processes which have more probability of having the token and requesting process then sends the request to only selected ones rather than broadcasting reducing number of messages  This is called heuristic algo as processes are heuristically selected for sending token request messages, according to their current states.
  • 33.
     For example,if k is chosen to be 2, then the processes having IDs 2 and above(i>=k) are in neutral state, process P1 (i<k) would be only process in R state.  Thus if process P2, now wishes to enter the critical region , the REQUEST message requires to be sent only to 1 as there is no need to send the REQUEST for the permission grant to others as they never requested to enter in CR in the first place.
  • 35.
    Raymond’s Tree-Based Algorithm Use minimal spanning tree for sending the REQUEST message to the process which is holding token  The spanning tree– N nodes/process is viewed as a graph; nodes/processes as vertices and links as edges of the network  Path from every node to every other node; the shortest path is the minimal spanning tree  Tree arranged as rooted tree; the process holding token has privilege to be root
  • 36.
     Each processmaintains HOLDER variable H, which points to the root or its own neighbor process that will lead it towards the root.  Each process has variable‘Asked’ which is set to FALSE initially. It is setTRUE only if REQUEST is sent (for itself ot for neighbors) and not received. If PRIVILEGE message is received , then it is reset to FALSE.  Each process Pi maintains request queue called RQi  consists of identities of those immediate neighbors that have requested for the privilege but have not yet been sent the token. Maximum size of RQ of node is the number of immediate neighbors +1.