Distributed System Practical File
Distributed System Practical File
#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: -
Implementation of Distributed Mutual Exclusion
import Utilities.*;
import Synchronization.*;
public void run() { // start three different threads in the same object
int meDo = whichOne++;
if (meDo == MAIN) {
new Thread(this).start();
main();
} else if (meDo == REQUESTS) {
new Thread(this).start();
handleRequests();
} else if (meDo == REPLIES) {
handleReplies();
}
}
Output: -
1.Server.java
import java.net.*;
import java.io.*;
public class server{
public static void main(String args[])throws IOException{
ServerSocket s1=null;
try{
s1=new ServerSocket(98);
}catch(Exception e){
System.out.println("Port not found");
e.printStackTrace();
}
Socket c=null;
try{
c=s1.accept();
System.out.println("Connection from"+c);
}catch(Exception e){
System.out.println("not accepted");
e.printStackTrace();
}
PrintWriter out=new PrintWriter(c.getOutputStream(),true);
BufferedReaderin=new BufferedReader(new InputStreamReader(c.getInputStream()));
String l;
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
System.out.println("I am ready type now");
while((l=sin.readLine())!=null){
out.println(l);
}
out.close();
sin.close();
c.close();
s1.close();
}
}
2.Client.java
import java.net.*;
import java.io.*;
public class client{
public static void main(String args[])throws IOException{
Socket s=null;
BufferedReader b=null;
try{
s=new Socket(InetAddress.getLocalHost(),98);
b=new BufferedReader(new InputStreamReader(s.getInputStream()));
}catch(Exception e){
System.out.println("I do not host");
e.printStackTrace();
}
String inp;
while((inp=b.readLine())!=null){
System.out.println(inp);
}
b.close();
s.close();
}
}
Output: -
D:\Sagar\RND\Java\NetWorking\ChatServer>java server
Connection fromSocket[addr=/127.0.0.1,port=1120,localport=98]
I am ready type now
Hello how r u? dude…
D:\Sagar\RND\Java\NetWorking\ChatServer>java client
Hello how r u? dude…
Implementation of ‘Java RMI’ Mechanism for Accessing Methods of
Remote Systems
1.CalculatorImpl.java
public class CalculatorImpl
extends
java.rmi.server.UnicastRemoteObject
implements Calculator {
public CalculatorImpl()
throws java.rmi.RemoteException {
super();
}
public long add(long a, long b)
throws java.rmi.RemoteException {
return a + b;
}
public long sub(long a, long b)
throws java.rmi.RemoteException {
return a - b;
}
public long mul(long a, long b)
throws java.rmi.RemoteException {
return a * b;
}
public long div(long a, long b)
throws java.rmi.RemoteException {
return a / b;
}
}
2.Calculator.java
public interface Calculator
extends java.rmi.Remote {
public long add(long a, long b)
throws java.rmi.RemoteException;
public long sub(long a, long b)
throws java.rmi.RemoteException;
public long mul(long a, long b)
throws java.rmi.RemoteException;
public long div(long a, long b)
throws java.rmi.RemoteException;
}
3.CalculatorServer.java
import java.rmi.Naming;
public class CalculatorServer {
public CalculatorServer() {
try {
Calculator c = new CalculatorImpl();
Naming.rebind("rmi://localhost:1099/CalculatorService", c);
} catch (Exception e) {
System.out.println("Trouble: " + e);
}
}
public static void main(String args[]) {
new CalculatorServer();
}
}
4.CalculatorClient.java
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
public class CalculatorClient {
public static void main(String[] args) {
try {
Calculator c = (Calculator)
Naming.lookup("rmi://localhost/CalculatorService");
System.out.println( c.sub(4, 3) );
System.out.println( c.add(4, 5) );
System.out.println( c.mul(3, 6) );
System.out.println( c.div(9, 3) );
}
catch (MalformedURLException murle) {
System.out.println();
System.out.println("MalformedURLException");
System.out.println(murle);
}
catch (RemoteException re) {
System.out.println();
System.out.println("RemoteException");
System.out.println(re);
}
catch (NotBoundException nbe) {
System.out.println();
System.out.println(
"NotBoundException");
System.out.println(nbe);
}
catch ( java.lang.ArithmeticException ae) {
System.out.println();
System.out.println(
"java.lang.ArithmeticException");
System.out.println(ae);
}
}
}
Output:
If all goes well you will see the following output:
1
9
18
3
Implementation of CORBA (Common Object Request Broker
Architecture) Mechanism
1.FileInterface.idl
interface FileInterface {
typedef sequence<octet> Data;
Data downloadFile(in string fileName);
};
Now, let's compile the FileInterface.idl and generate server-side skeletons. Using the
command:
2.FileServant.java
import java.io.*;
3.FileServer.java
import java.io.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
4.FileClient.java
import java.io.*;
import java.util.*;
import org.omg.CosNaming.*;
import org.omg.CORBA.*;
if(argv.length < 1) {
System.out.println("Usage: java FileClient filename");
}
1. D:\Sagar\RND\Java\CORBA>tnameserv
2. D:\Sagar\RND\Java\CORBA>java FileServer
3. D:\Sagar\RND\Java\CORBA>idlj -fclient FileInterface.idl
4. D:\Sagar\RND\Java\CORBA>java FileClient hello.txt
Output:
Implementation of RPC Mechanism for a File Transfer across a
network in ‘C’
/*
* rdate.c client program for remote date program
*/
#include <stdio.h>
#include <rpc/rpc.h> /* standard RPC include file */
#include "date.h" /* this file is generated by rpcgen */
/*
* Create client handle
*/
if ((cl = clnt_create(server, DATE_PROG, DATE_VERS, "udp")) == NULL) {
/*
* can’t establish connection with server
*/
clnt_pcreateerror(server);
exit(2);
}
/*
* First call the remote procedure "bin_date".
*/
/*
* Now call the remote procedure str_date
*/
if ( (sresult = str_date_1(lresult, cl)) == NULL) {
clnt_perror(cl, server);
exit(4);
}
printf("time on host %s = %s", server, *sresult);
clnt_destroy(cl); /* done with the handle */
exit(0);
}
/*
* date.x Specification of the remote date and time server
*/
/*
* Define two procedures
* bin_date_1() returns the binary date and time (no arguments)
* str_date_1() takes a binary time and returns a string
*
*/
program DATE_PROG {
version DATE_VERS {
long BIN_DATE(void) = 1; /* procedure number = 1 */
string STR_DATE(long) = 2; /* procedure number = 2 */
} = 1; /* version number = 1 */
} = 0x31234567;
/*
* dateproc.c remote procedures; called by server stub
*/
#include <time.h>
#include <rpc/rpc.h> /* standard RPC include file */
#include "date.h" /* this file is generated by rpcgen */
/*
* Return the binary date and time
*/