0% found this document useful (0 votes)
23 views12 pages

Distributed System Lab Manual

The document discusses implementing remote procedure call (RPC) mechanisms in Java and Java RMI. It provides code samples to create a server and client for RPC using sockets in Java. It also provides code samples to create a server and client for Java RMI. The document then discusses simulating Lamport's logical clock algorithm in C programming language by creating physical and logical clocks. It provides a sample C program to do the same. Finally, the document asks to write a program in Java to simulate the Bully election algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
23 views12 pages

Distributed System Lab Manual

The document discusses implementing remote procedure call (RPC) mechanisms in Java and Java RMI. It provides code samples to create a server and client for RPC using sockets in Java. It also provides code samples to create a server and client for Java RMI. The document then discusses simulating Lamport's logical clock algorithm in C programming language by creating physical and logical clocks. It provides a sample C program to do the same. Finally, the document asks to write a program in Java to simulate the Bully election algorithm.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 12

1. Implement RPC mechanism for accessing methods of remote systems in ‘JAVA’.

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();

System.out.println("Enter first parameter :");


sendMessage2 = keyRead.readLine();

System.out.println("Enter second parameter : ");


sendMessage3 = keyRead.readLine();

pwrite.println(sendMessage1);
pwrite.println(sendMessage2);
pwrite.println(sendMessage3);

if((receiveMessage = receiveRead.readLine()) != null)


System.out.println(receiveMessage);
//}
}
}

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;

public class RmiServer extends UnicastRemoteObject implements RmiServerIntf {

public static final String MESSAGE = "Hello World";

public RmiServer() throws RemoteException {


super(0); // required to avoid the 'rmic' step, see below
}

public String getMessage() {


return MESSAGE;
}

public static void main(String args[]) throws Exception {


System.out.println("RMI server started");
try {
//special exception handler for registry creation
LocateRegistry.createRegistry(1099);
System.out.println("java RMI registry created.");
} catch (RemoteException e) {
// do nothing, error means registry already exists
System.out.println("java RMI registry already exists.");
}

//Instantiate RmiServer
RmiServer obj = new RmiServer();

// Bind this object instance to the name "RmiServer"


Naming.rebind("//localhost/RmiServer", obj);
System.out.println("PeerServer bound in registry");
}
}

3
Prepared By: Abhishesh Dahal, Dinesh Gothe
RmiClient.java

import java.rmi.Naming;

public class RmiClient {

public static void main(String args[]) throws Exception {


RmiServerIntf obj = (RmiServerIntf)Naming.lookup("//localhost/RmiServer");
System.out.println(obj.getMessage());
}
}

RmiServerIntf.java

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface RmiServerIntf extends Remote {


public String getMessage() throws 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();

printf("Enter the no. of physical clocks: ");


scanf("%d",&n);

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;
}
}

printf("\nPress a key for watching timestamp of physical clocks");


getch();

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();

clrscr(); x=0; for(i=0;i<10;i++)


for(j=0;j<n;j++)
for(k=0;k<num[j];k++)
if(b[j][k]==i)
{
x = rand() % 10 + x;
printf("Logical Clock Timestamp for process %c",a[j][k]);
printf(":%d ",x);
printf("\n");
}
getch();
return;
}

Output

Enter the no. of physical clocks: 2


No. of nodes for physical clock 1: 2
Enter the name of process: a
Enter the name of process: b

No. of nodes for physical clock 2: 2


Enter the name of process: c
Enter the name of process: d

Press a key for watching timestamp of physical clocks

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

Press a key for watching timestamp of logical clocks

Logical Clock Timestamp for process a: 6


Logical Clock Timestamp for process b: 13
Logical Clock Timestamp for process c: 18
Logical Clock Timestamp for process d: 23

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;

public static void main(String args[])throws IOException{


System.out.println("Enter the number of process");
Scanner in = new Scanner(System.in);
n = in.nextInt();

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);
}

static void elect(int ele){


ele = ele-1;
co = ele+1;
for(int i=0;i<n;i++)
{
if(pro[ele]<pro[i])
{
System.out.println("Election message is sent from "+(ele+1)+" to "+(i+1));
if(sta[i]==1)
elect(i+1);
}
}
}
}

7
Prepared By: Abhishesh Dahal, Dinesh Gothe
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

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.

Process 0 entered critical section.

Error: Another process is currently executing critical section.


Please wait till its execution is over.

Process 0 exits critical section.

Process 1 entered critical section.

Process 1 exits critical section.

Process 2 entered critical section.

Error: Another process is currently executing critical section.


Please wait till its execution is over.

Process 2 exits critical section.

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();

printf("Enter the number of frames: ");


scanf("%d", &noFrame);
for(i=0;i<noFrame;i++)
{
for(j=0;j<4;j++)
{
sendFrame[j]='0'+rand()%2;
}
printf("\n\nThe frame being sent is: %s",sendFrame);

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

You might also like