Unit 1
Unit 1
Unit I
Introduction to Distributed Systems
Characteristics
Request/Reply Protocols
RMI
1) Distributed Systems
A distributed system contains multiple nodes that are physically separate but linked together
using the network. All the nodes in this system communicate with each other and handle
processes in tandem. Each of these nodes contains a small part of the distributed operating
system software.
A diagram to better explain the distributed system is −
2)Types of Distributed Systems
The nodes in the distributed systems can be arranged in the form of client/server systems or peer
to peer systems. Details about these are as follows –
a) Client/Server Systems
In client server systems, the client requests a resource and the server provides that resource. A
server may serve multiple clients at the same time while a client is in contact with only one
server. Both the client and server usually communicate via a computer network and so they are a
part of distributed systems.
b) Peer to Peer Systems
The peer to peer systems contains nodes that are equal participants in data sharing. All the tasks
are equally divided between all the nodes. The nodes interact with each other as required as share
resources. This is done with the help of a network.
● All the nodes in the distributed system are connected to each other. So nodes can easily
share data with other nodes.
● More nodes can easily be added to the distributed system i.e. it can be scaled as required.
● Failure of one node does not lead to the failure of the entire distributed system. Other
nodes can still communicate with each other.
● Resources like printers can be shared with multiple nodes rather than being restricted to
just one.
● Resource Sharing
Resource sharing means that the existing resources in a distributed system can be accessed or
remotely accessed across multiple computers in the system.
Hardware resources are shared for reductions in cost and convenience. Data is shared for
consistency and exchange of information.
Resources are managed by a software module known as a resource manager. Every resource has
its own management policies and methods.
● Heterogeneity
In distributed systems components can have variety and differences in Networks, Computer
hardware, Operating systems, Programming languages and implementations by different
developers.
● Openness
Openness is concerned with extensions and improvements of distributed systems. The distributed
system must be open in terms of Hardware and Softwares. In order to make a distributed system
open,
● Concurrency
Concurrency is a property of a system representing the fact that multiple activities are executed
at the same time. The concurrent execution of activities takes place in different components
running on multiple machines as part of a distributed system. In addition, these activities may
perform some kind of interactions among them. Concurrency reduces the latency and increases
the throughput of the distributed system.
● Scalability
Scalability is mainly concerned about how the distributed system handles the growth as the
number of users for the system increases. Mostly we scale the distributed system by adding more
computers in the network. Components should not need to be changed when we scale the system.
Components should be designed in such a way that it is scalable.
● Fault Tolerance
In a distributed system hardware, software, network anything can fail. The system must be
designed in such a way that it is available all the time even after something has failed.
● Transparency
Distributed systems should be perceived by users and application programmers as a whole rather
than as a collection of cooperating components. Transparency can be of various types like
access, location, concurrency, replication, etc.
The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that tests for
safety by simulating the allocation for predetermined maximum possible amounts of all
resources, then makes an “s-state” check to test for possible activities, before deciding whether
allocation should be allowed to continue.
Banker’s algorithm is named so because it is used in banking system to check whether loan can
be sanctioned to a person or not.
Suppose there are n number of account holders in a bank and the total sum of their money is S. If
a person applies for a loan then the bank first subtracts the loan amount from the total money that
bank has and if the remaining amount is greater than S then only the loan is sanctioned. It is done
because if all the account holders comes to withdraw their money then the bank can easily do it.
In other words, the bank would never allocate its money in such a way that it can no longer
satisfy the needs of all its customers. The bank would try to be in safe state always.
Following Data structures are used to implement the Banker’s Algorithm:
Let ‘n’ be the number of processes in the system and ‘m’ be the number of resources types.
Available :
● It is a 1-d array of size ‘m’ indicating the number of available resources of each type.
● Available[ j ] = k means there are ‘k’ instances of resource type Rj
Max :
● It is a 2-d array of size ‘n*m’ that defines the maximum demand of each process in a system.
● Max[i, j ] = k means process Pi may request at most ‘k’ instances of resource type Rj.
Allocation:
● It is a 2-d array of size ‘n*m’ that defines the number of resources of each type currently
allocated to each process.
● Allocation[i, j ] = k means process Pi is currently allocated ‘k’ instances of resource type Rj
Need:
● It is a 2-d array of size ‘n*m’ that indicates the remaining resource need of each process.
● Need [ i, j ] = k means process Pi currently need ‘k’ instances of resource type Rj
for its execution.
● Need [ i, j ] = Max [ i, j ] – Allocation [ i, j ]
Allocationi specifies the resources currently allocated to process Pi and Needi specifies the
additional resources that process Pi may still request to complete its task.
Banker’s algorithm consists of Safety algorithm and Resource request algorithm
b)Safety Algorithm
The algorithm for finding out whether or not a system is in a safe state can be described as
follows:
1) Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively.
Initialize: Work = Available
Finish[i] = false; for i=1, 2, 3, 4….n
2) Find an i such that both
a) Finish[i] = false
b) Needi <= Work
if no such i exists goto step (4)
3) Work = Work + Allocation[i]
Finish[i] = true
goto step (2)
4) if Finish [i] = true for all i
then the system is in a safe state
c)Resource-Request Algorithm
Let Requesti be the request array for process Pi. Requesti [j] = k means process Pi wants k
instances of resource type Rj. When a request for resources is made by process Pi, the following
actions are taken:
d)Example:
Considering a system with five processes P0 through P4 and three resources of type A, B, C.
Resource type A has 10 instances, B has 5 instances and type C has 7 instances. Suppose at time
t0 following snapshot of the system has been taken:
Question3. What will happen if process P1 requests one additional instance of resource type
A and two instances of resource type C?
We must determine whether this new system state is safe. To do so, we again execute Safety
algorithm on the above data structures.
Hence the new system state is safe, so we can immediately grant the request for process P1 .
// Available Resources
avail = new int[] { 3, 3, 2 };
}
void isSafe()
{
int count=0;
//visited array to find the already allocated process
booleanvisited[] = new boolean[n];
for (int i = 0;i< n; i++)
{
visited[i] = false;
}
//work array to store the copy of available resources
int work[] = new int[m];
for (int i = 0;i< m; i++)
{
work[i] = avail[i];
}
while (count<n)
{
boolean flag = false;
for (int i = 0;i< n; i++)
{
if (visited[i] == false)
{
int j;
for (j = 0;j< m; j++)
{
if (need[i][j] > work[j])
break;
}
if (j == m)
{
safeSequence[count++]=i;
visited[i]=true;
flag=true;
void calculateNeed()
{
for (int i = 0;i< n; i++)
{
for (int j = 0;j< m; j++)
{
need[i][j] = max[i][j]-alloc[i][j];
}
}
}
[Link]();
//Calculate the Need Matrix
[Link]();
f) Python3
# Banker's Algorithm
# Driver code:
if __name__=="__main__":
if (flag == 0):
ans[ind] = i
ind += 1
for y in range(m):
avail[y] += alloc[i][y]
f[i] = 1
1. Architectural Models
2. Interaction Models
3. Fault Models
1. Architectural Models
Architectural model describes responsibilities distributed between system components and how
are these components placed.
a) Client-server model
The system is structured as a set of processes, called servers, that offer services to the users,
called clients.
● The client-server model is usually based on a simple request/reply protocol, implemented
with send/receive primitives or using remote procedure calls (RPC) or remote method
invocation (RMI):
● The client sends a request (invocation) message to the server asking for some service;
● The server does the work and returns a result (e.g. the data requested) or an error code if
the work could not be performed.
A server can itself request services from other servers; thus, in this new relation, the server
itself acts like a client.
c) Peer-to-peer
● Processes (objects) interact without particular distinction between clients and servers.
● The pattern of communication depends on the particular application.
● A large number of data objects are shared; any individual computer holds only a small
part of the application database.
● Processing and communication loads for access to objects are distributed across many
computers and access links.
● This is the most general and flexible model.
● Peer-to-Peer tries to solve some of the above.
● It distributes shared resources widely -> share computing and communication loads.
2. Interaction Model
Interaction model are for handling time i. e. for process execution, message delivery, clock drifts
etc.
Main features:
● Lower and upper bounds on execution time of processes can be set.
● Transmitted messages are received within a known bounded time.
● Drift rates between local clocks have a known bound.
Important consequences:
1. In a synchronous distributed system there is a notion of global physical time (with a
known relative precision depending on the drift rate).
2. Only synchronous distributed systems have a predictable behavior in terms of timing.
Only such systems can be used for hard real-time applications.
3. In a synchronous distributed system it is possible and safe to use timeouts in order to
detect failures of a process or communication link.
4. It is difficult and costly to implement synchronous distributed systems.
● Many distributed systems (including those on the Internet) are asynchronous. - No bound
on process execution time (nothing can be assumed about speed, load, and reliability of
computers).
● No bound on message transmission delays (nothing can be assumed about speed, load,
and reliability of interconnections) - No bounds on drift rates between local clocks.
Important consequences:
3. Fault Models
● Failures can occur both in processes and communication channels. The reason can be
both software and hardware faults.
● Fault models are needed in order to build systems with predictable behavior in case of
faults (systems which are fault tolerant).
● such a system will function according to the predictions, only as long as the real faults
behave as defined by the “fault model”.
8)Request/Reply Communication
Queues are the key to connectionless communication. Each server is assigned an Inter-Process
Communication (IPC) message queue called a request queue and each client is assigned a
reply queue. Therefore, rather than establishing and maintaining a connection with a server, a
client application can send requests to the server by putting those requests on the server's
queue, and then check and retrieve messages from the server by pulling messages from its
own reply queue.
The request/reply model is used for both synchronous and asynchronous service requests as
described in the following topics.
a)Synchronous Messaging
In a synchronous call, a client sends a request to a server, which performs the requested action
while the client waits. The server then sends the reply to the client, which receives the reply.
This is known as Synchronous Request/Reply Communication.
b) Asynchronous Messaging
In an asynchronous call, the client does not wait for a service request it has submitted to finish
before undertaking other tasks. Instead, after issuing a request, the client performs additional
tasks (which may include issuing more requests). When a reply to the first request is
available, the client retrieves [Link] is known as Asynchronous Request/Reply
Communication.
9) RMI
RMI stands for Remote Method Invocation. It is a mechanism that allows an object residing in
one system (JVM) to access/invoke an object running on another JVM.
RMI is used to build distributed applications; it provides remote communication between Java
programs. It is provided in the package [Link].
In an RMI application, we write two programs, a server program (resides on the server) and
a client program (resides on the client).
● Inside the server program, a remote object is created and reference of that object is made
available for the client (using the registry).
● The client program requests the remote objects on the server and tries to invoke its
methods.
The following diagram shows the architecture of an RMI application.
Let us now discuss the components of this architecture.
● Transport Layer − This layer connects the client and the server. It manages the existing
connection and also sets up new connections.
● Stub − A stub is a representation (proxy) of the remote object at client. It resides in the
client system; it acts as a gateway for the client program.
● Skeleton − This is the object which resides on the server side. stub communicates with
this skeleton to pass request to the remote object.
● RRL(Remote Reference Layer) − It is the layer which manages the references made by
the client to the remote object.
Whenever a client invokes a method that accepts parameters on a remote object, the parameters
are bundled into a message before being sent over the network. These parameters may be of
primitive type or objects. In case of primitive type, the parameters are put together and a header
is attached to it. In case the parameters are objects, then they are serialized. This process is
known as marshalling.
At the server side, the packed parameters are unbundled and then the required method is
invoked. This process is known as unmarshalling.
d)RMI Registry
RMI registry is a namespace on which all server objects are placed. Each time the server creates
an object, it registers this object with the RMIregistry (using bind() or reBind() methods).
These are registered using a unique name known as bind name.
To invoke a remote object, the client needs a reference of that object. At that time, the client
fetches the object from the registry using its bind name (using lookup() method).
The following illustration explains the entire process −
e)Goals of RMI
● Logical Clocks refer to implementing a protocol on all machines within your distributed
system, so that the machines are able to maintain consistent ordering of events within
some virtual timespan.
● Distributed systems may have no physically synchronous global clock, so a logical clock
allows global ordering on events from different processes in such systems.
a) Example
If we go outside then we have made a full plan that at which place we have to go first, second
and so on. We don’t go to second place at first and then the first place. We always maintain the
procedure or an organization that is planned before. In a similar way, we should do the
operations on our PCs one by one in an organized way.
Suppose, we have more than 10 PCs in a distributed system and every PC is doing it’s own work
but then how we make them work together. There comes a solution to this i.e. LOGICAL
CLOCK.
Method-1:
● This means that if one PC has a time 2:00 pm then every PC should have the same time
which is quite not possible. Not every clock can sync at one time. Then we can’t follow
this method.
Method-2:
●
Another approach is to assign Timestamps to events.
● Taking the example into consideration, this means if we assign the first place as 1, second
place as 2, third place as 3 and so on. Then we always know that the first place will
always come first and then so on. Similarly, If we give each PC their individual number
than it will be organized in a way that 1st PC will complete its process first and then
second and so on. But Timestamps will only work as long as they obey causality.
b)Causality
● Taking single PC only if 2 events A and B are occurring one by one then TS(A) < TS(B). If
A has timestamp of 1, then B should have timestamp more than 1, then only happen before
relationship occurs.
● Taking 2 PCs and event A in P1 (PC.1) and event B in P2 (PC.2) then also the condition will
be TS(A) < TS(B). Taking example- suppose you are sending message to someone at 2:00:00
pm, and the other person is receiving it at 2:00:02 [Link] it’s obvious that TS(sender) <
TS(receiver).
● Transitive Relation
If, TS(A) <TS(B) and TS(B) <TS(C), then TS(A) < TS(C)
● Concurrent Event
This means that not every process occurs one by one, some processes are made to happen
simultaneously i.e., A || B.
d)Causal ordering
Causal ordering is a vital tool for thinking about distributed systems. Once you understand it,
many other concepts become much simpler.
(i)The fundamental property of distributed systems:
Messages sent between machines may arrive zero or more times at any point after they are sent
This is the sole reason that building distributed systems is hard.
For example, because of this property it is impossible for two computers communicating over a
network to agree on the exact time. You can send me a message saying "it is now 10:00:00" but I
don't know how long it took for that message to arrive. We can send messages back and forth all
day but we will never know for sure that we are synchronized.
If we can't agree on the time then we can't always agree on what order things happen in. Suppose
I say "my user logged on at 10:00:00" and you say "my user logged on at 10:00:01". Maybe mine
was first or maybe my clock is just fast relative to yours. The only way to know for sure is if
something connects those two events.
For example, if my user logged on and then sent your user an email and if you received that
email before your user logged on then we know for sure that mine was first.
This concept is called causal ordering and is written like this:
A -> B (event A is causally ordered before event B)
Let's define it a little more formally. We model the world as follows: We have a number of
machines on which we observe a series of events. These events are either specific to one
machine (eg user input) or are communications between machines. We define the causal ordering
of these events by three rules:
If A and B happen on the same machine and A happens before B then A -> B
If I send you some message M and you receive it then (send M) -> (recv M)
(ii)Clocks
Lamport clocks and Vector clocks are data-structures which efficiently approximate the causal
ordering and so can be used by programs to reason about causality.
If A -> B then LC_A < LC_B
11. Consistency
When mutable state is distributed over multiple machines each machine can receive update
events at different times and in different orders.
If the final state is dependent on the order of updates then the system must choose a single
serialisation of the events, imposing a global total order.
A distributed system is consistent exactly when the outside world can never observe two
different serialisations.
The first choice risks violating consistency if some other machine makes the same choice with a
different set of events.
The second violates availability by waiting for every other machine that could possibly have
received a conflicting event before performing the requested action.
There is no need for an actual network partition to happen - the trade-off between availability
and consistency exists whenever communication between components is not instant.
Ordering requires waiting
Even your hardware cannot escape this law. It provides the illusion of synchronous access to
memory at the cost of availabilty. If you want to write fast parallel programs then you need to
understand the messaging model used by the underlying hardware.
a)Distributed Algorithm:
Distributed system is a collection of independent computers that do not share their memory.
Each processor has its own memory and they communicate via communication networks.
Many algorithms used in distributed system require a coordinator that performs functions needed
by other processes in the system.
b)Election Algorithms:
Election algorithms choose a process from group of processors to act as a coordinator. If the
coordinator process crashes due to some reasons, then a new coordinator is elected on other
processor.
Election algorithm basically determines where a new copy of coordinator should be restarted.
Election algorithm assumes that every active process in the system has a unique priority number.
The process with highest priority will be chosen as a new coordinator. Hence, when a
coordinator fails, this algorithm elects that active process which has highest priority number.
Then this number is send to every active process in the distributed system.
We have two election algorithms for two different configurations of distributed system.
This algorithm applies to system where every process can send a message to every other process
in the system.
When any process notices that the coordinator is no longer responding to request, it initiates an
ELECTION. The process holds an ELECTION message as follows:
Example:
We start with 6 processes, all directly connected to each other. Process 6 is the leader,
as it has the highest number.
Process 6 fails.
Process 3 notices that Process 6 does not respond. So it starts an election, notifying
those processes with ids greater than 3.
Both Process 4 and Process 5 respond, telling Process 3 that they'll take over from here.
Algorithm –
1. If process P1 detects a coordinator failure, it creates new active list which is empty initially.
It sends election message to its neighbour on right and adds number 1 to its active list.
2. If process P2 receives message elect from processes on left, it responds in 3 ways:
● (I) If message received does not contain 1 in active list then P1 adds 2 to its active list
and forwards the message.
● (II) If this is the first election message it has received or sent, P1 creates new active list
with numbers 1 and 2. It then sends election message 1 followed by 2.
● (III) If Process P1 receives its own election message 1 then active list for P1 now
contains numbers of all the active processes in the system. Now Process P1 detects
highest priority number from list and elects it as the new coordinator.
Example:
We start with 6 processes, connected in a logical ring. Process 6 is the leader, as it has the highest
number.
Process 6 fails
Process 3 notices that Process 6 does not respond. So it starts an election, sending a message
containing its id to the next node in the ring.
Process 5 passes the message on, adding its own id to the message.
Process 0 passes the message on, adding its own id to the message.
Process 1 passes the message on, adding its own id to the message.
Process 4 passes the message on, adding its own id to the message.
When Process 3 receives the message back, it knows the message has gone around the ring, as its
own id is in the list. Picking the highest id in the list, it starts the coordinator message "5 is the
leader" around the ring.
class Anele{
static int n;
static int pro[] = new int[100];
static int sta[] = new int[100];
static int co;
int i,j,k,l,m;
for(i=0;i<n;i++)
{
[Link]("For process "+(i+1)+":");
[Link]("Status:");
sta[i]=[Link]();
[Link]("Priority");
pro[i] = [Link]();
}
elect(ele);
[Link]("Final coordinator is "+co);
}
Output:
Enter the number of process
7
For process 1:
Status:
1
Priority
1
For process 2:
Status:
1
Priority
2
For process 3:
Status:
1
Priority
3
For process 4:
Status:
1
Priority
4
For process 5:
Status:
1
Priority
5
For process 6:
Status:
1
Priority
6
For process 7:
Status:
0
Priority
7
Which process will initiate election?
4
Election message is sent from 4 to 5
Election message is sent from 5 to 6
Election message is sent from 6 to 7
Election message is sent from 5 to 7
Election message is sent from 4 to 6
Election message is sent from 6 to 7
Election message is sent from 4 to 7
Final coordinator is 6
}
public class Ring{
int noOfProcesses;
Process[] processes;
Scanner sc;
public Ring(){
sc=new Scanner([Link]);
}
public void initialiseRing(){
[Link](“Enter no of processes”);
noOfProcesses=[Link]();
processes = new Process[noOfProcesses];
for(int i=0;i<[Link];i++){
processes[i]= new Process(i);
}
}
}
}
return maxIdIndex;
}
public void performElection(){
while(true){
if(processes[next].active){
[Link](“Process “+processes[prev].id+” pass Election(“+processes[prev].id+”)
to”+processes[next].id);
prev=next;
}
next = (next+1)%noOfProcesses;
if(next == initiatorProcesss){
break;
}
}
[Link](“Process “+ processes[getMax()].id +” becomes coordinator”);
int coordinator = processes[getMax()].id;
prev = coordinator;
next =(prev+1)%noOfProcesses;
while(true){
if(processes[next].active)
{
[Link](“Process “+ processes[prev].id +” pass Coordinator(“+coordinator+ “)
message to process “+processes[next].id );
prev = next;
}
next = (next+1) % noOfProcesses;
if(next == coordinator)
{
[Link](“End Of Election “);
break;
}
}
Output:
C:\Users\Garry\Desktop\CLIX\Bully>java Bully
Enter No of Processes
5
Process no 4 fails
Process 0Passes Election(0) message to process 1
Process 0Passes Election(0) message to process 2
Process 0Passes Election(0) message to process 3
Process 1Passes Ok(1) message to process 0
Process 2Passes Ok(2) message to process 0
Process 3Passes Ok(3) message to process 0
Process 1Passes Election(1) message to process 2
Process 1Passes Election(1) message to process 3
Process 2Passes Ok(2) message to process 1
Process 3Passes Ok(3) message to process 1
Process 2Passes Election(2) message to process 3
Process 3Passes Ok(3) message to process 2
Finally Process 3 Becomes Coordinator
Process 3Passes Coordinator(3) message to process 2
Process 3Passes Coordinator(3) message to process 1
Process 3Passes Coordinator(3) message to process 0
End of Election
In single computer system, memory and other resources are shared between different processes.
The status of shared resources and the status of users is easily available in the shared memory so
with the help of shared variable (For example: Semaphores) mutual exclusion problem can be
easily solved.
In Distributed systems, we neither have shared memory nor a common physical clock and there
for we cannot solve mutual exclusion problem using shared variables. To eliminate the mutual
exclusion problem in distributed system approach based on message passing is used.
A site in distributed system does not have complete information of state of the system due to lack
of shared memory and a common physical clock.
● No Deadlock:
Two or more site should not endlessly wait for any message that will never arrive.
● No Starvation:
Every site who wants to execute critical section should get an opportunity to execute it in
finite time. Any site should not wait indefinitely to execute critical section while other site
are repeatedly executing critical section
● Fairness:
Each site should get a fair chance to execute critical section. Any request to execute critical
section must be executed in the order they are made i.e Critical section execution requests
should be executed in the order of their arrival in the system.
● Fault Tolerance:
As we know shared variables or a local kernel can not be used to implement mutual exclusion in
distributed systems. Message passing is a way to implement mutual exclusion. Below are the
three approaches based on message passing to implement mutual exclusion in distributed
systems:
Example:
● Suzuki-Kasami’s Broadcast Algorithm
● A site communicates with other sites in order to determine which sites should execute
critical section next. This requires exchange of two or more successive round of messages
among sites.
● This approach use timestamps instead of sequence number to order requests for the
critical section.
● When ever a site make request for critical section, it gets a timestamp. Timestamp is also
used to resolve any conflict between critical section requests.
● All algorithm which follows non-token based approach maintains a logical clock. Logical
clocks get updated according to Lamport’s scheme
Example:
● Lamport's algorithm, Ricart–Agrawala algorithm
● Instead of requesting permission to execute the critical section from all other sites, Each
site requests only a subset of sites which is called a quorum.
● Any two subsets of sites or Quorum contains a common site.
● This common site is responsible to ensure mutual exclusion
In a distributed system deadlock can neither be prevented nor avoided as the system is so vast
that it is impossible to do so. Therefore, only deadlock detection can be implemented. The
techniques of deadlock detection in the distributed system require the following:
● Progress –
The method should be able to detect all the deadlocks in the system.
● Safety –
There are three approaches to detect deadlocks in distributed systems. They are as follows:
I. Centralized approach –
In the centralized approach, there is only one responsible resource to detect deadlock. The
advantage of this approach is that it is simple and easy to implement, while the drawbacks
include excessive workload at one node, single-point failure (that is the whole system is
dependent on one node if that node fails the whole system crashes) which in turns makes the
system less reliable.
In the distributed approach different nodes work together to detect deadlocks. No single point
failure (that is the whole system is dependent on one node if that node fails the whole system
crashes) as the workload is equally divided among all nodes. The speed of deadlock detection
also increases.
III. Hierarchical approach –
This approach is the most advantageous. It is the combination of both centralized and
distributed approaches of deadlock detection in a distributed system. In this approach, some
selected nodes or clusters of nodes are responsible for deadlock detection and these selected
nodes are controlled by a single node.