Distributed System Lab Manual
Distributed System Lab Manual
Ser.java
import java.io.*;
import java.net.*;
class ser
{
public static void main(String[] args) throws Exception
{
ServerSocket sersock = new ServerSocket(3000);
System.out.println("Server ready");
Socket sock = sersock.accept( );
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);
InputStream istream = sock.getInputStream();
BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
String fun;
int a,b,c;
//while(true)
//{
fun = receiveRead.readLine();
a = Integer.parseInt(receiveRead.readLine());
b = Integer.parseInt(receiveRead.readLine());
if(fun != null)
System.out.println("Operation : "+fun);
System.out.println("Parameter 1 : "+a);
System.out.println("Parameter 2 : "+b);
if(fun.compareTo("add")==0)
{
c=a+b;
System.out.println("Addition = "+c);
pwrite.println("Addition = "+c);
}
if(fun.compareTo("sub")==0)
{
c=a-b;
System.out.println("Substraction = "+c);
pwrite.println("Substraction = "+c);
}
if(fun.compareTo("mul")==0)
{
c=a*b;
System.out.println("Multiplication = "+c);
pwrite.println("Multiplication = "+c);
}
if(fun.compareTo("div")==0)
1
Prepared By: Abhishesh Dahal, Dinesh Gothe
{
c=a/b;
System.out.println("Division = "+c);
pwrite.println("Division = "+c);
}
System.out.flush();
//}
}
}
Cli.java
import java.io.*;
import java.net.*;
class cli
{
public static void main(String[] args) throws Exception
{
Socket sock = new Socket("localhost", 3000);
BufferedReader keyRead = new BufferedReader(new InputStreamReader(System.in));
OutputStream ostream = sock.getOutputStream();
PrintWriter pwrite = new PrintWriter(ostream, true);
InputStream istream = sock.getInputStream();
BufferedReader receiveRead = new BufferedReader(new InputStreamReader(istream));
System.out.println("Client ready, type and press Enter key");
String receiveMessage, sendMessage1,sendMessage2, sendMessage3, temp;
//while(true)
//{
System.out.println("\nEnter operation to perform(add,sub,mul,div)....");
temp = keyRead.readLine();
sendMessage1=temp.toLowerCase();
pwrite.println(sendMessage1);
pwrite.println(sendMessage2);
pwrite.println(sendMessage3);
2
Prepared By: Abhishesh Dahal, Dinesh Gothe
2. Implement ‘JAVA RMI’ mechanism for accessing methods of remote systems.
RmiServer.java
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.server.UnicastRemoteObject;
//Instantiate RmiServer
RmiServer obj = new RmiServer();
3
Prepared By: Abhishesh Dahal, Dinesh Gothe
RmiClient.java
import java.rmi.Naming;
RmiServerIntf.java
import java.rmi.Remote;
import java.rmi.RemoteException;
4
Prepared By: Abhishesh Dahal, Dinesh Gothe
3. Write a program to simulate the functioning of Lamport’s logical clock in 'C’.
Lamport.c
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main()
{
int i,j,k;
int x=0;
char a[10][10];
int n,num[10],b[10][10];
clrscr();
for(i=0;i<n;i++)
{
printf("\nNo. of nodes for physical clock %d: ",i+1);
scanf("%d",&num[i]);
x=0;
for(j=0;j<num[i];j++)
{
printf("\nEnter the name of process: ");
scanf("%s",&a[i][j]);
b[i][j]=x + rand() % 10;
x=b[i][j]+1;
}
}
clrscr();
for(i=0;i<n;i++)
{
printf("Physical Clock %d: ",i+1);
for(j=0;j<num[i];j++)
{
printf("\nProcess: %c ",a[i][j]);
printf(" has P.T. : %d ",b[i][j]);
printf("\n");
}
5
Prepared By: Abhishesh Dahal, Dinesh Gothe
}
printf("Press a key for watching timestamp of logical clocks");
getch();
Output
Physical Clock 1
Process a has P.T.: 6
Process b has P.T.: 7
Physical Clock 2
Process c has P.T.: 2
Process d has P.T.: 3
6
Prepared By: Abhishesh Dahal, Dinesh Gothe
4. Write a program to simulate the Election Algorithm (Bully Algorithm) in Java.
Bully.java
import java.io.*;
import java.util.Scanner;
class Bully{
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++)
{
System.out.println("For process "+(i+1)+":");
System.out.println("Status:");
sta[i]=in.nextInt();
System.out.println("Priority");
pro[i] = in.nextInt();
}
System.out.println("Which process will initiate election?");
int ele = in.nextInt();
elect(ele);
System.out.println("Final coordinator is "+co);
}
7
Prepared By: Abhishesh Dahal, Dinesh Gothe
Output
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
Final coordinator is 6
8
Prepared By: Abhishesh Dahal, Dinesh Gothe
5. Write a program to simulate the Distributed Mutual Exclusion in ‘C’.
MutualExclusion.c
#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++;
9
Prepared By: Abhishesh Dahal, Dinesh Gothe
t1 = time(NULL);
}
}
}
}
Output
Press a key (except q) to enter a process into critical section. Press q at any time to exit.
10
Prepared By: Abhishesh Dahal, Dinesh Gothe
6. Write a code in ‘C’ to implement sliding window protocol.
Sliding.c
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>.
void main()
{
char sendFrame[4],receivedFrame[4],b;
int acknowledge[4];
int i,j,noFrame,sent,totalSent=0;
clrscr();
retrysend:
for(j=0;j<4;j++)
{
if(rand()%500>80)
{
acknowledge[j]=1;
receivedFrame[j]=sendFrame[j];
}
else
{
acknowledge[j]=0;
receivedFrame[j]='x';
}
}
sent=1;
for(j=0;j<4;j++)
{
if(acknowledge[j]==0)
sent=0;
11
Prepared By: Abhishesh Dahal, Dinesh Gothe
delay(40);
printf("\nAcknowlegment for %d",j);
printf("th bit was: %d",acknowledge[j]);
}
receivedFrame[4]=NULL;
printf("\nThe frame received was:%s ",receivedFrame);
if(sent==1)
{
printf("\nThe frame sent was sent successfully ");
getch();
}
else
{
printf("\nThe frame was not sent!");
}
if(sent==0)
{
printf("\nDo you want to retry sending frame (y/n) ");
scanf("%c",&b);
if(b=='y' || b=='Y')
goto retrysend;
}
if(sent==1)
totalSent++;
}
printf("\n\nTotal Frames to be sent: %d",noFrame);
printf("\nTotal Frames sent successfully:%d ",totalSent);
getch();
}
12
Prepared By: Abhishesh Dahal, Dinesh Gothe