Sie sind auf Seite 1von 7

==================================================

=================================
NAME: VAIBHAV C SAWANT
ROLL NO. B4269
DIV : BE(B)COMP
COLLEGE :MITCOE
==================================================
=================================
Problem Statement : To implement BULLY algorithm of Election Algorithm
#include<stdio.h>
#include<conio.h>
#include<process.h>
struct proc
{
int live;
int priority;
}process[10];
int n,cordinator=1;
void display()
{
int i;
printf("\n TOTAL GIVEN PROCESSES ARE AS FOLLOWS::\n\n");
printf("PROCESS ");
for(i=1;i<=n;i++)
{
printf("P%d\t",i);
}
printf("\nLIVE\t ");
for(i=1;i<=n;i++)
{
printf("%d\t",process[i].live);
}
printf("\nPRIORITY ");
for(i=1;i<=n;i++)
{
printf("%d\t",process[i].priority);
}
}
/******* BULLY ALGORITHM ********/
void bully()
{

int ch,c,id,i=0,cordinator,init,max=0;
cordinator=i;
for(i=1;i<=n;i++)
{
if(process[cordinator].priority<process[i].priority&& process[i].live==1)
cordinator=i;
}
printf("\n\n CURRENT CO-ORDINATOR IS:: P%d",cordinator);
do
{
printf("\n\n\n *** BULLY ALGORITHM ***");
printf("\n\n1.Crash a Process\n2.Activate Process\n3.Display\n4.Exit");
printf("\nENTER YOUR CHOICE:: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n\n Enter the process id to crash:: ");
scanf("%d",&id);
if(process[id].live==0)
{
printf("\n\n Already crashed process...");
}
else
{
process[id].live=0;
printf("\n\n process P%d is crashed...",id);
if(id==cordinator)
{
while(1)
{
printf("\n\n Enter process priority who intiates election:: ");
scanf("%d",&init);
if(process[init].live==0)
{
printf("\n\n The selected process is crashed process...");
}
else
{
for(i=1;i<=n;i++)
{
if(i!=init&& process[i].priority>process[init].priority)
printf("\n\n Election MSG sent from %d to %d",init,i);

}// for i
for(i=1;i<=n;i++)
{
if(i!=init)
{
if(process[i].priority>process[init].priority&&process[i].live!=0)
{
printf("\n\n OK from %d to %d ",i,init);
}// if process
}// if init
}// for i
max=-99;

for(i=1;i<=n;i++)
{
if((max<process[i].priority) && process[i].live!=0)
{
cordinator=i;
max=process[i].priority;
}//if max
}// for i
printf("\n\n NEW CO-ORDINATOR IS:: P%d",cordinator);
break;
}//else
}//while
}// if coordinator
}//else outer
break;
case 2:
printf("\n\n Enter process id to activate:: ");
scanf("%d",&id);
if(process[id].live==1)
{
printf("\n\n Process %d is already active...",id);
}
else
{
process[id].live=1;
printf("\n\n Process %d activated...",id);
}
if(process[id].priority>process[cordinator].priority)
{
cordinator=id;

printf("\n NEW CO-ORDINATOR IS:: P%d\n\n",id);


}
break;
case 3: display(); break;
case 4:break;
}
}while(ch!=4);
}
void main()
{
int ch,i,c;
clrscr();
printf("\n**********BULLY ALGORITHM*************");
printf("\n\n ENTER NO. OF PROCESSES:: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n\nEnter P%d process live or not(0/1):: ",i);
scanf("%d",&process[i].live);
printf("\n\nEnter P%d process Priority:: ",i);
scanf("%d",&process[i].priority);
}
display();
bully();
getch();
}

// OUTPUT
**********BULLY ALGORITHM*************
ENTER NO. OF PROCESSES::

Enter P1 process live or not(0/1)::


Enter P1 process Priority::
1

Enter P2 process live or not(0/1)::


Enter P2 process Priority::
2

Enter P3 process live or not(0/1)::


Enter P3 process Priority::

1
3

Enter P4 process live or not(0/1)::


Enter P4 process Priority::

1
4

Enter P5 process live or not(0/1)::


Enter P5 process Priority::

1
5

TOTAL GIVEN PROCESSES ARE AS FOLLOWS::


PROCESS
LIVE
PRIORITY

P1
1
1

P2
0
2

P3
1
3

P4
1
4

CURRENT CO-ORDINATOR IS::

P5
1
5

P5

*** BULLY ALGORITHM ***


1.Crash a Process
2.Activate Process
3.Display
4.Exit
ENTER YOUR CHOICE::
1
*** BULLY ALGORITHM ***
1.Crash a Process
2.Activate Process
3.Display
4.Exit
ENTER YOUR CHOICE:: 1
Enter the process id to crash:: 5
process P5 is crashed...
Enter process priority who intiates election:: 1
Election MSG sent from 1 to 2

Election MSG sent from 1 to 3


Election MSG sent from 1 to 4
Election MSG sent from 1 to 5
OK from 2 to 1
OK from 3 to 1
OK from 4 to 1
NEW CO-ORDINATOR IS:: P4
*** BULLY ALGORITHM ***
1.Crash a Process
2.Activate Process
3.Display
4.Exit
ENTER YOUR CHOICE::
1
*** BULLY ALGORITHM ***
1.Crash a Process
2.Activate Process
3.Display
4.Exit
ENTER YOUR CHOICE::
1
Enter the process id to crash:: 2
Already crashed process...
*** BULLY ALGORITHM ***
1.Crash a Process
2.Activate Process
3.Display
4.Exit
ENTER YOUR CHOICE::
2
Enter process id to activate::
Process 5 activated...
NEW CO-ORDINATOR IS::
*** BULLY ALGORITHM ***
1.Crash a Process
2.Activate Process
3.Display
4.Exit
ENTER YOUR CHOICE::
2

P5

Enter process id to activate::

Proess 2 activated...
*** BULLY ALGORITHM ***
1.Crash a Process
2.Activate Process
3.Display
4.Exit
ENTER YOUR CHOICE::
3
TOTAL GIVEN PROCESSES ARE AS FOLLOWS::
PROCESS
LIVE
PRIORITY

P1
1
1

P2
1
2

P3
1
3

*** BULLY ALGORITHM ***


1.Crash a Process
2.Activate Process
3.Display
4.Exit
ENTER YOUR CHOICE:: 4

P4
1
4

P5
1
5

Das könnte Ihnen auch gefallen