DS Practical File
DS Practical File
Session:2024-25
1
Roll no: 2102300100116
INDEX
Implementation of CORBA
4 (Common Object Request Broker 19
Architecture) mechanism.
2
Implementation of Voting Protocol
8 Algorithm 37
3
Program No.1
AIM: WAP to simulate the functionality of Lamport's Logical clock in C.
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<stdlib.h>
#include<graphics.h>
#include<string.h>
#include<dos.h>
void main(){
int msg[10][4]={0},totmsg;
char op;
int pi,pj,ei,ej;
clrscr();
cin>>n;
for(i=0;i<n;i++){
cin>>s[i][8];
for(j=1;j<=s[i][8];j++){ s[i]
[j]=j;
do{
4
cin>>msg[m][1];
cin>>msg[m][3]; cout<<"\n\nPress
cout<<op;
m++;
totmsg=m;
}while(op=='y');
m=0;
for (i=0;i<totmsg;i++)
{ pi=msg[i][0];
ei=msg[i][1];
pj=msg[i][2];
ej=msg[i][3];
if(s[pj][ej]< (s[pi][ei]+1))
{ s[pj][ej]=s[pi][ei]+1;
for (j=ej+1;j<=s[pj][8];j++){
s[pj][j]=s[pj][j-1]+1;
for(i=0;i<n;i++){
char* p1;
itoa(i,p1,10);
outtextxy(5,100+next,"P");
5
outtextxy(13,100+next,p1);
line(100,100+next,600,100+next); for(j=1;j<=s[i]
[8];j++){
char* p2;
itoa(j,p2,10);
outtextxy(100+step,90+next,"e");
outtextxy(110+step,90+next,p2);
//timestamp
char* p3;
itoa(s[i][j]-1,p3,10);
outtextxy(100+step,110+next,"t");
outtextxy(110+step,110+next,p3);
circle(105+step,100+next,5);
step+=50;
step=0;
next+=100;
delay(2000);
for(m=0;m<totmsg;m++)
{ setlinestyle(SOLID_LINE,1,3);
setcolor(m+4);
line(msg[m][1]*50+50,msg[m][0]*100+100,msg[m][3]*50+50,msg[m][2]*100+100);
if (msg[m][2]>msg[m][0]){ line(msg[m][3]*50+50,msg[m][2]*100+100,msg[m]
[3]*50+50,msg[m][2]*100+90);
line(msg[m][3]*50+50,msg[m][2]*100+100,msg[m][3]*50+40,msg[m][2]*100+90);
else{ line(msg[m][3]*50+50,msg[m][2]*100+100,msg[m][3]*50+50,msg[m]
[2]*100+110); line(msg[m][3]*50+50,msg[m][2]*100+100,msg[m][3]*50+40,msg[m]
[2]*100+110);
}}
6
getch(); }
7
Program No.2
AIM: WAP to Implement Vector clock in C.
#include<stdio.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void main()
long start[]={0,0,0},*vector;
clrscr();
while(!kbhit())
p1(1,&start[0]);
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));
if(i==1)
8
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]);
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
9
return &b[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];
1
0
Program No.3
AIM: Simulation of Distributed mutual exclusion in java.
import Utilities.*;
import Synchronization.*;
int whichOne = 0;
number = 0;
int replyCount = 0;
this.id = id;
10
this.numNodes = numNodes;
this.napOutsideCS = napOutsideCS;
this.napInsideCS = napInsideCS;
this.requestChannel = requestChannel;
this.replyChannel = replyChannel;
this.requestsToMe = requestsToMe;
= new boolean[numNodes];
public void run() { // start three different threads in the same object int
meDo = whichOne++;
Thread(this).start();
main();
{ new Thread(this).start();
handleRequests();
{ P(s);
requesting = true;
number = highNumber + 1;
V(s);
11
replyCount = 0;
{ P(wakeUp);
{ P(s);
requesting = false;
V(s);
{ if (deferred[j]) {
deferred[j] = false;
send(replyChannel[j], id);
}
}
{ int napping;
nap(napping);
{ int napping;
12
private void main()
{ while (true)
{ outsideCS();
chooseNumber(); // PRE-PROTOCOL
sendRequest(); // "
waitForReply(); // "
insideCS();
P(s);
replyCount++;
13
if (replyCount == numNodes - 1) V(wakeUp);
go.optErr = true;
int ch = -1;
int numNodes = 5;
System.out.println(usage); System.exit(0);
System.err.println(usage); System.exit(1);
System.out.println("DistributedMutualExclusion: numNodes="
argNum = go.optIndexGet();
14
for (int i = 0; i < numNodes; i++) { napOutsideCS[i]
go.tryArg(argNum++, 2);
napOutsideCS[i]*1000, napInsideCS[i]*1000,
requestChannelS, replyChannelS,
requestChannelR[i], replyChannelR[i]);
nap(runTime*1000);
System.out.println("age()=" + age()
15
+ ", time to stop the threads and exit");
System.exit(0);
Output:
D:\Prakash\Java\RND\Advanced>javac dimu.java
16
age()=3300, node 3 has now left its critical section
17
age()=13510, Node 0 napping outside CS for 4162 ms
18
Program No.4
AIM: Implementation of CORBA (Common Object Request Broker
Architecture) mechanism.
1.FileInterface.idl
interface FileInterface {
};
Now, let's compile the FileInterface.idl and generate server-side skeletons. Using the
command:
import java.io.*;
BufferedInputStream(new FileInputStream(fileName));
input.read(buffer,0,buffer.length);
input.close();
} catch(Exception e) {
e.printStackTrace();
return(buffer);
3.FileServer.java
import java.io.*;
19
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
{ try{
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
ncRef.rebind(path, fileRef);
synchronized(sync){
sync.wait();
} catch(Exception e) { System.err.println("ERROR:
" + e.getMessage());
e.printStackTrace(System.out);
20
4.FileClient.java
import java.io.*;
import java.util.*;
import org.omg.CosNaming.*;
import org.omg.CORBA.*;
{ try {
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
FileInterfaceOperations fileRef =
FileInterfaceHelper.narrow(ncRef.resolve(path));
if(argv.length < 1) {
BufferedOutputStream(new FileOutputStream(argv[0]));
output.write(data, 0, data.length);
output.flush();
output.close();
} catch(Exception e) {
21
e.printStackTrace();
1. D:\Prakash\RND\Java\CORBA>tnameserv
2. D:\Prakash\RND\Java\CORBA>java FileServer
Output:
22
Program No.5
AIM: Implementation of Path Pushing Algorithm
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<math.h>
#include<time.h>
int sum=0,sum1=0,sum2=0,sum3=0;
//fraction[]->It is used for randomly storing the number of resources allocated at any time int
fraction[4];
int fraction1[4];
int fraction2[4];
int fraction3[4];
//request[]->It is used for storing the resources requested by users at any time.
int request[4];
int request1[4];
int request2[4];
int request3[4];
/*available[]->It is used to check for the resources available by substracting the total number of
resources available
with total number of resources on hold.*/
int available[4];
void Calculate();
23
int main()
cout<<" "<<endl;
cout<<" "<<endl;
cin>>no_fax;
cin>>no_scanner;
cin>>no_tapedrive;
for(int i=0;i<4;i++)
{
cin>>request[i];
for(int i=0;i<4;i++)
cin>>request1[i];
i=0;i<4;i++)
cin>>request2[i];
24
{
cin>>request3[i];
/*srand(time0)-It used for generating a seed at a particular time otherwise if we use rand() in c+
+ directly
then it will always return the same random numbers after executing multiple times.
srand(time(0));
int randnum[4];
int randnum1[4];
int randnum2[4];
int randnum3[4];
//for loop Used for generating random numbers within the max limit for
sum+=randnum[i];
sum1+=randnum1[i];
sum2+=randnum2[i];
sum3+=randnum3[i];
fraction[i]=(no_printer*randnum[i]/sum);
fraction1[i]=(no_fax*randnum1[i]/sum1);
25
fraction2[i]=(no_scanner*randnum2[i]/sum2);
fraction3[i]=(no_tapedrive*randnum3[i]/sum3);
r1=fraction[0]+fraction[1]+fraction[2]+fraction[3];
r2=fraction1[0]+fraction1[1]+fraction1[2]+fraction1[3];
r3=fraction2[0]+fraction2[1]+fraction2[2]+fraction2[3];
r4=fraction3[0]+fraction3[1]+fraction3[2]+fraction3[3];
cout<<"\n "<<"**********************************************************\n\
n"<<" ";
cout<<"\n\n ********** Displaying Total Resource on hold Details ********** \n"<<" ";
cout<<"\n "<<"**********************************************************\n\
n"<<" ";
26
cout<<"\n\n ********** Displaying Total Resources available ********** \n"<<" ";
cout<<"\n "<<"**********************************************************\n\
n"<<" ";
available[0]=no_printer-r1;
available[1]=no_fax-r2;
available[2]=no_scanner-r3;
available[3]=no_tapedrive-r4;
Calculate();
return 0;
}
/* We calculate the resources available and take the resources requested by users as input and
check for possibility of assigning the resources by taking counters if the resources requested by
a particular person is less
i.e. the number of printers,scanners,tapedrive,fax then the counter value is increased till 4.
resources are assigned to the person and the total resources available at that time is
If the counter does not go upto 4 in any case then deadlock is detected.*/
27
void Calculate()
int c1,c2,c3,c4;
c1=0,c2=0,c3=0,c4=0;
do
c1=0,c2=0,c3=0,c4=0;
for(int i=0;i<4;i++)
if(request[i]<=available[i])
c1+=1;
else
c1=-1;
if(c1==4)
for(int i=0;i<4;i++)
available[i]+=request[i];
for(int i=0;i<4;i++)
if(request1[i]<=available[i])
c2+=1;
else
c2=-1;
if(c2==4)
for(int i=0;i<4;i++)
28
available[i]+=request1[i];
for(int i=0;i<4;i++)
if(request2[i]<=available[i])
c3+=1;
else
c3=-1;
if(c3==4)
for(int i=0;i<4;i++)
available[i]+=request2[i];
for(int i=0;i<4;i++)
if(request3[i]<=available[i])
c4+=1;
else
c4=-1;
if(c4==4)
for(int i=0;i<4;i++)
available[i]+=request3[i];
29
if(c1!=4 && c2!=4 && c3!=4 && c4!=4)
break;
else{
break;
}while(c1==4||c2==4||c3==4||c4==4);
30
Program No.6
AIM: Implementation of Edge Chasing Algorithm
Implementation:
if Pi is locally dependent on itself then declare a deadlock else for all Pj and Pk such that
2 Pj is waiting on Pk , and
On the receipt of a probe (i, j, k), the site takes the following actions:
if
1 Pk is blocked, and
begin
if k=i
(c’) Pm and Pn are on different sites, send a probe (i, m, n) to the home site of Pn end.
A probe message is continuously circulated along the edges of the global TWF graph and a deadlock is
detected when a probe message returns to its initiating process.
One probe message (per deadlock detection initiation) is sent on every edge of the WFG which that two
sites. Thus, the algorithm exchanges at most m(n − 1)/2 messages to detect a deadlock that involves m
processes and that spans over n sites. The size of messages is fixed and is very small (only 3 integer
words). Delay in detecting a deadlock is O(n).
31
Program :
#include <stdio.h>
#define MAX_SITES 10
int main() {
scanf("%d", &n);
scanf("%d", &processes[i]);
printf("Deadlock Detection\n");
printf("Enter the site number and process ID for which deadlock detection should be initiated: ");
printf("Enter the processes of two different sites connected with requesting edge: ");
scanf("%d %d", &sp1, &sp2);
if (p1 == sp2) {
printf("Deadlock detected\n");
} else {
32
return 0;
OUTPUT:
Enter total number of sites: 3
Deadlock Detection
Enter the site number and process ID for which deadlock detection should be initiated: 2 3
Enter the processes of two different sites connected with requesting edge: 3 4
Deadlock detected
33
Program No.7
AIM: Implementation of Commit Protocol Algorithm
#include <iostream>
#include <vector>
#include <algorithm>
{ INIT,
READY,
COMMITTED,
ABORTED
};
class Node
{ private:
State state;
public:
Node() : state(State::INIT) {}
{ return state;
{ state = newState;
};
class Coordinator
{ private:
34
std::vector<Node> nodes;
public:
void beginTransaction() {
{ node.setState(State::READY);
void commitTransaction() {
n.getState() == State::READY;
});
if (allReady) {
} else {
abortTransaction();
void abortTransaction() {
{ node.setState(State::ABORTED);
35
};
int main() {
// Simulate a transaction
coordinator.beginTransaction();
coordinator.commitTransaction();
return 0;
Output:
Transaction committed successfully!
36
Program No.7
AIM: Implementation of Voting Protocol Algorithm
#include <iostream>
#include <vector>
#include <algorithm>
NO,
UNDECIDED
};
class Node
{ private:
Vote vote;
public:
Node() : vote(Vote::UNDECIDED) {}
void setVote(Vote v) {
vote = v;
return vote;
};
class VotingProtocol
{ private:
37
std::vector<Node> nodes;
public:
void startVoting() {
if (rand() % 2 == 0)
{ node.setVote(Vote::YES);
} else {
node.setVote(Vote::NO);
void countVotes() {
int yesCount = 0;
int noCount = 0;
if (node.getVote() == Vote::YES)
{ yesCount++;
{ noCount++;
38
} else {
};
int main() {
VotingProtocol protocol(5);
protocol.countVotes();
return 0;
OUTPUT:
39