Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 24

JSS MAHAVIDYAPEETHA

LABORATORY MANUAL

Subject Name: Distributed System Lab

Subject Code: NCS-751

COURSE : B.Tech SEMESTER : VII SEM

Name

Roll No.

Section-Batch

Department of Computer Science and Engineering


JSS ACADEMY OF TECHNICAL EDUCATION
C-20/1, SECTOR-62, NOIDA

Name: Rajat Sharma Roll No: 1609110914 Batch:7th-CS1-B1


VISION OF THE INSTITUTE
JSS Academy of Technical Education Noida aims to become an Institution of excellence in imparting
quality Outcome Based Education that empowers the young generation with Knowledge,
Skills,Research, Aptitude and Ethical values to solve Contemporary Challenging Problems.

MISSION OF THE INSTITUTE


 Develop a platform for achieving globally acceptable level of intellectual acumen and technological
competence.

 Create an inspiring ambience that raises the motivation level for conducting quality research.

 Provide an environment for acquiring ethical values and positive attitude.

VISION OF THE DEPARTMENT


To spark the imagination of the Computer Science Engineers with values, skills and creativity to solve
the real world problems.

MISSION OF THE DEPARTMENT


 To inculcate creative thinking and problem solving skills through effective teaching, learning and
research.

 To empower professionals with core competency in the field of Computer Science and Engineering.

 To foster independent and life long learning with ethical and social responsibilities.

PROGRAM EDUCATIONAL OUTCOMES (PEOs)


PEO1: To empower students with effective computational and problem solving skills.
PEO2: To enable students with core skills for employment and entrepreneurship.
PEO3: To imbibe students with ethical values and leadership qualities.
PEO4: To foster students with research oriented ability which helps them in analyzing and solving real
life problems and motivate them for pursuing higher studies.

Name: Rajat Sharma Roll No: 1609110914 Batch:7th-CS1-B1


PROGRAM OUTCOMES (POs)
Engineering Graduates will be able to:

PO1: Engineering knowledge: Apply the knowledge of mathematics, science, engineering


fundamentals, and an engineering specialization to the solution of complex engineering problems.

PO2: Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.

PO3: Design/development of solutions: Design solutions for complex engineering problems


and design system components or processes that meet the specified needs with appropriate
consideration for the public health and safety, and the cultural, societal, and environmental
considerations.

PO4: Conduct investigations of complex problems: Use research-based knowledge and


research methods including design of experiments, analysis and interpretation of data, and
synthesis of the information to provide valid conclusions.

PO5: Modern tool usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modeling to complex engineering
activities with an understanding of the limitations.

PO6: The engineer and society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent responsibilities
relevant to the professional engineering practice.

PO7: Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and need
for sustainable development.
PO8: Ethics: Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.

PO9: Individual and team work: Function effectively as an individual, and as a member or
leader in diverse teams, and in multidisciplinary settings.

PO10: Communication: Communicate effectively on complex engineering activities with the


engineering community and with society at large, such as, being able to comprehend and write
effective reports and design documentation, make effective presentations, and give and receive
clear instructions.
PO11: Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member
and leader in a team, to manage projects and in multidisciplinary environments.

PO12: Life-long learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological change.

Name: Rajat Sharma Roll No: 1609110914 Batch:7th-CS1-B1


PROGRAM SPECIFIC OUTCOMES (PSOs)
PSO1: An ability to apply foundation of Computer Science and Engineering, algorithmic principles and
theory in designing and modeling computation based systems.

PSO2: The ability to demonstrate software development skills.

Name: Rajat Sharma Roll No: 1609110914 Batch:7th-CS1-B1


JSSATEN CSE DEPARTMENT

Program 1:
Objective: WAP TO IMPLEMENT LAMPORT LOGICAL CLOCK
CODE:
#include<stdio.h>
#include<conio.h>
int maxx(int,int,int);
void main(){
int i, p1[4], p2[3], p3[4];
int d=1;
p1[1]=1;
p2[1]=1;
p3[1]=1;
for(i=1;i<=4;i++)
if(i>1)
p1[i]=p1[i-1]+d;
for(i=1;i<=4;i++)
if(i>1)
p3[i]=p3[i-1]+d;
for(i=1;i<=3;i++) {
if(i==2)
p2[i]=maxx(p1[2],p2[1],p3[1])+d;
else if(i==3)
p2[i]=maxx(p1[3],p2[2],p3[4])+d;
}
printf("\nClock values are:\nP1:");
for(i=1;i<=4;i++)
printf("\t%d",p1[i]);
printf("\nP2:");
for(i=1;i<=3;i++)
printf("\t%d",p2[i]);
printf("\nP3:");

Name: Rajat Sharma Roll No: 1609110914 Batch:7th-CS1-B1


JSSATEN CSE DEPARTMENT

for(i=1;i<=4;i++)
printf("\t%d",p3[i]);
getch();}
int maxx(int p,int q,int r)
{
if(p>q)
{
if(p>r)
return(p);
else
return(r); }
else
{
if(q>r)
return(q);
else
return(r);
}}

OUTPUT:

Name: Rajat Sharma Roll No: 1609110914 Batch:7th-CS1-B1


JSSATEN CSE DEPARTMENT

Program 2:
Objective:WAP TO IMPLEMENT LAMPORT NON TOKEN
BASEDMUTUAL EXCLUSION ALGORITHM

CODE:
#include<stdio.h>
#include<conio.h>

void main()
{
int s[10],n,i,ch=1,temp,j;
printf("\n enter the number of sites");
scanf("\n %d",&n);

for (i=0;i<n;i++)
{
printf("\n enter the timestamp of %d process",i+1);
scanf("%d",&s[i]);
}

for(i=0;i<n;i++)
for(j=n-1;j>=i+1;j--)
if(s[j]<s[j-1])
{
temp=s[j];
s[j]=s[j-1];
s[j-1]=temp;
}

for(i=0;i<n;i++)
printf("\n %d",s[i]);

Name: Rajat Sharma Roll No: 1609110914 Batch:7th-CS1-B1


JSSATEN CSE DEPARTMENT

while(ch!=0)
{
printf("\n enter the choice");
printf("\n 1.reqesting a critical section");
printf("\n 2. releasing a critical section");
printf("\n 0.exit");
scanf("\n %d",&ch);
switch(ch)
{
case 1 :
printf("\n enter the timestamp");
scanf("\n %d",&s[n]);
n++;
for(i=0;i<n;i++)
for(j=n;j>=i+1;j--)
if(s[j]>s[j+1])
{
temp=s[j];
s[i]=s[j+1];
s[j+1]=temp;
}
for(i=0;i<n;i++)
printf("\n %d",s[i]);
break;
case 2:
s[n]=0;
n--;
}
}
getch();
}

Name: Rajat Sharma Roll No: 1609110914 Batch:7th-CS1-B1


JSSATEN CSE DEPARTMENT

OUTPUT:

Name: Rajat Sharma Roll No: 1609110914 Batch:7th-CS1-B1


JSSATEN CSE DEPARTMENT

Program 3:
Objective:PROGRAM USING TCP SOCKET: CLIENT-SERVER IN C
CODE:

SERVER:
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<errno.h>
#include<unistd.h>

int main(int argc,char *argv[])


{
socklen_t sid,clen;
char buffer[1024];

struct sockaddr_in saddr,caddr;int n;if(argc<2)


{
fprintf(stderr,"ERROR: port is not given \n");
exit(1);
}

sid=socket(AF_INET,SOCK_DGRAM,0);

if(sid<0)
perror("socket_create");

bzero((char*)&saddr,sizeof(saddr));
saddr.sin_family=AF_INET;
saddr.sin_port=htons(atoi(argv[1]));
saddr.sin_addr.s_addr=INADDR_ANY;

if(bind(sid,(struct sockaddr *)&saddr,sizeof(saddr))<0)


perror("socket_bind");

Name: Rajat Sharma Roll No: 1609110914 Batch:7th-CS1-B1


JSSATEN CSE DEPARTMENT

clen=sizeof(caddr);
bzero(buffer,1024);
n=recvfrom(sid,buffer,1023,0,(struct sockaddr*)&caddr,&clen);

if(n<0)
perror("receive");

printf("The msg is %s", buffer);


close(sid);
return 0;
}

CLIENT:
#include <netdb.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>

int main(int argc,char *argv[])


{
int sid,n;
char buffer[1024];
struct sockaddr_in saddr;
struct hostent *hen;

if(argc<3)
{
fprintf(stderr,”Error: Host name and port is not given \n”);
exit(1);
}

sid=socket(AF_INET,SOCK_DGRAM,0);

if(sid<0)
perror("Socket create");

hen=gethostbyname(argv[1]);

Name: Rajat Sharma Roll No: 1609110914 Batch:7th-CS1-B1


JSSATEN CSE DEPARTMENT

if(hen==NULL)
{
fprintf(stdout,"Host not found");
exit(1);
}
saddr.sin_family=AF_INET;
saddr.sin_port=htons(atoi(argv[2]));
bcopy((char *)hen->h_addr,(char *)&saddr.sin_addr.s_addr,hen->h_length);
if(bind(sid,(struct sockaddr *)&saddr,sizeof(saddr))<0)
perror("Socket_bind");

printf("Enter the data u want to send : \n");


fgets(buffer,1023,stdin);
n=sendto(sid,buffer,1023,0,(struct sockaddr *)&saddr,sizeof(saddr)):
if(n<0)
perror(“Error in send to”);
close(sid);
return(0);
}

OUTPUT:

CLIENT:
[root@localhost]$ cc udpclient.c
[root@localhost]$ cc udpclient.c -o
ucli[root@localhost]$ ./ucli localhost 3927
Enter the data u want to send :Hello, how are you?
[root@localhost]$

SERVER:
[root@localhost]$ cc udpserver.c
[root@localhost]$ cc udpserver.c -o user
[root@localhost]$ ./user 3927The msg is Hello, how are you?
[root@localhost]$

Name: Rajat Sharma Roll No: 1609110914 Batch:7th-CS1-B1


JSSATEN CSE DEPARTMENT

Program 4:
Objective:IMPLEMENT JAVA RMI MECHANISM FOR
ACCESSINGMETHODS OF REMOTE SYSTEMS.

CODE:

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)

Name: Rajat Sharma Roll No: 1609110914 Batch:7th-CS1-B1


JSSATEN CSE DEPARTMENT

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

Name: Rajat Sharma Roll No: 1609110914 Batch:7th-CS1-B1


JSSATEN CSE DEPARTMENT

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

Running The Application:

D:\Nipun\RND\Java\NetWorking\RMI>rmic CalculatorImpl
Now open three cmd prompts and follow these at each.
1. D:\Nipun\RND\Java\NetWorking\RMI>Rmiregistry
2. D:\Nipun\RND\Java\NetWorking\RMI>java CalculatorServer
3. D:\Nipun\RND\Java\NetWorking\RMI>java CalculatorClient

OUTPUT:
1
9
18
3

Name: Rajat Sharma Roll No: 1609110914 Batch:7th-CS1-B1


JSSATEN CSE DEPARTMENT

Program 5:
Objective:IMPLEMENTATION OF CORBA (COMMONOBJECT
REQUEST BROKER ARCHITECTURE) MECHANISM.

CODE:

1. FileInterface.idl

interface FileInterface {
typedef sequence<octet> Data;
Data downloadFile(in string fileName);
};

NOTE:Now, let's compile the FileInterface.idl and generate server-side skeletons. Using
thecommand:

D:\Nipun\RD\Java\CORBA>idlj -fserver FileInterface.idl

2. FileServant.java

import java.io.*;

public class FileServant extends _FileInterfaceImplBase


{ public byte[] downloadFile(String fileName){
File file = new File(fileName);
byte buffer[] = new byte[(int)file.length()];
try {
BufferedInputStream input = new
BufferedInputStream(new FileInputStream(fileName));
input.read(buffer,0,buffer.length);
input.close();
} catch(Exception e) {
System.out.println("FileServant Error: "+e.getMessage());
e.printStackTrace();
}
return(buffer);
}
}

Name: Rajat Sharma Roll No: 1609110914 Batch:7th-CS1-B1


JSSATEN CSE DEPARTMENT

3. FileServer.java

import java.io.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;

public class FileServer {


public static void main(String args[]) {
try{
// create and initialize the ORB
ORB orb = ORB.init(args, null);
// create the servant and register it with the ORB
FileServant fileRef = new FileServant();
orb.connect(fileRef);
// get the root naming context
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService"); NamingContext
ncRef = NamingContextHelper.narrow(objRef);
// Bind the object reference in naming
NameComponent nc = new NameComponent("FileTransfer", " ");
NameComponent path[] = {nc};
ncRef.rebind(path, fileRef);
System.out.println("Server started....");
// Wait for invocations from clients
java.lang.Object sync = new java.lang.Object();
synchronized(sync){
sync.wait();
}
} catch(Exception e) {
System.err.println("ERROR: " + e.getMessage());
e.printStackTrace(System.out);
}
}
}

4. FileClient.java

import java.io.*;
import java.util.*;
import org.omg.CosNaming.*;
import org.omg.CORBA.*;

Name: Rajat Sharma Roll No: 1609110914 Batch:7th-CS1-B1


JSSATEN CSE DEPARTMENT

public class FileClient {


public static void main(String argv[]) {
try {
// create and initialize the ORB
ORB orb = ORB.init(argv, null);
// get the root naming context
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService"); NamingContext
ncRef = NamingContextHelper.narrow(objRef);
NameComponent nc = new NameComponent("FileTransfer", " ");
// Resolve the object reference in naming
NameComponent path[] = {nc};
FileInterfaceOperations fileRef =
FileInterfaceHelper.narrow(ncRef.resolve(path));

if(argv.length < 1) {
System.out.println("Usage: java FileClient filename");
}

// save the file


File file = new File(argv[0]);
byte data[] = fileRef.downloadFile(argv[0]);
BufferedOutputStream output = new
BufferedOutputStream(new FileOutputStream(argv[0]));
output.write(data, 0, data.length); output.flush();

output.close();
} catch(Exception e) {
System.out.println("FileClient Error: " + e.getMessage());
e.printStackTrace();
}
}
}

Running the application

1.D:\Nipun\RND\Java\CORBA>tnameserv
2.D:\Nipun\RND\Java\CORBA>java FileServer
3.D:\Nipun\RND\Java\CORBA>idlj -fclient FileInterface.idl
4.D:\Nipun\RND\Java\CORBA>java FileClient hello.txt

Name: Rajat Sharma Roll No: 1609110914 Batch:7th-CS1-B1


JSSATEN CSE DEPARTMENT

OUTPUT:

Name: Rajat Sharma Roll No: 1609110914 Batch:7th-CS1-B1


JSSATEN CSE DEPARTMENT

Program 6:
Objective: SLIDING WINDOW PROTOCOL PROGRAM IN C
CODE:

#include<stdio.h>
int main()
{
int w,i,f,frames[50];

printf("Enter window size: ");


scanf("%d",&w);
printf("\nEnter number of frames to transmit: ");
scanf("%d",&f);
printf("\nEnter %d frames: ",f);

for(i=1;i<=f;i++)
scanf("%d",&frames[i]);

printf("\nWith sliding window protocol the frames will be sent in the following manner
(assuming no corruption of frames)\n\n");
printf("After sending %d frames at each stage sender waits for acknowledgement sent by the
receiver\n\n",w);

for(i=1;i<=f;i++)
{
if(i%w==0)
{
printf("%d\n",frames[i]);
printf("Acknowledgement of above frames sent is received by sender\n\n");
}
else
printf("%d ",frames[i]);
}

if(f%w!=0)
printf("\nAcknowledgement of above frames sent is received by sender\n");

return 0;
}

Name: Rajat Sharma Roll No: 1609110914 Batch:7th-CS1-B1


JSSATEN CSE DEPARTMENT

OUTPUT:

Enter window size: 3

Enter number of frames to transmit: 5

Enter 5 frames: 12 5 89 4 6

With sliding window protocol the frames will be sent in the following manner (assuming no
corruption of frames)

After sending 3 frames at each stage sender waits for acknowledgement sent by the

receiver 12 5 89

Acknowledgement of above frames sent is received by

sender 4 6

Acknowledgement of above frames sent is received by sender

Name: Rajat Sharma Roll No: 1609110914 Batch:7th-CS1-B1


JSSATEN CSE DEPARTMENT

Program 7:
Objective: IMPLEMENT RPC MECHANISM FOR A FILE
TRANSFERACROSS A NETWORK IN C

CODE:

SERVER: server.c

#include"rpc/rpc.h"
#include"square.h"
#include"stdio.h"
#include"stdlib.h"
#include"math.h"

square_out *squareproc_1_svc(square_in *inp,structsvc_req *rqstp)


{
staticsquare_out out;
out.res1= inp->arg1 * inp->arg1;
return(&out);
}

CLIENT: client.c
#include"errno.h"
#include"rpc/rpc.h"
#include"square.h"
#include"stdio.h"
#include"stdlib.h"
#include"math.h"

intmain(intargc,char**argv)
{
CLIENT *cl;
square_in in;
square_out *outp;
f(argc!=3)
{
printf("\n\n error:insufficient arguments!!!");
exit(-1);
}

cl=clnt_create(argv[1],SQUARE_PROG,SQUARE_VERS,"tcp");
in.arg1=atol(argv[2]);

Name: Rajat Sharma Roll No: 1609110914 Batch:7th-CS1-B1


JSSATEN CSE DEPARTMENT

if(cl==NULL)
{
printf("\nerror:%s",strerror(errno));
exit(-1);
}

if((outp=squareproc_1(&in,cl))==NULL)
{
printf("\nerror :%s",clnt_sperror(cl,argv[1]));
exit(-1);
}

printf("\n\n result is : %ld",outp->res1);


exit(0);
}

HEADER FILE: square.h

structsquare_in
{
/*input arg*/
longarg1;
};

structsquare_out
{
/*op result*/
longres1;
};

program SQUARE_PROG
{
version SQUARE_VERS
{
square_out SQUAREPROC(square_in)=1; /*proc no=1*/
}=1; /*version no*/
}=0x31230000;/*prog no*/

Name: Rajat Sharma Roll No: 1609110914 Batch:7th-CS1-B1


JSSATEN CSE DEPARTMENT

OUTPUT:

[root@localhost~]#rpcgen -C square.x
[root@localhost~]#cc -c client.c -o client.o
[root@localhost~]#cc -c square_clnt.c -o square_clnt.o
[root@localhost~]#cc -c square_xdr.c -o square.xdr.o
[root@localhost~]#cc -o client client.o square_clnt.o square_xdr.o
[root@localhost~]#cc -c client.c server.c square_xdr.c
[root@localhost~]#cc -c server.c -o server.o
[root@localhost~]#cc -c square_svc.c -o square_svc.o
[root@localhost~]#cc -o server server.o square_svc.o square_xdr.o
[root@localhost~]#./server &
[1] 2264
[root@localhost~]#./client localhost 4
result is: 16

Name: Rajat Sharma Roll No: 1609110914 Batch:7th-CS1-B1

You might also like