Distributed System Lab

Share Embed Donate


Short Description

Download Distributed System Lab...

Description

Lamport’s Logical Clock Inherent Limitations of a Distributed System  Absence of Global clock • •

difficult to make temporal order of events difficult to collect up-to-date information on the state of the entire system

 Absence of Shared Memory •

no up-to-date state of the entire system to any individual process as there's no shared memory • coherent view -- all observations of different processes ( computers ) are made at the same physical time we can obtain a coherent but partial view of the system or incoherent view of the system •

complete view ( global state ) -- local views ( local states ) + messages in transit difficult to obtain a coherent global state Leslie Lamport proposed this scheme to provide ordering of events in a distributed environment using logical clocks. Because it is impossible to have perfectly synchronized clocks and global time in a distributed system, it is often necessary to use logical clocks instead. Definitions: Happened Before Relation (->). This relation captures causal dependencies between events, that is, whether or not events have a cause and effect relation. This relation (->) is defined as follows: a -> b, if a and b are in the same process and a occurred before b. a -> b, if a is the event of sending a message and b is the receipt of that message by another process. If a -> b and b -> c, then a -> c - that is, the relation has the property of transitivity.

Causally Related Events: If event a -> event b, then a causally affects b.

Concurrent Events: Two distinct events a and b are concurrent (a || b) if (not) a -> b and (not) b -> a. That is, the events have no causal relationship. This is equivalent to b || a.

For any two events a and b in a system, only one of the following is true: a -> b, b -> a, or a || b.

e11 → e12

, e12 → e22

e21 → e13 , e14 || e24

Lamport introduced a system of logical clocks in order to make the -> relation possible. It works like this: Each process Pi in the system has its own clock Ci. Ci can be looked at as a function that assigns a number, Ci(a) to an event a. This is the timestamp of the event a in process Pi. These numbers are not in any way related to physical time -- that is why they are called logical clocks. These are generally implemented using counters, which increase each time an event occurs. Generally, an event's timestamp is the value of the clock at that time it occurs.

Conditions Satisfied by the Logical Clock system: For any events a and b, if a -> b, then C(a) < C(b). This is true if two conditions are met: If a occurs before b, then Ci(a) < Ci(b). If a is a message sent from Pi and b is the recept of that same message in Pj, then Ci(a) < Cj(b).

Implementation Rules Required: Clock Ci is incremented for each event: Ci := Ci + d (d > 0) if a is the event of sending a message from one process to another, then the receiver sets its clock to the max of its current clock and the sender's clock - that is, Cj := max(Cj, tm + d) (d > 0) . Limitation of Lamport's Clocks: if a → b but C(a) < C(b) does not necessarily imply a → b

then

C(a)

<

C(b)

Distributed Mutual Exclusion Distributed mutual exclusion (DME) coordinates software on different computers so that they agree upon assigning a resource or section of code to one particular task.

Requirement • Mutual Exclusion • Freedom from deadlock • Eventual entry(freedom from starvation) • All processes must participate equally. • Only interested processes participate. Assumptions •N nodes randomly request access. •Messages are not lost or corrupted. •Message transmissions take a finite, variable, non-zero time. •Messages arrive in order. •Transmission time might not be transitive. •Network is fully connected.

Mutual Exclusion Goals • • • •



Minimize the number of messages sent. Grant permission in order of request. Fault tolerant Fair to all systems Scalable

Distributed Mutual Exclusion Algorithms Assertion Based •

Lamport algorithm Ricart-Agrawala algorithm Maekawaa algorithm

• •

Token Based •



Raymond’s Tree-based algorithm Simple 2 process algorithm

Lamport’s DME Algorithm •Processes are granted mutual exclusion in the order in which they make the request. Each process maintains a request queue sorted in timestamp order. •Assertion based algorithm. •Uses the Lamport time numbers. 1.To request a resource, process Pi sends a timestamped request message to every other process and puts the request on its own request queue. 2.When process Pk receives a request message, it sends a timestamped reply and puts the request on its request queue. 3.Process Pi can access the resource when •Its own request is at the top of the request queue. •It has received a reply from every other process. 4.To release a resource, Piremoves its request from its queue and sends a release message to all other processes. When process Pkreceives a release message, it removes Pi’s request from its queue.

Ricartand Agrawala’sAlgorithm •Similar to Lamport’sAlgorithm but slightly more efficient. 1.To request a resource, process Pi sends a time stamped request message to every other process. 2. When process Pk receives a request msg if Pkis not currently requesting the resource it sends a time stamped reply else if timestamp of Pi request is < the timestamp of Pk’s request it sends a timestamped reply else it keeps Pi’s request. 3.Process Pi has received a reply from every other process, it can access the resource. 4.To release a resource, Pireturns a reply message to all pending processes. Simple Two Process Algorithm •Assume only two processes are competing for a sigle resource. •The two processes communicate by message passing.

Distributed Chat Server using TCP Sockets in ‘C’ Receiver: #include #include #include #include #include #include

#include #include #include

using namespace std ; //To use cout

#ifndef INADDR_NONE #define INADDR_NONE ((unsigned long)-1) INADDR_NONE if not already defined.

//This defines a constant

#endif

class receiver

//Class that contains members and methods to implement the receiver

{ int portNo ; int msgCount ; //Private Data Members

public:

receiver(int x)

//Constructor that initialzes portNO to the passed value and

0 to msgCount { portNo = x ; msgCount = 0 ; //It counts the no. of messages recived bby a perticular sender }

void connection_oriented(void) ;

//FOR TCP CONNECTION

void connection_less(void) ;

//FOR UDP SOCKET

};

int main() { int tempVar ; //for temporary input purpose int connectionType = 0 ; //to store the value for the type of Connection - 1 for TCP or 2 for UDP

couttempVar ;

if(tempVar < 1024 || tempVar > 65535) { ;

cout
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF