Distributed System Lab Manual File Final
Distributed System Lab Manual File Final
Type of Algorithm: The algorithm of Lamport’s Logical Clock is a simple algorithm used to determine the order of events in a
distributed computer system.
Algorithm:
the "time" concept in distributed systems -- used to order events in a distributed system.
assumption:
o the execution of a process is characterized by a sequence of events. An event can be the execution of one instruction or
of one procedure.
o sending a message is one event, receiving a message is one event.
The events in a distributed system are not total chaos. Under some conditions, it is possible to ascertain the order of the events.
Lamport's logical clocks try to catch this.
AB if A andB are within the same process (same sequential thread of control) and A occurred before B.
if AB and BC then AC
The value of d could be 1, or it could be an approximation to the elapsed real time. For example, we could take d1 to be the elapsed
local time, and d2 to be the estimated message transmission time. The latter solves the problem of waiting forever for a virtual time
instant to pass.
Program Code:
#include<stdio.h>
#include<conio.h>
int max1(int a, int b) //to find the maximum timestamp between two events
{
if (a>b)
return a;
else
return b;
}
int main()
{
int i,j,k,p1[20],p2[20],e1,e2,dep[20][20];
printf("enter the events : ");
scanf("%d %d",&e1,&e2);
for(i=0;i<e1;i++)
p1[i]=i+1;
for(i=0;i<e2;i++)
p2[i]=i+1;
printf("enter the dependency matrix:\n");
printf("\t enter 1 if e1->e2 \n\t enter -1, if e2->e1 \n\t else enter 0 \n\n");
for(i=0;i<e2;i++)
printf("\te2%d",i+1);
for(i=0;i<e1;i++)
{
printf("\n e1%d \t",i+1);
for(j=0;j<e2;j++)
scanf("%d",&dep[i][j]);
}
for(i=0;i<e1;i++)
{
for(j=0;j<e2;j++)
{
if(dep[i][j]==1) //change the timestamp if dependency exist
{ p2[j]=max1(p2[j],p1[i]+1);
for(k=j;k<e2;k++)
p2[k+1]=p2[k]+1;
}
if(dep[i][j]==-1) //change the timestamp if dependency exist
{
p1[i]=max1(p1[i],p2[j]+1);
for(k=i;k<e1;k++)
p2[k+1]=p1[k]+1;
}
}
}
printf("P1 : "); //to print the outcome of Lamport Logical Clock
for(i=0;i<e1;i++)
{
printf("%d",p1[i]);
}
printf("\n P2 : ");
for(j=0;j<e2;j++)
printf("%d",p2[j]);
getch();
return 0 ;
}
Output 1:
Experiment: 2
Type of Algorithm: Huang's algorithm is an algorithm for detecting termination in a distributed system.
Algorithm:
Some weaknesses to Huang's algorithm are that it is unable to detect termination if a message is lost in transit or if a process
fails while in an active state.
Program Code:
#include<stdio.h>
#include<conio.h>
float W_fm,W_f1,W_f2;
void f2()
{
Int a,b;
W_f2=0.2;
printf("\n\nFUNCTION F2 STARTED: W_f2=%f",W_f2);
a=10; b=4;
printf("\nA=%d B=%d\nDifference(A-B)=%d",a,b,a-b);
W_f2=0.0;
printf("\nFUNCTION F2 TERMINITATED: W_f2=%f",W_f2);
}
void f1()
{
Int a,b;
Float W_f1=0.5;
printf("\n\nFUNCTION F1 STARTED: W_f1=%f",W_f1);
a=10; b=4;
printf("\nA=%d B=%d\nSum(A+B)=%d",a,b,a+b);
float W_f1=0.2;
printf("\nFUNCTION F1 HALTS: W_f1=%f",W_f1);
f2();
W_f1=0.5;
printf("\n\nFUNCTION F1 RESTARTED: W_f1=%f",W_f1);
W_f1=0.0;
printf("\nFUNCTION F1 TERMINITATED: W_f1=%f",W_f1);
}
int main(){
float W_fm=1;
printf("MAIN FUNCTION STARTED: W_fm=%f ",W_fm);
float W_fm=0.5;
printf("\nMAIN FUNCTION HALTS: W_fm=%f",W_fm);
f1();
W_fm=1;
printf("\n\nMAIN FUNCTION RESTARTED: W_fm=%f",W_fm);
W_fm=0;
getch();
}
Output : 2
Experiment: 3
Objective: Implement data transfer between client-server using socket programming.
Program Code:
package com.journaldev.socket;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.ClassNotFoundException;
import java.net.ServerSocket;
import java.net.Socket;
}
Java Socket Client
package com.journaldev.socket;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
Program Code:
Server Code
import java.io.*;
import java.net.*;
import java.util.*;
public class Server
{
private static Socket socket;
public static void main(String[] args)
{
try
{
int port = 2411;
ServerSocketserverSocket = new ServerSocket(port);
System.out.println("Server Started and listening to the port 2411");
while(true)
{
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReaderisr = new InputStreamReader(is);
BufferedReaderbr = new BufferedReader(isr);
String number = br.readLine();
System.out.println("Message received from client is "+number);
String returnMessage;
intnum = Integer.parseInt(number);
if(num==1)
{
Date time = new Date();
OutputStreamos = socket.getOutputStream();
ObjectOutputStreamoutputStream = new ObjectOutputStream(os);
outputStream.writeObject(time);
outputStream.flush();
System.out.println("Current time in millisec is "+time.getTime());
System.out.println("Current time of system is "+time);
}
else
{
returnMessage = "Invalid Input" + "\n";
OutputStreamos = socket.getOutputStream();
OutputStreamWriterosw = new OutputStreamWriter(os);
BufferedWriterbw = new BufferedWriter(osw);
bw.write(returnMessage);
System.out.println("Message sent to the client is "+returnMessage);
bw.flush();
}
}
}
catch (Exception e)
{ e.printStackTrace();
}
finally
{
try
{
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
Client Code
import java.io.*;
import java.net.*;
import java.util.*;
public class Client {
private static Socket socket;
public static void main(String args[])
{
try
{
String host = "localhost";
int port = 2411;
InetAddress address = InetAddress.getByName(host);
socket = new Socket(address, port);
OutputStreamos = socket.getOutputStream();
OutputStreamWriterosw = new OutputStreamWriter(os);
BufferedWriterbw = new BufferedWriter(osw);
String number = "1";
String sendMessage = number + "\n";
bw.write(sendMessage);
bw.flush();
System.out.println("Message sent to the server : "+sendMessage);
InputStream is = socket.getInputStream();
ObjectInputStreaminStream = new ObjectInputStream(is);
Date time =(Date)inStream.readObject();
Date current = new Date();
long t1 = time.getTime();
long t2 = current.getTime();
long t3 = t2+t1;
t3 = t3/2;
System.out.println("time received from the server : " +t1);
System.out.println("Current time of client is : " +t2);
System.out.println("Average time in millisec is : " +t3);
time.setTime(t3);
System.out.println("So current time is : " +time);
}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
try
{
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
Output: 4
Experiment: 5
Program Code:
Interface
import java.rmi.Remote;
import java.rmi.RemoteException;
Class
// Implementing the remote interface
public class ImplExample implements Hello {
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
RMI client program
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
Output: 5
Feature:
Communication:
Inter process communication in Coda is performed using RPCs. RPC2system for Coda is much more sophisticated than
traditional RPC.
Process:
Coda uses a local cache to provide access to server data when the network connection is lost. During normal operation, a
user reads and writes to the file system normally, while the client fetches, or "hoards", all of the data the user has listed
as important in the event of network disconnection.
Naming & Location:
The name space in Coda is hieratically structured as in UNIX and is partitioned into disjoint volumes.
A VOLUME consists of a set of files & directories located on one server, & is the unit of replication in coda.
Each files & directory is defined by a 96 bit long unique File Identifier (FID), Replicas of a file have same FID.
It corresponds to a partial sub tree in the shared name space as maintained by the Vice servers.
File Identifiers:
Considering that the collection of shared files may be replicated and distributed across multiple Vice servers, it
becomes important to uniquely identify each file in such a way that it can be tracked to its physical location, while at the
same time maintaining replication and location transparency.
Program:
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<time.h>
void main()
{
int cs=0,pro=0;
double run=5;
char key='a';
time_t t1,t2;
clrscr();
printf("Press a key(except q) to enter a process into critical section.");
printf(" \nPress q at any time to exit.");
t1 = time(NULL) - 5;
while(key!='q')
{
while(!kbhit())
if(cs!=0)
{
t2 = time(NULL);
if(t2-t1 > run)
{
printf("Process%d ",pro-1);
printf(" exits critical section.\n");
cs=0;
}
}
key = getch();
if(key!='q')
{
if(cs!=0)
printf("Error: Another process is currently executing critical section
Please wait till its execution is over.\n");
else
{
printf("Process %d ",pro);
printf(" entered critical section\n");
cs=1;
pro++;
t1 = time(NULL);
}
}
}
}
Output:7
Press a key(except q) to enter a process into critical section.
Press q at any time to exit.
Objective: Implement RPC mechanism for a file transfer across a network in ‘C’
Program:
Server Code
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define IP_PROTOCOL 0
#define PORT_NO 15050
#define NET_BUF_SIZE 32
#define cipherKey 'S'
#define sendrecvflag 0
#define nofile "File Not Found!"
// function to encrypt
char Cipher(char ch)
{
return ch ^ cipherKey;
}
// driver code
int main()
{
int sockfd, nBytes;
struct sockaddr_in addr_con;
int addrlen = sizeof(addr_con);
addr_con.sin_family = AF_INET;
addr_con.sin_port = htons(PORT_NO);
addr_con.sin_addr.s_addr = INADDR_ANY;
char net_buf[NET_BUF_SIZE];
FILE* fp;
// socket()
sockfd = socket(AF_INET, SOCK_DGRAM, IP_PROTOCOL);
if (sockfd < 0)
printf("\nfile descriptor not received!!\n");
else
printf("\nfile descriptor %d received\n", sockfd);
// bind()
if (bind(sockfd, (struct sockaddr*)&addr_con, sizeof(addr_con)) == 0)
printf("\nSuccessfully binded!\n");
else
printf("\nBinding Failed!\n");
while (1) {
printf("\nWaiting for file name...\n");
fp = fopen(net_buf, "r");
printf("\nFile Name Received: %s\n", net_buf);
if (fp == NULL)
printf("\nFile open failed!\n");
else
printf("\nFile Successfully opened!\n");
while (1) {
// process
if (sendFile(fp, net_buf, NET_BUF_SIZE)) {
sendto(sockfd, net_buf, NET_BUF_SIZE,
sendrecvflag,
(struct sockaddr*)&addr_con, addrlen);
break;
}
// send
sendto(sockfd, net_buf, NET_BUF_SIZE,
sendrecvflag,
(struct sockaddr*)&addr_con, addrlen);
clearBuf(net_buf);
}
if (fp != NULL)
fclose(fp);
}
return 0;
}
Client Code
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define IP_PROTOCOL 0
#define IP_ADDRESS "127.0.0.1" // localhost
#define PORT_NO 15050
#define NET_BUF_SIZE 32
#define cipherKey 'S'
#define sendrecvflag 0
// driver code
int main()
{
int sockfd, nBytes;
struct sockaddr_in addr_con;
int addrlen = sizeof(addr_con);
addr_con.sin_family = AF_INET;
addr_con.sin_port = htons(PORT_NO);
addr_con.sin_addr.s_addr = inet_addr(IP_ADDRESS);
char net_buf[NET_BUF_SIZE];
FILE* fp;
// socket()
sockfd = socket(AF_INET, SOCK_DGRAM,
IP_PROTOCOL);
if (sockfd < 0)
printf("\nfile descriptor not received!!\n");
else
printf("\nfile descriptor %d received\n", sockfd);
while (1) {
printf("\nPlease enter file name to receive:\n");
scanf("%s", net_buf);
sendto(sockfd, net_buf, NET_BUF_SIZE,
sendrecvflag, (struct sockaddr*)&addr_con,
addrlen);
printf("\n---------Data Received---------\n");
while (1) {
// receive
clearBuf(net_buf);
nBytes = recvfrom(sockfd, net_buf, NET_BUF_SIZE,
sendrecvflag, (struct sockaddr*)&addr_con,
&addrlen);
// process
if (recvFile(net_buf, NET_BUF_SIZE)) {
break;
}
}
printf("\n-------------------------------\n");
}
return 0;
}
Output: 8
Server
Client
Index
8
Implement RPC mechanism for a file transfer across a network in ‘C’