Professional Documents
Culture Documents
P 2 P 2 P 3 ... P 4
m
m m m
P 1 P 1
P ro c ess 1 P ro c ess 2
d a ta
sender r e c e iv e r
Operations provided in Interprocess
Communications
• Receive ( [sender], message )
W eb serve r
S 1 S 2 S 3 S 4
o p e ra tio n s:
H TTP S 1 : a cce p t co n n e ctio n
a proce ss S 2 : re ce ive (re q u e s t)
re qu e st
S 3 : s e n d (re s p o n s e )
a n o p e ra tio n S 3 : disco n n e ct
H TTP
C 1 : m a k e co n n e ctio n
re spon se
C 2 : se n d (re q u e s t)
d a ta flo w C 3 : re ce ive (re sp o n se )
C 1 C 2 C 3 C 4
C 4 : disco n n e ct
W e b brow ser
b lo c k in g r e c e iv e s t a r t s
b lo c k i n g s e n d s t a r t s a n o p e r a t io n
e x e c u t i o n f lo w
a c k n o w le d g e m e n t o f d a t a r e c e iv e d s u s p e n d e d p e r io d
b lo c k in g s e n d r e t u r n s p r o v id e d b y t h e I P C f a c i li t y b lo c k i n g r e c e i v e e n d s
Client Server
Sender Receiver
Asynchronous send and synchronous receive
P rocess 2
P ro cess 1
b lo c k in g r e c e iv e s t a r t s
n o n b lo c k in g s e n d
o p e r a t io n
e x e c u t io n f lo w
s u s p e n d e d p e r io d
b lo c k in g r e c e iv e r e t u r n s
Client Server
Sender Receiver
Synchronous send and Asynchronous Receive
P rocess 2
P ro cess 1
b lo c k in g s e n d is s u e d
n o n b l o c k in g r e c e iv e is s u e d
t r a n s p a r e n t a c k n o w le d g e m e n t
p r o v i d e d b y t h e I P C f a c ili t y e x e c u t i o n f lo w
s u s p e n d e d p e r io d
n o n b lo c k in g r e c e i v e is s u e d
a n d r e t u r n e d im m e d i a t e ly
b lo c k i n g s e n d i s s u e d
p r o c e s s is n o t i f ie d
o f t h e a r r iv a l o f
d a ta
e x e c u t io n f l o w
s u s p e n d e d p e r io d
1 . c o n v e r t d a t a t o in t e r n a l r e p r e s e n t a t io n
u n m a rsh a llin g
2 . r e b u ild d a t a s t r u c t u r e s .
" T h is is a t e s t . "
-1 .5 E x t e r n a l t o in t e r n a l r e p r e s e n t a t io n a n d v ic e v e r s a
is n o t r e q u ir e d
7 .3 - if t h e t w o s id e s a r e o f t h e s a m e h o s t t y p e ;
1 .2 - if t h e t w o s id e s n e g o t ia t e s a t c o n n e c t io n .
host B
Text-based protocols
Groups:
18
Group Communication
Primitives:
multicast(g, m): sends the message m to all members
of group g
deliver(m) : delivers the message m to the calling
process
sender(m) : unique identifier of the process that sent
the message m
group(m): unique identifier of the group to which the
message m sent
19
Remote Procedure Call
call
Client Server
return
21
Remote Procedure Call
;SampleServer remoteObject
;int s
…
1,2
;s = remoteObject.sum(1,2)
;System.out.println(s)
RPC Operations
• Client Stub :
The client stub is responsible for carrying out
the out following two tasks :
• RPCRuntime :
The RPCRuntime handles transmission of
messages across the network between
client and server machines. It is
responsible for retransmissions,
acknowledgements, packet routing, and
encryption.
remote local C
invocation invocation local E
remote
invocation invocation F
A B local
invocation D
remoteobject
Local object
Data
remote
interface
m1 implementation m4
{ m2
m3 of methods
m5
m6
skeleton
Stub
RMI Client RMI Server
return
establish remote
references. return call lookup
1
2 1
server
client 3
4
1. Server implements the method and registers an object that can be used by a client to a registry
( a database that keeps track of all services and their locations). The client is aware of the remote
object. It contacts the registry to obtain information about the location of the server node, the port
number where the process resides and the object id number (Many clients can request the service
simultaneously).
2. The registry responds with information about the server’ s IP address , port number for the service,
object id number etc.
3. The client sends a request to invoke the service on the server.
4. The server executes the service and returns the results to the client.
5. The communication between the client and the server is handled through the stub and skeleton.
48
The Differences between RMI and RPC
• Clock synchronization is a problem from computer science and engineering which deals
with the idea that internal clocks of several computers may differ. Even when initially set
accurately, real clocks will differ after some amount of time due to clock drift, caused by
clocks counting time at slightly different rates.
• Every computer needs a timer mechanism (called a computer clock) to keep track of
current time and also for various accounting purposes such as calculating the time spent
by a process in CPU utilization, disk I/O and so on, so that the corresponding user can be
charged properly.
• An application may have processes that concurrently run on multiple nodes of the
system. For correct results, several such distributed applications require that the clocks
of the nodes are synchronized with each other.
• For example, on line reservation system to be fair, the only remaining seat booked
almost simultaneously from two different nodes should be offered to the client who
booked first, even if the time different between the two bookings is very small. It may
not be possible to guarantee this if the clocks of the nodes of the system are not
synchronized.
HOW COMPUTER CLOCKS ARE IMPLEMENTED
• A computer clock usually consists of three components – a quartz crystal that oscillates at a well – defined
frequency, a counter register, and a holding register.
• The holding register is used to store a constant value that is decided based on the frequency of oscillation of
the quartz crystal. That is, the value in the counter register is decremented by 1 for each oscillation of the
quartz crystal.
• When the value of the counter register becomes zero, an interrupt is generated and its value is reinitialized to
the value in the holding register. Each interrupt is called clock tick.
• The value in the holding register is chosen 60 so that on 60 clock ticks occur in a second.
• clock drift rate: the relative amount that a computer clock differs from a perfect clock
Clocks Drifting
.The relation between clock time and UTC when clocks tick at different rates
53 Distributed Systems
How Clocks Work in Computer
Holding Quartz Oscillation at a well-
crystal defined frequency
register
Each crystal oscillation
When counter gets 0, its
decrements the counter by 1
value reloaded from the
holding register
Counter
When counter is 0, an
interrupt is generated, which
is call a clock tick
CPU
At each clock tick, an interrupt
service procedure add 1 to time
stored in memory Memory
Clock Skew problem
2146 2147 2148 2149 2150 2151 2152 2153 2154
Computer 1
output.o file created
57 Distributed Systems
De- Centralized Algorithm :Lamport’s algorithm
Lamport developed a “happens before” notation to express this: a→b means that a happens
before b. If a is a message sent and b is a the message being received, then a→b must be true.
A message cannot be received before it is sent1. This relationship is transitive. If a→b and b→c
.then a→c
The importance of measuring time is to assign a time value to each event on which everyone will
agree on the final order of events. That is, if a→b then clock(a) < clock(b) since the clock must
never run backwards. If a and b occur on different processes that do not exchange messages
.(even through third parties) then a→b is not true. These events are said to be concurrent
Consider the sequence of events depicted in Figures 1 and 2 taking place between three
.machines whose clocks tick at different rates
In three of the six messages, we get the appearance of moving back in time. Because of this,
future messages from those sources appear to have originated earlier than they really have. If
we are to sort messages by the timestamps placed upon them when they were sent, the
sequence of messages would appear to be {a, b, e, d, c, f} rather than {a, b, c, d, e, f}. Lamport’s
:algorithm remedies the situation as follows
Each message carries a timestamp of the sending time (according to the sender’s
clock). When a message arrives and the receiver’s clock is less than the timestamp
on the received message, the system’s clock is forwarded to the message’s
.timestamp + 1. Otherwise nothing is done
If we apply this algorithm to the same sequence of messages, we can see that
message ordering is now preserved (Figures 3 and 4). Note that between every
.two events, the clock must tick at least once
Algorithm:
1. A machine sends a request to the time server at least every
d/2 seconds, where d is the maximum difference allowed
between a clock and the UTC;
2. The time server sends a reply message with the current UTC
when receives the request;
3. The machine measures the time delay between time serve’s
sending the message and the machine’s receiving the
message. Then, it uses the measure to adjust the clock.
61 Distributed Systems
Cristian’s algorithm
62 Distributed Systems
Cristian’s algorithm
A major problem – the client clock is fast arriving value of CUTC will be
.smaller than client’s current time, C
?What to do
One needs to gradually slow down client clock by adding less time
.per tick
Minor problem – the one-way delay from the server to client is “significant”
.and may vary considerably
?What to do
.Measure this delay and add it to CUTC
.The best estimate of delay is (T1 – T0)/2
In cases when T1 – T0 is above a threshold, then ignore the measurement.
The new time can be set to the time returned by the server plus the time that
:lapsed since the server generated the timestamp
63 Distributed Systems
Cristian’s algorithm
a)The time daemon asks all the other machines for their clock value discrepancies
c) The time daemon tells everyone how to adjust their clock to the average
Ie. The server now averages the three timestamps: the two it received and its own, computing (3:00+3:25+2:50)/3 = 3:05. Now it sends an
offset to each machine so that the machine's time will be synchronized to the average once the offset is applied. The machine with a time of
3:25 gets sent an offset of -0:20 and the machine with a time of 2:50 gets an offset of +0:15. The server has to adjust its own time by +0:05.
Mutual Exclusion
• Mutual exclusion (often abbreviated to mutex) algorithms are
used in concurrent programming to avoid the simultaneous use
of a common resource, such as a global variable, by pieces of
computer code called critical sections.
a) Process 1 asks the coordinator for permission to enter a critical region. Permission is
granted
b) Process 2 then asks permission to enter the same critical region. The coordinator does
not reply.
c) When process 1 exits the critical region, it tells the coordinator, which then replies to 2
Centralized Algorithm
• Advantages
– Mutual exclusion guaranteed by coordinator
– “Fair” sharing possible without starvation
– Simple to implement
• Disadvantages
– Single point of failure (coordinator crashes)
– Performance bottleneck
Distributed Algorithm
• When a process wants to enter a critical region, it builds a message
containing:
name of the critical region
it’s process number
it’s current time
• The process sends this message to all the processes in the network. When
another process receives this message, it takes the action pertaining on its
state and the critical region mentioned. Three cases are possible here:
1) If the message receiving process is not in the critical region and does
not wish to enter it, it sends it back.
2) Receiver is already in the critical region and does not reply
3) Receiver wants to enter the same critical region but has not done so,
it compares the “time stamp” of the incoming message with the one it has
sent to others for permission. The lowest one wins and can enter the
critical region.
• When the process exists from the critical region, it sends an OK message
to inform everyone.
A Distributed Algorithm
requires a total ordering of all events in the system
A message contains the critical region name, the process number and the current time
a) Two processes (0,2) want to enter the same critical region at the same moment.
b) Process 0 has the lowest timestamp, so it wins and enters the critical region.
c) When process 0 is done, it sends an OK also, so 2 can now enter the critical region.
• This algorithm is worse than the centralized one (n points of failure, scaling,
multiple messages…)
Distributed Algorithm
• Advantage
– No central bottleneck
– Fewer messages than Decentralized
• Disadvantage
– n points of failure
– i.e., failure of one node to respond locks up system
Token Ring Algorithm
• In software, a logical ring is constructed in which each process is assigned
a position in the ring. The ring positions may be allocated in numerical
order of network addresses or some other means. It does not matter
what the ordering is. All that matters is that each process knows who is
next in line after itself.
• When the ring is initialized, process 0 is given a token. The token circulates
around the ring. It is passed from process k to process k +1 in point-to-
point messages. When a process acquires the token from its neighbor, it
checks to see if it is attempting to enter a critical region. If so, the process
enters the region, does all the work it needs to, and leaves the region.
After it has exited, it passes the token along the ring.
• It is not permitted to enter a second critical region using the same token.
If a process is handed the token by its neighbor and is not interested in
entering a critical region, it just passes it along. As a consequence, when
no processes want to enter any critical regions, the token just circulates at
high speed around the ring.
A Token Ring Algorithm
when the process acquires the token, it accesses the critical region (if needed)
start
• Advantages
• Fairness, no starvation
• Recovery from crashes if token is not lost
• Disadvantage
• Crash of process holding token
• Difficult to detect; difficult to regenerate exactly one
token
Mutual Exclusion
Crash of any
Distributed 2(n–1) 2(n–1)
process
Lost token,
Token ring 1 to 0 to n – 1
process crash
• Now this receiver holds an election and in the end all the processes
give out except one and that one is the new coordinator. The new
coordinator announces its new post by sending all the processes a
message that it is starting immediately and is the new coordinator of
the system.
Election Algorithms
The Bully Algorithm
Selecting a coordinator
• When the message returns to p, it sees its own process ID in the list and knows
that the circuit is complete.