C Program to Implement Bully Election Algorithm : Election Algorithms
Bully Election Algorithm in C Programming Language
- Each node has access to some permanent storage that survives node failures.
- There are no transmission errors.
- The communication subsystem does not fail
Algorithm :
- The bully election algorithm
- Process 4 holds an election
- Process 5 and 6 respond, telling 4 to stop
- Now 5 and 6 each hold an election
- Process 6 tells 5 to stop
- Process 6 wins and tells everyone
C Program for Bully Election Algorithm : Election Algorithms
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
#include<stdio.h> #include<conio.h> #include<alloc.h> #include<stdlib.h> 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; } //end while 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; } //end find_priority() 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; } } //end for 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(); } |