Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 10

ASSIGNMENT NO.

:
-----------------------------------------------------------------------------------------------Title: Write a program to stimulate election algorithm to count vote; in a loosely
coupled distributed system few machines will act as a voting machines
connected to server. For synchronizations using following methods A) Bully
Algorithm B) Ring Algorithm
ROLL NO.:
BATCH:
-----------------------------------------------------------------------------------------------//Bully Algorithm
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void bully();
int p[10],no,co,i,t,flag;
void bully()
{
int j,temp;
i=t;
co=-1;
printf("\n\n\t\t-------------Bully Algorithm-----------");
if(no-1==1)
{
printf("\n\n\t\tP%d -> E -> P%d",p[no-1],p[no-1]);
printf("\n\n\t\tP%d -> OK -> P%d",p[no-1],p[no-1]);
printf("\n\n\t\t CO :: %d ",p[no-1]);
}
else
{
printf("\n\n\t\tElection Message");
while(i!=0)
{
if(i==t)
flag++;
if(flag==2)
{
i=0;
break;

}
for(j=1;j<=no-1;j++)
{
if(p[i]<p[j])
{
printf("\n\n\t\tP%d -> E -> P%d",p[i],p[j]);
}
}
if(i<no-1 && flag!=2)
i++;
else if(i==no-1)
i=t;
}
getch();
printf("\n\n\t\tResponse");
for(i=t;i<=no-1;i++)
{
for(j=t;j<=no-1;j++)
{
if(p[i]>p[j])
{
printf("\n\n\t\tP%d -> OK -> P%d",p[i],p[j]);
}
}
}
getch();
printf("\n\n\t\tCo-ordinator Message");
for(i=1;i<no-1;i++)
{
printf("\n\n\t\tP%d -> CO %d -> P%d",p[no-1],p[no-1],p[i]);
}
}
}
void main()
{
int j,temp,ch;
char c;
clrscr();

do
{
printf("\n\n\tEnter the total no of processes:(Greater than 1!) ");
scanf("%d",&no);
if(no==1 || no>10)
printf("No of processes should be greater than 1 and less
than 10!");
}while(no==1 || no>10);
for(i=1;i<=no;i++)
{
printf("\n\t\tEnter the processes ID: ");
scanf("%d",&p[i]);
}
//arranging processes in ascending order
for(i=1;i<=no;i++)
{
for(j=1;j<no;j++)
{
if(p[i]<p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
do
{
printf("\n\t\tEnter the tracker process ID: ");
scanf("%d",&t);
if(t==p[no])
printf("\n\n\t\tProcess %d is crashed!!",p[no]);
}while(t==p[no]);
//finding the location of tracker process
for(j=1;j<no;j++)
{
if(p[j]==t)
i=j;
}
t=i;
printf("\n\t\tCurrently running processes::\n\t");
for(j=1;j<no;j++)

{
printf("\tP%d",p[j]);
}
bully();
getch();
}

Output
Enter the total no of processes:(Greater than 1!) 5
Enter the processes ID: 6
Enter the processes ID: 5
Enter the processes ID: 4
Enter the processes ID: 3
Enter the processes ID: 2
Enter the tracker process ID: 2
Currently running processes::
P2
P3
P4
P5
-------------Bully Algorithm----------Election Message
P2 -> E -> P3
P2 -> E -> P4
P2 -> E -> P5
P3 -> E -> P4
P3 -> E -> P5
P4 -> E -> P5
Response
P3 -> OK -> P2
P4 -> OK -> P2
P4 -> OK -> P3
P5 -> OK -> P2
P5 -> OK -> P3
P5 -> OK -> P4

Co-ordinator Message
P5 -> CO 5 -> P2
P5 -> CO 5 -> P3
P5 -> CO 5 -> P4

Ring Algorithm
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void ring();
int p[10],no,co,i,t,flag;
void ring()
{
int j,temp;
printf("\n\n\t\t-------------Ring Algorithm-------------");
flag=0;
co=-1;
i=t;
printf("\n\n\t\tElection message");
while(i!=0)
{
if(i==t)
flag++;
if(flag==2)
{
i=0;
break;
}
if(i<no-1 && flag != 2)
{
//co holds highest ID
if(p[i]>co)
co=p[i];
printf("\n\n\t\tP%d-> E %d -> P%d",p[i],co,p[i+1]);
i++;
}
else if(i==no-1)
{
if(p[i]>co)
co=p[i];
printf("\n\n\t\tP%d-> E %d -> P%d",p[i],co,p[1]);
i=1;
//i=1 as ring is present.So after last again 1st comes
}

}
getch();
printf("\n\n\t\tCo-ordinator message");
i=t;
flag=0;
while(i!=0)
{
if(i==t)
flag++;
if(flag==2)
{
i=0;
break;
}
if(i<no-1 && flag!=2)
{
printf("\n\n\t\tP%d -> CO %d -> P%d",p[i],co,p[i+1]);
i++;
}
else if(i==no-1)
{
printf("\n\n\t\tP%d -> CO %d -> P%d",p[i],co,p[1]);
i=1;
}
}
getch();
printf("\n\n\t\tCO-ordinator is - P%d",co);
}
void main()
{
int j,temp,ch;
char c;
clrscr();
do
{
printf("\n\n\tEnter the total no of processes:(Greater than 1!) ");
scanf("%d",&no);
if(no==1 || no>10)
printf("No of processes should be greater than 1 and less
than 10!");
}while(no==1 || no>10);
for(i=1;i<=no;i++)

{
printf("\n\t\tEnter the processes ID: ");
scanf("%d",&p[i]);
}
for(i=1;i<=no;i++)
{
for(j=1;j<no;j++)
{
if(p[i]<p[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
}
}
}
do
{
printf("\n\t\tEnter the tracker process ID: ");
scanf("%d",&t);
if(t==p[no])
printf("\n\n\t\tProcess %d is crashed!!",p[no]);
}while(t==p[no]);
for(j=1;j<no;j++)
{
if(p[j]==t)
i=j;
}
t=i;
printf("\n\t\tCurrently running processes::\n\t");
for(j=1;j<no;j++)
{
printf("\tP%d",p[j]);
}
ring();
getch();
}

Output
Enter the total no of processes:(Greater than 1!) 5
Enter the processes ID: 6
Enter the processes ID: 5
Enter the processes ID: 4
Enter the processes ID: 3
Enter the processes ID: 2
Enter the tracker process ID: 2
Currently running processes::
P2
P3
P4
P5
-------------Ring Algorithm------------Election message
P2-> E 2 -> P3
P3-> E 3 -> P4
P4-> E 4 -> P5
P5-> E 5 -> P2
Co-ordinator message
P2 -> CO 5 -> P3
P3 -> CO 5 -> P4
P4 -> CO 5 -> P5
P5 -> CO 5 -> P2
CO-ordinator is - P5

You might also like