Вы находитесь на странице: 1из 6

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

===================================
NAME: VAIBHAV C SWANT
COLLEGE: MIT COLLEGE OF
ENGINEERING
DIV: COMP(B)
ROLL NO. B4269
==================================================
===================================
AIM: Implementation of Ring topology
#include<stdio.h>
#include<conio.h>
#include<process.h>
struct client
{
int id;
int pri;
int st;
}c[20];
struct token
{
int id;
int pri;
}t[10];
int tot_c;
int cord;
void create_clients()
{
int n;
int i;
int mx=0;int p;
clrscr();
printf("\nEnter Number of clients : ");
scanf("%d",&n);
tot_c=n;
for(i=0;i<tot_c;i++)
{
printf("\nEnter %d Client Data : ",i);
c[i].id=i;
printf("\nEnter it's Priority : "); scanf("%d", &c[i].pri);
if(mx<c[i].pri){mx=c[i].pri;p=i; }
c[i].st=1;
}
cord=p;
}

void find_cord()
{
int i;
int mx,p=999;
mx=0;
for(i=0;i<tot_c;i++)
{
if(c[i].st==1)
{
if(mx<c[i].pri)
{
mx=c[i].pri; p=i;
}
}
}
if(p==999)
cord=999;
else
cord=p;
}
void getCord()
{
if(c[cord].st!=0)
printf("\n Current Coordinator is : %d ",cord);
getch();
}
void print_clients()
{
int i;
clrscr();
printf("\nID Priority Status");
for(i=0;i<tot_c;i++)
{
printf("\n%d %d
%d",c[i].id,c[i].pri,c[i].st);
}
getch();
}

void force_crash()

{
//check whether the client is present or not ###
int j;
printf("Enter client ID : " );
scanf("%d",&j);
if(c[j].st!=0)
c[j].st=0;
else
printf("Client Is Already Dead");
getch();
}
void build_token(int st)
{
int i;
for(i=st;i<tot_c;i++)
{
if(c[i].st!=0)
printf(" \n %d >> %d " , c[i].id, c[i].pri);
}
if(st>0)
{
for(i=0;i<st;i++)
{
if(c[i].st!=0)
printf(" \n %d >> %d " , c[i].id, c[i].pri);
}
}
}
void send_msg()
{
int clnt;
printf("\nWho want to send msg : ");
scanf("%d",&clnt);
if(c[clnt].st!=0)
{
if(c[cord].st==0)
{
printf("\nCoordinator is Dead....\n Re election Initiated....");
build_token(clnt);
find_cord();
printf("\nMsg sent to newly elected cord : %d ", cord);
}
else
{
printf("Msg sent to coordinator : %d ",cord);
}
}
else
printf("Requested client is Dead");

getch();
}
void main()
{
int choice;
clrscr();
do
{
clrscr();
printf("
Ring Election Algorithm ");
printf("\n---------------------------");
printf("\n1. Create Clients ");
printf("\n2. Print Client Data");
printf("\n3. Force Client Crash");
printf("\n4. Send Message");
printf("\n5. Get Current Coordinator ");
printf("\n6. Close Simulation");
printf("\n---------------------------");
printf("\n Enter Your Choice :");
scanf("%d",&choice);
switch(choice)
{
case 1: create_clients();break;
case 2: print_clients();break;
case 3: force_crash();break;
case 4: send_msg();break;
case 5: getCord();break;
case 6: break;
}
}while(choice!=6);
}
==================================== OUTPUT
===================================
Ring Election Algorithm
--------------------------1. Create Clients
2. Print Client Data
3. Force Client Crash
4. Send Message
5. Get Current Coordinator
6. Close Simulation
--------------------------Enter Your Choice :1
Enter Number of clients : 8
Enter 0 Client Data :

Enter it's Priority : 0


Enter 1 Client Data :
Enter it's Priority : 1
Enter 2 Client Data :
Enter it's Priority : 2
Enter 3 Client Data :
Enter it's Priority : 3
Enter 4 Client Data :
Enter it's Priority : 4
Enter 5 Client Data :
Enter it's Priority : 5
Enter 6 Client Data :
Enter it's Priority : 6
Enter 7 Client Data :
Enter it's Priority : 7
2. Print Client Data
ID
0
1
2
3
4
5
6
7

Priority Status
0
1
1
1
2
1
3
1
4
1
5
1
6
1
7
1

3. Force Client Crash


Enter client ID : 7

4. Send Message
Who want to send msg : 0
Coordinator is Dead....
Re election Initiated....

0 >> 0
1 >> 1
2 >> 2
3 >> 3
4 >> 4
5 >> 5
6 >> 6
Msg sent to newly elected cord : 6
5. Get Current Coordinator
Current Coordinator is : 6
6. Close Simulation
Enter Your Choice :6

Вам также может понравиться