Presentation is loading. Please wait.

Presentation is loading. Please wait.

Synchronization. Why we need synchronization? It is important that multiple processes do not access shared resources simultaneously. Synchronization in.

Similar presentations


Presentation on theme: "Synchronization. Why we need synchronization? It is important that multiple processes do not access shared resources simultaneously. Synchronization in."— Presentation transcript:

1 Synchronization

2 Why we need synchronization? It is important that multiple processes do not access shared resources simultaneously. Synchronization in a distributed system is difficult since each processes may be using its own clock time. In Distributed system there must be a global agreement on time.

3 Clock synchronization As an example consider UNIX make program. Normally in Unix, large programs are split up into multiple source files,so to change one source file only require one file to be recompiled not all files. Consider a program which consist of 100 files.When the programmer has finished changing all source files, he runs the make which examines the time at which all source and object files were last modified.

4 If the source file input.c has time 2151and the corresponding object file input. o has time 2150, make knows that input.c has been changed since input. o was created and thus input.c must be recompiled. If input.c has time 2144 and output.c has time 2145, no compilation is needed.

5 Thus make goes through all the source files to find out which one need to be recompiled and calls the compiler to recompile them.

6 What could happen in a distributed system in which there is no global agreement on time?

7 Clock Synchronization Figure 6-1. When each machine has its own clock, an event that occurred after another event may nevertheless be assigned an earlier time.

8 Therefore make will not call the compiler and the resulting binary program will contain a mixture of object file from the old source and new source program and pgm may crash.

9 Physical clocks A computer timer is precisely machined quartz crystal. Associated with each crystal are two registers counter, holding register. It is possible to program a timer to generate an interrupt 60 times a second, or at any other desired frequency. Each interrupt is called one clock tick.

10 Physical clocks A computer timer is precisely machined quartz crystal. Associated with each crystal are two registers counter, holding register. It is possible to program a timer to generate an interrupt 60 times a second, or at any other desired frequency. Each interrupt is called one clock tick.

11 When a system has n computers, all n crystals will run at slightly different rates, causing the clocks gradually to get out of synchronization and give different values when read out. This difference in time values is called clock skew.

12 The berkeley algorithm

13 Figure 6-7. (a) The time daemon asks all the other machines for their clock values.

14 Figure 6-7. (b) The machines answer.

15 Figure 6-7. (c) The time daemon tells everyone how to adjust their clock.

16 Logical clocks

17 Lamport’s Logical clock In 1978 Lamport pointed out that processes must agree on the order in which events occur also must be synchronized. He developed an algorithm for that.

18 Lamport’s Logical clock For synchronization Lamport defined a relation called happens-before. The expression a -> b a happens before b means that all processes agree that first event a occurs then event b occurs.

19 Lamport’s Logical Clocks (1) The "happens-before" relation → can be observed directly in two situations: If a and b are events in the same process, and a occurs before b, then a → b is true. If a is the event of a message being sent by one process, and b is the event of the message being received by another process, then a → b

20 Figure 6-9. (a) Three processes, each with its own clock. The clocks run at different rates.

21 Figure 6-9. (b) Lamport’s algorithm corrects the clocks.

22 Lamport’s Logical Clocks (4) Figure 6-10. The positioning of Lamport’s logical clocks in distributed systems.

23 Lamport’s Logical Clocks (5) Updating counter C i for process P i 1.Before executing an event P i executes C i ← C i + 1. 2.When process P i sends a message m to P j, it sets m’s timestamp ts (m) equal to C i after having executed the previous step. 3.Upon the receipt of a message m, process P j adjusts its own local counter as C j ← max{C j, ts (m)}, after which it then executes the first step and delivers the message to the application.

24 Example: Totally Ordered Multicasting Figure 6-11. Updating a replicated database and leaving it in an inconsistent state.

25 Vector clock

26 Vector clocks is an algorithm for generating a partial ordering of events in a distributed system and detecting causality violations.

27 Casuality If I send you a message, and you then send one to someone else, your message is causally related to mine because its possible that whatever was in my message influenced the message you sent. However, if I send my message to a third party, it no longer causally precedes yours because you couldn't have reacted to what I sent.

28 Just as in Lamport timestamps, interprocess messages contain the state of the sending process's logical clock. A vector clock of a system of N processes is an array of N logical clocks, one per process, a local copy of which is kept in each process with the following rules for clock updates:

29 Rules for clock updates Initially all clocks are zero. Each time a process experiences an internal event, it increments its own logical clock in the vector by one. Each time a process prepares to send a message, it increments its own logical clock in the vector by one and then sends its entire vector along with the message being sent.

30 Rules for clock updates Each time a process receives a message, it increments its own logical clock in the vector by one and updates each element in its vector by taking the maximum of the value in its own vector clock and the value in the vector in the received message (for every element)

31 Vector Clocks Figure 6-12. Concurrent message transmission using logical clocks.

32 Vector Clocks Vector clocks are constructed by letting each process P i maintain a vector VC i with the following two properties: 1.VC i [ i ] is the number of events that have occurred so far at P i. In other words, VC i [ i ] is the local logical clock at process P i. 2.If VC i [ j ] = k then P i knows that k events have occurred at P j. It is thus P i ’s knowledge of the local time at P j.

33 Vector Clocks Steps carried out to accomplish property 2 of previous slide: 1.Before executing an event P i executes VC i [ i ] ← VC i [i ] + 1. 2.When process P i sends a message m to P j, it sets m’s (vector) timestamp ts (m) equal to VC i after having executed the previous step. 3.Upon the receipt of a message m, process P j adjusts its own vector by setting VC j [k ] ← max{VC j [k ], ts (m)[k ]} for each k, after which it executes the first step and delivers the message to the application.


Download ppt "Synchronization. Why we need synchronization? It is important that multiple processes do not access shared resources simultaneously. Synchronization in."

Similar presentations


Ads by Google