Distributed Systems Lab.docx_20241205_084733_0000
Distributed Systems Lab.docx_20241205_084733_0000
Distributed Systems Lab.docx_20241205_084733_0000
KCS- 751A
Academic Year: 2024- 25
Submitted to:
Er. Shweta Rajpoot
Submitted By:
Abhinav Sharma
(2200430109001)
Index
ll h C h fil h b d d i f b h
call the RPC. The IDL files can then be used to generate code to interface between the
client and server. The most common tool used for this is RPCGEN.
7.Chat Server
Chat server is a standlone application that is made up the combination of two-application,
server application (which runs on server side) and client application (which runs on client
side). This application is using for chatting in LAN. To start chatting you must be
connected with the server after that your message can broadcast to each and every client.
For making this application we are using some core java features like swing, collection,
networking, I/O Streams and threading also. In this application we have one server and any
number of clients (which are to be communicated with each other). For making a server we
have to run the MyServer file at any system on the network that we want to make server
and for client we have to run MyClient file on the system that we want to make client. For
running whole client operation we can run the Login.
There are five stages involved:
Step 1: A simple server that will accept a single client connection and display everything
the client says on the screen. If the client user types ".bye", the client and the server will
both quit.
Step 2: A server as before, but this time it will remain 'open' for additional connection once
a client has
quit. The server can handle at most one connection at a time.
Step 3: A server as before, but this time it can handle multiple clients simultaneously. The
output from all
connected clients will appear on the server's screen.
Step 4: A server as before, but this time it sends all text received from any of the connected
clients to all
clients. This means that the server has to receive and send, and the client has to send as well
as receive
Step 5: Wrapping the client from step 4 into a very simple GUI interface but not changing
the functionality
of either server or client. The client is implemented as an Applet, but a Frame would have
worked just as
well (for a stand-alone program).
Expirement -1
Write a program in c for implementation of non token base algorithm for distributed mutual
exclusion.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,d,p,a,c=0,aa[10],j,n;
char ch='y';
clrscr();
printf("enter no of processes");
scanf("%d",&n);
i=0;
do
{
printf("enter the process no which want to execute critical section");
scanf("%d",&a);
aa[i]=a;
i++;
c=c+1;
d=i;
printf("some other process want to execute cs? then press y");
fflush(0);
scanf("%c",&ch);
}
while(ch=='y');
for(j=1;j<=c;j++)
{
printf("\ncritical section is executing for process %d in queue......",j);
printf("\ncritical section is finished for process %d",j);
printf("\nrelease msg has sent by process%d",j);
}
getch();
}
Expirement -2
Write a program in C to implement Lamports logical clock
#include<stdio.h>
#include<conio.h>
struct process {
int e;
int ts[10];
}p[10];
void main() {
int i,j,n,m,t,e1,e2;
char ch;
clrscr();
printf("enter the no. of process");
scanf("%d",&n);
for(i=0;i<n;i++) {
printf("enter the no. of events in process %d",i+1);
scanf("%d",&p[i].e);
for(j=0;j<p[i].e;j++) { p[i].ts[j]=j+1; } }
for(i=0;i<n;i++) { for(j=0;j<p[i].e;j++) printf("%d ",p[i].ts[j]);
printf("\n");
}
do { printf("enter the process no & event no. from which message is passing (less than
%d)",n);
scanf("%d %d",&m,&e1);
printf("enter the process no & event no. on which msg is passing (less than %d)",n);
scanf("%d %d",&t,&e2);
if((p[m].ts[e1]+1)>p[t].ts[e2]) {
p[t].ts[e2]=p[m].ts[e1]+1;
for(i=e2;i<p[t].e;i++) { p[t].ts[i+1]=p[t].ts[i]+1; } }
printf("is there more message(y/n)");
fflush(0);
scanf("%c",&ch);
}while(ch=='y'&& ch=='Y');
for(i=0;i<n;i++) {
for(j=0;j<p[i].e;j++)
printf("%d ",p[i].ts[j]);
printf("\n"); }
getch();
}
Expirement -3
Write a program to implement edge chasing distributed deadlock detection algorithm.
#include<stdio.h>
#include<conio.h>
void main(){
int temp,process[10][15], site_count=0, process_count=0,i,j,k,waiting[15];
int p1,p2,p3;
clrscr();
printf("\n Enter the no. of sites (max 3) \n");
scanf("%d",&site_count);
for(i=1;i<=site_count;i++) {
printf("\n Enter the no. of processes in %d site ( max 4)\n",i);
scanf("%d",&process_count);
for(j=0;j<process_count;j++) {
process[i][j]=i+(i*j);
}
}
printf("\n Enter the blocked process \n");
scanf("%d",&k);
for(i=1;i<=3;i++) {
for(j=0;j<=3;j++)
{
if(k==process[i][j])
{
printf("Process %d is at site %d ",k,i);
temp=i;
}
if(k==process[i][j])
printf("It is a deadlock \n");
if(k==(process[temp][j])&&((process[temp][j])==waiting[process[i][j]]) && (temp!=i))
{
//probe(temp,j,process[i][j]);
if(process[i][j]==waiting[process[temp][j]]);
printf("It is a deadlock\n");
}
}
}getch();}
Expirement -4
Write a program in C to implement locking algorithm.
#include<stdio.h>
#include<conio.h>
void main() {
int a=0;
char b,c;
clrscr();
do
{
printf("if transaction T1 want to lock data object");
fflush(0);
scanf("%c",&b);
if(a==0 && b=='y') {
a=1;
b='n';
}
else if(a==1)
printf("data object is locked");
printf("if transaction T2 want to lock data object");
fflush(0);
scanf("%c",&b);
if(a==0 && b=='y') {
a=1;
b='n';
}
else
printf("data object is locked");
printf("\nif transaction want to release data object");
fflush(0);
scanf("%c",&b);
if(a==1 && b=='y')
a=0;
printf("do you want to continue");
fflush(0);
scanf("%c",&c);
}while(c=='y');
getch();
}
Expirement -5
Write a program to implement Remote Method Invocation
import java.rmi.*;
public class AddClient {
public static void main(String args[]) {
try
{
String addServerURL = "rmi://" + args[0] + "/Addserver";
AddServerIntf addServerIntf = (AddServerIntf)Naming.lookup(addServerURL);
System.out.println("the first number is :"+args[1]);
double d1 =Double.valueOf(args[1]).doubleValue();
System.out.println("the second number is :"+args[2]);
double d2 =Double.valueOf(args[2]).doubleValue();
System.out.println("The sum is:" + addServerIntf.add(d1,d2));
}
catch (Exception e)
{
System.out.println(" Exception:" +e);
}
}
}
//Add Server
import java.net.*;
import java.rmi.*;
public class AddServer
{
public static void main(String [] args)
{
try
{
AddServerImpl addServerImpl = new AddServerImpl();
Naming.rebind("Addserver",addServerImpl);
}
catch(Exception e)
{
System.out.println("Exception: " +e);
}
}
}
// Add Server IMPL
import java.rmi.*;
import java.rmi.server.*;
public class AddServerImpl extends UnicastRemoteObject implements AddServerIntf
{
public AddServerImpl() throws RemoteException
{
}
public double add(double d1, double d2) throws RemoteException
{
return d1+d2;
}}
//ADD SERVER INTF
import java.rmi.*;
public interface AddServerIntf extends Remote
{
double add(double d1, double d2) throws RemoteException;
}
Expirement -6
Write a Program to implement Remote Procedure Call.
#include <stdio.h>
#include <rpc/rpc.h> /* standard RPC include file */
#include "date.h" /* this file is generated by rpcgen */
main(int argc, char *argv[])
{
CLIENT *cl; /* RPC handle */
char *server;
long *lresult; /* return value from bin_date_1() */
char **sresult; /* return value from str_date_1() */
if (argc != 2) {
fprintf(stderr, "usage: %s hostname\n", argv[0]);
exit(1);
}
server = argv[1];
if ((cl = clnt_create(server, DATE_PROG, DATE_VERS, "udp")) == NULL) {
clnt_pcreateerror(server);
exit(2);
}
if ( (lresult = bin_date_1(NULL, cl)) == NULL) {
clnt_perror(cl, server);
exit(3);
}
printf("time on host %s = %ld\n",server, *lresult);
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);
}
program DATE_PROG {
version DATE_VERS {
long BIN_DATE(void) = 1;
string STR_DATE(long) = 2;
}=1
} = 0x31234567;
#include <time.h>
#include <rpc/rpc.h> /* standard RPC include file */
#include "date.h" /* this file is generated by rpcgen */
long *bin_date_1_svc(void *arg, struct svc_req *s)
{
static long timeval; /* must be static */
timeval = time((long *) 0);
return(&timeval);
}
char **str_date_1_svc(long *bintime, struct svc_req *s)
{
static char *ptr; /* must be static */
ptr = ctime((const time_t *)bintime); /* convert to local time */
return(&ptr);
}
Expirement -7
Write a program to implement Chat Server.
import java.net.*;
import java.io.*;
public class servertcp
{
public static void main(String args[]) throws IOException
{
ServerSocket ss=new ServerSocket(12);
Socket s=ss.accept();
System.out.println("Connection from "+s);
PrintWriter pw1=new PrintWriter(s.getOutputStream(),true);
BufferedReader br3=new BufferedReader(new InputStreamReader(s.getInputStream()));
BufferedReader br4=new BufferedReader(new InputStreamReader(System.in));
System.out.println("i m ready");
String s1, s2;
/*while((s1=br4.readLine())!=null)
{
pw1.println(s1);
System.out.println(s2);
}
*/
while(true)
{
do
{
s1=br4.readLine();
pw1.println(s1);
}
while(!s1.equals("over"));
do
{
s2=br3.readLine();
System.out.println(s2);
}
while(!s2.equals("over"));
}
}
}
// Client
import java.net.*;
import java.io.*;
public class clienttcp
{
public static void main(String args[]) throws IOException
{
Socket cc=new Socket(InetAddress.getLocalHost(),12);
PrintWriter pw=new PrintWriter(cc.getOutputStream(),true);
BufferedReader br1=new BufferedReader(new InputStreamReader(cc.getInputStream()));
BufferedReader br2=new BufferedReader(new InputStreamReader(System.in));
String str1, str2;
/*while((str1=br2.readLine())!=null)
{
System.out.println(str1);
//pw.println(str2);
}
*/
while(true)
{
do
{
str1=br1.readLine();
System.out.println(str1);
}
while(!str1.equals("over"));
do
{
str2=br2.readLine();
pw.println(str2);
}
while(!str2.equals("over"));
}
}
}
Expirement -8
Write a Program to implement termination detection
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<stdlib.h>
#include<math.h>
void main() {
int i,j,k=0,n,tw,total=0,we,ca,w[20];
clrscr();
printf("enter the no of process=");
scanf("%d",&n);
printf("\n\nassign a controlling agent=");
scanf("%d",&ca);
printf("\n\nenter the total weight=");
scanf("%d",&tw);
while((k<n)) {
randomize();
w[k]=random(tw);
tw=tw-w[k];
k++;
}
for(k=0;k<n;k++) {total=total+w[k];
}
printf("%d",total);
w[n-1]=abs(tw-total);
printf("%d",w[n-1]);
printf("\n\n\t\t\t\tControlling agent%d %d\n\n\n",ca,w[ca]);
printf("\n\nsending computational message to...\n\n");
for(j=0;j<n;j++)
{
if(j!=(ca-1))
{
sound(700);
delay(2000);
printf("\tprocess%d %d",j+1,w[j]);
}}
nosound();
getch();
}