Professional Documents
Culture Documents
Experiment-4: Logical Clocks - Vector Clocks Description
Experiment-4: Logical Clocks - Vector Clocks Description
Experiment-4: Logical Clocks - Vector Clocks Description
Description:
In experiment-3 we studied Lamport’s logical clock, a problem with this clock is that it cannot tell you when two events
are concurrent. For example, in the figure below if we look at the timestamps, we could conclude that event 3 in process
1 happend before event 8 in process 2, but this is not necessarily true.
To see how this could happen, let us say that event 3 takes a few seconds to happen in process 1, this will not affect event
8 in process 2, that is, several events could happen in process 2 before event 3 happens in process 1, thus it is possible for
event 8 in process 2 to be executed before event 3 in process 1. We can verify this by adding a some delay (say 5 seconds)
before executing event 3 in process 1 as follows:
Now, if we run the program of experiment 3 after this modification we get the following output:
nibras@eagle:~/$
Where, process 1 has a pid=19298, process 2 has a pid=19299 and process 3 has a pid=19300. As can be seen from the
output in line 1, an event happened in process 1 and the logical clock was 2. After that several events happened in process
2 and process 3 during 5 seconds, the last was in process 2 when its logical clock was 8 – see line 8.
After 5 seconds (timestamp 12 seconds in line 9) an event happened in process 1 and thus its logical clock becomes 3. So,
in this case the event that happened in process 2 when the logical clock was 8 – see line 8 -- actually happened before the
event that happened in process 1 when the logical clock was 3 – see line 9.
Vector Clocks
Vector Clocks solve this problem by using a vector counter instead of an integer counter. The vector clock of a system
with N processes is a vector of N counters, that is, one counter per process. Vector counters follow these rules:
When applying vector clocks to the example above, we get a vector for each event as showed in following figure.
Here, there is clearly a conflict (3>2 and 0<4), and thus we can conclude that these events are concurrent and have not
influenced each other.
In-LAB Assignment:
We are going to modify the source code in experiment 3 so it implements vector clocks. Specifically, we are going to
change the way the new counter is calculated when a message is received. We are going to work with arrays, so we need
to calculate the maximum between the counter in the message and the process’s counter for each element.
We will implement and run three separate processes that can communicate with each other through messages. Each
process has its own internal counter that will be updated with each event. We will write a Python 3 program that will
print a line for each event together with the updated internal counter and also the time on the computer running the
processes. The modified source code is shown below.
def local_time(counter):
return ' (LAMPORT_TIME={}, LOCAL_TIME={})'.format(counter, datetime.now())
def process_one(pipe12):
pid = 0
counter = [0,0,0]
counter = event(pid, counter)
counter = send_message(pipe12, pid, counter)
sleep(5)
counter = event(pid, counter)
counter = recv_message(pipe12, pid, counter)
counter = event(pid, counter)
if __name__ == '__main__':
oneandtwo, twoandone = Pipe()
twoandthree, threeandtwo = Pipe()
process1.start()
process2.start()
process3.start()
process1.join()
process2.join()
process3.join()
Now we can run the program by typing the following in the command line: