DS Lab Manual
DS Lab Manual
DS Lab Manual
1. A process increments its counter before each event in that process,When a process sends a message,
it includes its counter value with the message;
2. On receiving a message, the counter of the recipient is updated, if necessary, to the greater of its
current counter and the timestamp in the received message.
Procedure:
#include<stdio.h>
#include<conio.h>
int max(int a,int b);
int main()
{
int i,j,k,p1[20],p2[20],e1,e2,dep[20][20];
printf("*** Lamport's Logical Clock ***\n");
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("\nEnter 1 if E1->E2 \nEnter -1, if E2->E1 \nElse Enter 0 \n\n");
printf(" ");
for(i=0;i<e2;i++)
printf(" e2%d",i+1);
for(i=0;i<e1;i++){
printf("\ne1%d ",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++){
//change the Time stamp if dependency exist
if(dep[i][j]==1){
p2[j]=max(p2[j],p1[i]+1);
for(k=j;k<e2;k++)
p2[k+1]=p2[k]+1;
}
//change the Time stamp if dependency exist
if(dep[i][j]==-1){
p1[i]=max(p1[i],p2[j]+1);
for(k=i;k<e1;k++)
p2[k+1]=p1[k]+1;
}
}
}
//to print the outcome of Lamport Logical Clock
printf("\nP1 : ");
for(i=0;i<e1;i++){
printf("%d",p1[i]);
}
printf("\nP2 : ");
for(j=0;j<e2;j++)
printf("%d",p2[j]);
getch();
return 0 ;
}
//to find the maximum timestamp between two events
int max(int a, int b)
{
if (a>b)
return a;
else
return b;
}
Output:
PRACTICAL 2:
Algorithms :
4. If own request is at the head of its queue and all replies have been received, enter critical
section.
5. Upon exiting the critical section, remove its request from the queue and send a release
message to every process.
#include <stdio.h>
#include<stdlib.h>
#include<pthread.h>
void *functionC();
int counter = 0;
main()
/* the process and all threads before the threads have completed. */
exit(0); }
void *functionC()
pthread_mutex_lock( &mutex1 );
counter++;
Results:
#include<pthread.h>
#define NTHREADS 10
int counter = 0;
main() {
pthread_t thread_id[NTHREADS];
int i, j;
/* Without the join I could be printing a value before all the threads */
pthread_mutex_lock( &mutex1 );
Compile:cc-lpthreadjoin1.c Run:./a.out
Results:
Thread number 1026
cond1.c
#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>
void *functionCount1();
void *functionCount2();
int count = 0;
exit(0);}
void *functionCount1() {
for(;;) {
// Lock mutex and then wait for signal to relase mutex pthread_mutex_lock( &count_mutex );
count++;
void *functionCount2() {
for(;;) {
pthread_mutex_lock( &count_mutex );
// Condition of if statement has been met. // Signal to free waiting thread by freeing the
mutex. //
pthread_cond_signal( &condition_var ); }
pthread_mutex_unlock( &count_mutex );
Final count: 10 .
PRACTICAL 3:
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
char buffer[MAXMSG];
int nbytes;
perror ("read");
exit (EXIT_FAILURE);
else if (nbytes == 0)
return -1;
else
return 0;
}}
int
main (void){
int sock;
int i;
size_t size;
perror ("listen");
exit (EXIT_FAILURE);
FD_ZERO (&active_fd_set);
while (1)
read_fd_set = active_fd_set;
perror ("select");
exit (EXIT_FAILURE);
if (i == sock){
int new;
size = sizeof (clientname);
&size);
perror ("accept");
exit (EXIT_FAILURE);}
fprintf (stderr,
inet_ntoa (clientname.sin_addr),
ntohs (clientname.sin_port));
Else{
}}}}}
PRACTICAL 4:
Aim: Implement RPC mechanism for a file transfer across a network in ‘C’
#include"rpc/rpc.h"
#include"square.h"
#include"stdio.h"
#include"stdlib.h"
#include"math.h"
cl=clnt_create(argv[1],SQUARE_PROG,SQUARE_VERS,"tcp");
in.arg1=atol(argv[2]);
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);
}
// .h FILENAME: square.h
struct square_in
{
/*input arg*/
long arg1;
};
struct square_out
{
/*op result*/
long res1;
};
program SQUARE_PROG
{
version SQUARE_VERS
{
square_out SQUAREPROC(square_in)=1; /*proc no=1*/
}=1; /*version no*/
}=0x31230000;/*prog no*/
[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
PRACTICAL 5:
RMI stands for Remote Method Invocation. It is a mechanism that allows an object residing in
one system (JVM) to access/invoke an object running on another JVM.
RMI is used to build distributed applications; it provides remote communication between Java
programs. It is provided in the package java.rmi.
Algorithm :
import java.rmi.*;
import java.rmi.server.*;
message = msg;
return message;
}}
HelloClient.java
import java.rmi.Naming;
try {
System.out.println (hello.say());
}}
HelloInterface.java
import java.rmi.*;
public interface HelloInterface extends Remote { public String say() throws RemoteException; }
HelloServer.java
import java.rmi.Naming;
try {
}}}
PRACTICAL 6:
Descriptions:
window protocols are used where reliable in-order delivery of packets is required, such as in
the Data Link Layer (OSI layer 2) as well as in the Transmission Control Protocol (TCP).
Conceptually, each portion of the transmission (packets in most data link layers, but bytes in
TCP) is assigned a unique consecutive sequence number, and the receiver uses the numbers to
place received packets in the correct order, discarding duplicate packets and identifying
missing ones. The problem with this is that there is no limit on the size of the sequence
number that can be required.
Program:
#include <STDIO.H>
#include <iostream.h>
#include <string>
#define THANKS -1
void main()
FILE *r_File1;
FILE *w_File2;
int m_framecount;
int frameCount = 0;
long currentP = 0;
long sentChar = 0;
long recvedChar = 0;
char s_name[100];
char d_name[100];
int frameSize;
int dataSize;
struct FRAME{
int s_flag;
intsequenceNo;
char data[90]
int n_flag;
};
FRAME frame;
int s_flag;
int nextSeq;
int n_flag;
}ack;
ack.s_flag = 126;
ack.n_flag = 126;
ack.nextSeq = NULL;
goto lable2;
}
lable3: cout<< "Please enter the size of frame 14--101 Only!" << endl;
cin >>frameSize;
cout << "source file could not be opened please check it and re-start!" <<endl;
goto lable1;
else
cout <<endl;
cout <<endl;
//after open the file, use fseek to resume the last position of a file pointer.
fseek(r_File1,currentP,SEEK_SET);
frame.sequenceNo = seqNo;
else
//This loop is used to fill the characters read from opened file to char array data
which //is a memeber of structure frame.
//we have to reseve a byte for \0 which is used to identify the end of the data array.
//that means each time we only read datasize -1 characters to the data array.
{ //if it is not end of file read a character from file then save it into
data //field in frame structure.
frame.data[j]= fgetc(r_File1);
if (frame.data[j]{
//sentChar++;
break;}
//frameCount = i;
frameCount++;
m_framecount = i +1;
cout <<endl;
cout << "The squence number is " << pf[i].sequenceNo <<endl;
cout << "The start flag is " << pf[i].s_flag <<endl;
cout << "There are " <<frameCount <<" frames has been created!"<<endl;
cout << "frame " << pf[i].sequenceNo <<" has been transported!";
cout<< endl;
fclose(r_File1);
break;
m_framecount = i +1;
cout <<endl;
cout << "There are total " <<frameCount <<" frames has been created!"<<endl;
fflush(r_File1);//refresh
cout <<endl;
cout <<"Total " << sentChar << " characters have been sent on this session!"<<endl;
cout <<endl;
cout <<endl;
cout <<endl;
int nextNoRecord = 0;
if(pf[m].data[n]
ack.nextSeq = THANKS;
//fputc(pf[m].data[n],w_File2);
recvedChar++;
break;
//write the character from current frame 's which in t index of data flied.
fputc(pf[m].data[n],w_File2);
recvedChar++;}
cout << "The string ---->" << pf[m].data <<" written succeed"<<endl;
fflush(w_File2);//refresh
if(ack.nextSeq == THANKS)
fclose(w_File2);
break;}
nextNoRecord= pf[m].sequenceNo;
cout <<endl;
cout <<"Total "<<recvedChar << " characters have been received on this session"<<endl;
cout <<endl;
cout <<endl;
if (ack.nextSeq != THANKS)
cout<<"CheckACK"<<endl;
if (nextNoRecord
ack.nextSeq =0 ;
Else{
cout << "The next expect frame is " << ack.nextSeq <<endl;
else
{ cout<<"CheckACK"<<endl;
delete []pf;
}}
else
cout <<endl;
cout <<endl;
numRead = 0;
fseek(r_File1,0,SEEK_END);
numRead = ftell(r_File1);
cout << "There are " << numRead <<" Bytes in the file" << endl;*/ }
►OUTPUT
1: use fixed source file name and fixed destination file name and fixed sliding
2: use keyboard to input the “source file name”, “destination file name”,
“sliding windows size”, and “frame size” to test program
Read file successfully.
Create frames successfully.
Description:
CORBA is essentially a design specification for an Object Request Broker (ORB), where an ORB
provides the mechanism required for distributed objects to communicate with one another,
whether locally or on remote devices, written in different languages, or at different locations
on a network.
Program:
#include <iostream>
#include "OB/CORBA.h"
#include <OB/Cosnaming.h>
#include "crypt.h"
#include "cryptimpl.h"
policies.length(1);
policies[0] =
rootPOA->create_thread_policy
(PortableServer::SINGLE_THREAD_MODEL);
//Free policies
CORBA::ULong len = policies.length();
policies[i]->destroy();
// Activate object
CORBA::String_var s = orb->object_to_string(o);
cout << "The IOR of the object is: " << s.in() << endl;
CosNaming::Name name;
name.length(1);
name[0].id = (const char *) "CryptographicService";
name[0].kind = (const char *) "";
} catch(const CORBA::Exception& e) {
// Decrement reference
count if (CrypImpl)
CrypImpl->_remove_ref();
//End CORBA
if (!CORBA::is_nil(orb)){
try{
orb->destroy();
return 0;
#include <iostream>
#include <string>
#include "OB/CORBA.h"
#include "OB/Cosnaming.h"
#include "crypt.h"
// Declare ORB
CORBA::ORB_var orb;
try {
name.length(1);
CORBA::String_var info_out;
::CaesarAlgorithm::charsequence_var
inseq; unsigned long key,shift;
try{
do{
{
cin.clear();
} while (cin.fail());
do{ // Get the shift
if (cin.fail())
cin.clear();
} while (cin.fail());
//shift = 938372;
getline(cin,info_in);
<< endl;
} while (exit!="y");
manager->shutdown()
}catch(const CORBA::Exception& e) {
try{
orb->destroy();
} catch(const CORBA::Exception& e)
}}
return 0;}
►OUTPUT
Running the Client-server Application Once we have implemented the client and the server,
it‟s time to connect them. Because our demonstration client and server exchange object
references via the naming service, we must ensure that the naming service (which is called
nameserv in Orbacus) is running. We use some command-line options to tell the naming
service the host and port on which it should listen. nameserv -OAhost localhost -OAport 8140
After this, we can start the server with a command-line option to tell it how to contact the
naming service. server -ORBInitRef NameService=corbaloc:iiop:localhost:8140/NameService
Finally we can start the client, again with a command-line option to tell it how to contact the
naming service. client -ORBInitRef NameService=corbaloc:iiop:localhost:8140/NameService