C Program for Bully Election Algorithm
#include
#include
#include
#include
struct process
{
int no;
int priority;
int active;
struct process *next;
};
typedef struct process proc;
struct priority
{
int pri;
struct priority *next;
proc *pp;
};
typedef struct priority pri;
pri* find_priority(proc *head, pri *head1)
{
proc *p1;
pri *p2, *p3;
p1 = head;
while (p1->next != head)
{
if
(p1->active == 1)
{
if (head1 == null)
{
head1
= (pri*) malloc(sizeof(pri));
head1->pri
= p1->priority;
head1->next
= null;
head1->pp
= p1;
p2
= head1;
}
else
{
p3
= (pri*) malloc(sizeof(pri));
p3->pri
= p1->priority;
p3->pp
= p1;
p3->next
= null;
p2->next
= p3;
p2
= p2->next;
}
p1 = p1->next;
}
else
p1 = p1->next;
}
p3 = (pri*) malloc(sizeof(pri));
p3->pri = p1->priority;
p3->pp = p1;
p3->next = null;
p2->next = p3;
p2 = p2->next;
p3 = head1;
return head1;
}
int find_max_priority(pri *head)
{
pri *p1;
int max = -1;
int i = 0;
p1 = head;
while (p1 != null)
{
if (max <
p1->pri && p1->pp->active == 1)
{
max = p1->pri;
i
= p1->pp->no;
}
p1 =
p1->next;
}
return i;
}
void bully()
{
proc *head;
proc *p1;
proc *p2;
int n, i, pr, maxpri, a, pid, max, o;
char ch;
head = p1 = p2 = null;
printf("\nnenter how many process:
");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("\nenter
priority of process %d: ", i + 1);
scanf("%d",
&pr);
printf("\nis
process with id %d is active ?(0/1) :", i + 1);
scanf("%d",
&a);
if (head ==
null)
{
head = (proc*) malloc(sizeof(proc));
if (head == null)
{
printf("\nmemory
cannot be allocated");
getch();
exit(0);
}
head->no = i + 1;
head->priority = pr;
head->active = a;
head->next = head;
p1 = head;
}
else
{
p2 = (proc*) malloc(sizeof(proc));
if (p2 == null)
{
printf("\nmemory
cannot be allocated");
getch();
exit(0);
}
p2->no = i + 1;
p2->priority = pr;
p2->active = a;
p1->next = p2;
p2->next = head;
p1 = p2;
}
}
printf("\nenter the process id
that invokes election algorithm: ");
scanf("%d", &pid);
p2 = head;
while (p2->next != head)
{
if (p2->no
== pid)
{
p2 = p2->next;
break;
}
p2 =
p2->next;
}
printf("\nprocess with id %d has
invoked election algorithm", pid);
printf("\t\nelection message is
sent to processes");
while (p2->next != head)
{
if (p2->no
> pid)
printf("%d", p2->no);
p2 =
p2->next;
}
printf("%d", p2->no);
p2 = head;
max = 0;
while (1)
{
if
(p2->priority > max && p2->active == 1)
max = p2->no;
p2 =
p2->next;
if (p2 ==
head)
break;
}
printf("\n\tprocess with the id
%d is the co-ordinator", max);
while (1)
{
printf("\ndo
you want to continue?(y/n): ");
flushall();
scanf("%c",
&ch);
if (ch == 'n'
|| ch == 'n')
break;
p2 = head;
while (1)
{
printf("\nenter the process with id %d is active or not (0/1): ",
p2->no);
scanf("%d", &p2->active);
p2 = p2->next;
if (p2 == head)
break;
}
printf("\nenter
the process id that invokes election algorithm: ");
scanf("%d",
&pid);
printf("\n\telection
message is sent to processes ");
while
(p2->next != head)
{
if (p2->no > pid)
printf("%d",
p2->no);
p2 = p2->next;
}
printf("%d",
p2->no);
p2 = head;
max = 0;
while (1)
{
if
(p2->no > max && p2->active == 1)
max
= p2->no;
p2 = p2->next;
if (p2 == head)
break;
}
printf("\n\tprocess
with id %d is the co-ordinator", max);
}
}
void main()
{
clrscr();
bully();
getch();
}
No comments:
Post a Comment