DS Lab 2021
DS Lab 2021
Department of
Computer Science and Engineering
Distribute System
(Lab File)
Submitted by:
if (a > b)
return a;
else
return b;
}
if (m[i][j] == 1) {
p2[j] = max1(p2[j], p1[i] + 1);
for (k = j + 1; k < e2; k++)
p2[k] = p2[k - 1] + 1;
}
if (m[i][j] == -1) {
p1[i] = max1(p1[i], p2[j] + 1);
for (k = i + 1; k < e1; k++)
p1[k] = p1[k - 1] + 1;
}
}
}
int main()
{
int e1 = 5, e2 = 3, m[5][3];
m[0][0] = 0;
m[0][1] = 0;
m[0][2] = 0;
m[1][0] = 0;
m[1][1] = 0;
m[1][2] = 1;
m[2][0] = 0;
m[2][1] = 0;
m[2][2] = 0;
m[3][0] = 0;
m[3][1] = 0;
m[3][2] = 0;
m[4][0] = 0;
m[4][1] = -1;
m[4][2] = 0;
return 0;
}
Ques 2 : WAP to implement vector clock
#include<stdio.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
long *p1(int i,long *comp);
long *p2(int i,long *comp);
long *p3(int i,long *comp);
void main()
{
long start[]={0,0,0},*vector;
clrscr();
while(!kbhit())
{
p1(1,&start[0]);
}
printf(“\n Process Vector\n”);
vector=p1(0,&start[0]);
printf(“p1[%ld%ld%ld]\n”,*vector,*(vector+1),*(vector+2));
vector=p2(0,&start[0]);
printf(“p2[%ld%ld%ld]\n”,*vector,*(vector+1),*(vector+2));
vector=p3(0,&start[0]);
printf(“p3[%ld%ld%ld]\n”,*vector,*(vector+1),*(vector+2));
}
long *p1(int i,long *comp)
{
static long a[]={0,0,0};
int next;
if(i==1)
{
a[0]++;
if(*(comp+1)>a[1])
a[1]=*(comp+1);
if(*(comp+2)>a[2])
a[2]=*(comp+2);
next=random(2);
if(next==0)
p2(1,&a[0]);
else if(next==1)
p3(1,&a[0]);
return(&a[0]);
}
else
return(&a[0]);
}
long *p2(int i,long *comp)
{
static long b[]={0,0,0};
int next;
if(i==1)
{
b[i]++;
if(*comp>b[0])
b[0]=*(comp);
if(*(comp+2)>b[2])
b[2]=*(comp+2);
next=random(2);
if(next==0)
p1(1,&b[0]);
else if(next==1)
p3(1,&b[0]);
return &b[0];
}
else
return &b[0];
}
long *p3(int i,long *comp)
{
static long c[]={0,0,0};
int next;
if(i==1)
{
c[2]++;
if(*comp>c[0])
c[0]=*(comp);
if(*(comp+1)>c[1])
c[1]=*(comp+1);
next=random(2);
if(next==0)
p1(1,&c[0]);
return &c[0];
}
else
return &c[0];
}
Ques 3 : Distributed application using RMI where client
send msg and server prints as output
import java.rmi.RMISecurityManager;
import java.rmi.Naming;
import java.rmi.RemoteException;
public class HelloClient
{
public static void main(String arg[])
{
String message = "blank";
// I download server's stubs ==> must set a SecurityManager
System.setSecurityManager(new RMISecurityManager());
try
{
Hello obj = (Hello) Naming.lookup( "//" +
"lysander.cs.ucsb.edu" +
"/HelloServer"); //objectname in registry
System.out.println(obj.sayHello());
}
catch (Exception e)
{
System.out.println("HelloClient exception: " + e.getMessage());
e.printStackTrace();
}
}
}
Ques 4 : Distributed application using RMI where client
submit 2 strings and server returns the concatenation
// import java.sql.*;
import java.rmi.*;
import java.io.*;
import java.util.*;
import java.util.Vector.*;
import java.lang.*;
import java.rmi.registry.*;
public class Client
{
static String name1,name2,name3; public static void main(String args[])
{
Client c=new Client();
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
int ch;
try { Registry r1 = LocateRegistry.getRegistry ( "localhost", 1030);
DBInterface DI=(DBInterface)r1.lookup("DBServ");
do
{
System.out.println("1.send input stings\n2.Display concatenated string \nEnter ur
choice");
ch= Integer.parseInt(b.readLine());
switch(ch)
{
case 1: System.out.println(" \n Enter first string:");
name1=b.readLine();
System.out.println(" \n Enter second string:");
name2=b.readLine();
name3=DI.input(name1,name2);
break;
case 2: //display System.out.println("\n Concatenated String is : ");
int i=0;
System.out.println(" " +name3+"");
break;
}
}
while(ch>0);
}
catch (Exception e)
{
// System.out.println("ERROR: " +e.getMessage());
}}}
Ques 5 : Distributed application using RMI where client
submit string and server return its reverse as output
import java.rmi.*;
public interface StrRev extends Remote
{
public String reverse(String str) throws RemoteException;
}
import java.rmi.*;
import java.rmi.server.*;
import java.util.*;
public class StrRev_Impli extends UnicastRemoteObject implements StrRev
{
public StrRev_Impli() throws RemoteException
{}
import java.rmi.*;
import java.net.*;
public class StrRev_Server
{
public static void main(String args[])
{
try
{
StrRev_Impli strimpli=new StrRev_Impli();
Naming.rebind("RmiReverse",strimpli);
}
catch(Exception ex){}
}
}
import java.io.*;
import java.rmi.*;
import java.net.*;
public class StrRev_Client
{
public static void main(String args[])
{
try
{
String url="rmi://127.0.0.1/RmiReverse";
StrRev strf=(StrRev)Naming.lookup(url);
// Client program
import java.io.*;
import java.net.*;
class GFG {
// driver function
public static void main(String[] args)
{
try {
// Create socket object by passing id address
// and port number establish connection
Socket socket = new Socket("localhost", 1346);
System.out.println(
"Connected Successfully.....")
// Buffer reader to get all the input stream
BufferedReader bs = new BufferedReader(
new InputStreamReader(s.getInputStream()));
System.out.println("Response from Server.....");
// Print response from server
System.out.println("Client Side : "
+ br.readLine());
// Close the connection
socket.close();
}
catch (UnknownHostException e) {
// Catch block for IP errors
System.out.println("IP not found for" + e);
}
catch (IOException e) {
// Catch block for data stream errors
System.out.println("Not found data for socket"
+ e);
}
}
}
Ques 7 : Distributed application using socket
programming where client submit an int and server
return its factorial as output
import java.io.*;
import java.net.*;
class factclient{
public static void main(String argv[]) throws Exception
{
String n;
DatagramSocket clientSocket = new DatagramSocket();
byte []send= new byte [102];
byte []resive=new byte [102];
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
System.out.println(”\nEnter Number :”);
n=inFromUser.readLine();
InetAddress ipadd= InetAddress.getByName(”localhost”);
send=n.getBytes();
DatagramPacketsendPck=new DatagramPacket(send,send.length,ipadd,6870);
clientSocket.send(sendPck);
DatagramPacket resPck=new DatagramPacket(resive,resive.length);
clientSocket.receive(resPck);
String fact=new String(resPck.getData());
System.out.println(”FROM SERVER:” +n+”!=”+fact);
clientSocket.close();
}
}
Ques 8 : WAP to implement mutual exclusion
algorithm.
#include <pthread.h>
#include <stdio.h>
int count = 0;
pthread_mutex_thread_lock;
void*run_thread()
{
pthread_mutex_lock(&thread_lock);
pthread_thread_id = pthread_self();
printf(“Thread%u: Current value of count=%d\n”,thread_id,count);
printf(:Thread%u incrementing count…\n”); count++; sleep(1);
printf(“Value of count after incremented by thread%u= %d\n”,thread_id,count);
pthread_mutex_unlock(&thread_lock);
pthread_exit(NULL);
}
int main(int argc, char*argv[])
{
pthread_thread_array[4];
int i=0,ret, thread_num = 4;
for(i=0; i<thread_num;i++)
{
If((ret = pthread_create(&thread_array[i],NULL,run_thread, NULL)== -1)
{
printf(“Thread creation failed with return code: %d”,ret);
exit(ret);
}
}
pthread_exit(NULL);
}