Lamport’s Logical Clocks
☞ Implementation of logical clocks is performed using the following rules for updating the clocks and
transmitting their values in messages:
[R1]: CPi is incremented before each event is issued at process Pi: CPi := CPi + 1.
[R2]: a) When a is the event of sending a message m from process Pi, then the timestamp tm = CPi(a) is
included in m (CPi(a) is the logical clock value obtained after applying rule R1).
b) On receiving message m by process Pj, its logical clock CPj is updated as follows:
CPj := max(CPj, tm).
c) The new value of CPj is used to timestamp the event of receiving message m by Pj (applying rule R1).
Vector Clocks
☞ Implementation of vector clocks is performed using the following rules for updating the clocks and
transmitting their values in messages:
[R1]: CvPi is incremented before each event is issued at process Pi: CvPi[i] :=CPi[i] + 1.
[R2]: a) When a is the event of sending a message m
from process Pi, then the timestamp tm = CvPi(a) is included in m (CvPi(a)isthevectorclockvalue obtained
after applying rule R1).
b) On receiving message m by process Pj, its vector clock CvPj is updated as follows:
∀k ∈ {1,2,..,n}, CvPj[k] := max(CPj[k],tm[k]).
c) The new value of CvPj is used to timestamp the event of receiving message m by Pj (applying rule R1).
Ricart-Agrawala Algorithm
The Algorithm
☞ Rule for process initialization
/* performed by each process Pi at initialization */
[RI1]: statePi := RELEASED.
☞ Rule for access request to CS
/* performed whenever process Pi requests an access to the CS */
[RA1]: statePi := REQUESTED.
TPi := the value of the local logical clock corresponding to this request.
[RA2]: Pi sends a request message to all processes; the message is of the form (TPi, i), where i is an
identifier of Pi.
[RA3]: Pi waits until it has received replies from all other n-1 processes.
☞ Rule for executing the CS
/* performed by Pi after it received the n-1 replies */
[RE1]: statePi := HELD.
Pi enters the CS.
☞ Rule for handling incoming requests
/* performed by Pi whenever it received a request (TPj, j) from Pj */
[RH1]: if statePi = HELD or ((statePi = REQUESTED) and ((TPi, i)< (TPj, j))) then
Queue the request from Pj without replying.
else
Reply immediately to Pj.
end if.
☞ Rule for releasing a CS
/* performed by Pi after it finished work in a CS */
[RR1]: statePi := RELEASED.
Pi replies to all queued requests.
The Bully Algorithm
The Algorithm
☞ By default, the state of a process is ELECTION-OFF
☞ Rule for election process initiator
/* performed by a process Pi, which triggers the election procedure, or which starts an election after
receiving itself an election message */
[RE1]: statePi := ELECTION-ON.
Pi sends an election message to all processes with a higher identifier.
Pi waits for answer message.
if no answer message arrives before time-out then
Pi is the coordinator and sends a coordinator message to all processes.
else
Pi waits for a coordinator message to arrive.
if no coordinator message arrives before time-out then
restart election procedure according to RE1
end if
end if.