synchronization. why we need synchronization? it is important that multiple processes do not access...

33
Synchronization

Upload: anis-anthony

Post on 04-Jan-2016

220 views

Category:

Documents


1 download

TRANSCRIPT

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

Synchronization

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

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.

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

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.

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

• 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.

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

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

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

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

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

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.

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

• 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.

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

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.

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

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.

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

• 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.

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

The berkeley algorithm

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

Figure 6-7. (a) The time

daemon asks all the other

machines for their clock

values.

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

Figure 6-7.

(b) The machines answer.

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

Figure 6-7. (c) The time

daemon tells everyone how

to adjust their clock.

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

Logical clocks

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

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.

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

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.

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

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

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

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

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

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

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

Lamport’s Logical Clocks (4)

Figure 6-10. The positioning of Lamport’s logical clocks in distributed systems.

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

Lamport’s Logical Clocks (5)Updating counter Ci for process Pi

1. Before executing an event Pi executes Ci ← Ci + 1.

2. When process Pi sends a message m to Pj, it sets m’s timestamp ts (m) equal to Ci after having executed the previous step.

3. Upon the receipt of a message m, process Pj adjusts its own local counter as Cj ← max{Cj , ts (m)}, after which it then executes the first step and delivers the message to the application.

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

Example: Totally Ordered Multicasting

Figure 6-11. Updating a replicated database and

leaving it in an inconsistent state.

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

Vector clock

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

Vector clock

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

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

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.

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

• 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:

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

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.

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

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)

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

Vector Clocks

Figure 6-12. Concurrent message transmission using logical clocks.

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

Vector ClocksVector clocks are constructed by letting each process Pi

maintain a vector VCi with the following two properties:

1. VCi [ i ] is the number of events that have occurred so far at Pi. In other words, VCi [ i ] is the local logical clock at process Pi .

2. If VCi [ j ] = k then Pi knows that k events have occurred at Pj. It is thus Pi’s knowledge of the local time at Pj .

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

Vector Clocks Steps carried out to accomplish property 2 of previous

slide:

1. Before executing an event Pi executes VCi [ i ] ← VCi [i ] + 1.

2. When process Pi sends a message m to Pj, it sets m’s (vector) timestamp ts (m) equal to VCi after having executed the previous step.

3. Upon the receipt of a message m, process Pj adjusts its own vector by setting VCj [k ] ← max{VCj [k ], ts (m)[k ]} for each k, after which it executes the first step and delivers the message to the application.