⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 election.c

📁 Programs for Distributed Systems. BE sem 2 for Computer engineering
💻 C
字号:

/*.....................................................

//Program For Implementing Election Algorithm

....................................................*/

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>

typedef struct process
{
   int no;
   int priority;
   int active;
   struct process *next;
}P;

typedef struct priority
{
   int pri;
   struct priority *next;
   P *pp;
}Pri;

Pri *find_priority(P *head,Pri *head1)
{
    P *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()
{
  int n,i,j,p,maxpri,a,pid,max,o;
  P *head=NULL,*p1=NULL,*p2=NULL;
  char ch;

  printf("\n\n\t\t Enter how many process: ");
  scanf("%d",&n);


  for(i=0;i<n;i++)
  {
      printf("\n\t\t\t Enter the priority of process %d : ",i+1);
      scanf("%d",&p);
      printf("\n\t\t\t Is process with id %d is active? (0/1): ",i+1 );
      scanf("%d",&a);
      if(head==NULL)
      {
	 head = (P*)malloc(sizeof(P));
	 if(head==NULL)
	 {
	    printf("\n Memory can not be allocated");
	    getch();
	    exit(0);
	 }
	 head->no = i+1;
	 head->priority = p;
	 head->active=a;
	 head->next = head;
	 p1 = head;
      }
      else
      {
	  p2 = (P*)malloc(sizeof(P));
	  if(p2==NULL)
	 {
	    printf("\n Memory can not be allocated");
	    getch();
	    exit(0);
	 }
	 p2->no = i+1;
	 p2->priority = p;
	 p2->active = a;
	 p1->next = p2;
	 p2->next = head;
	 p1 = p2;
      }
  }
  printf("\n\t\t Enter 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("\n\t Process %d has invoked election algorithm",pid);
  printf("\n\t Election 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\t Process with id %d is the co-ordinator",max);
  while(1)
  {
     printf("\n\t Do you want to continue? (y/n): ");
     flushall();
     scanf("%c",&ch);
     if(ch=='n' || ch=='N')  break;
     p2=head;
     while(1)
     {
	printf("\n\tEnter 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("\n\t\t Enter the process id that invokes election algorithm: ");
     scanf("%d",&pid);
     printf("\n\t Election 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\t Process with id %d is the co-ordinator",max);
  }
}
void Ring()
{
  int n,i,j,p,maxpri,a,pid,max,o;
  P *head=NULL,*p1=NULL,*p2=NULL;
  Pri *head1=NULL;
  char ch;
  printf("\n\n\t\t Enter how many process: ");
  scanf("%d",&n);
  for(i=0;i<n;i++)
  {
      printf("\n\t\t\t Enter the priority of process %d : ",i+1);
      scanf("%d",&p);
      printf("\n\t\t\t Is process with id %d is active? (0/1): ",i+1 );
      scanf("%d",&a);
      if(head==NULL)
      {
	 head = (P*)malloc(sizeof(P));
	 if(head==NULL)
	 {
	    printf("\n Memory can not be allocated");
	    getch();
	    exit(0);
	 }
	 head->no = i+1;
	 head->priority = p;
	 head->active=a;
	 head->next = head;
	 p1 = head;
      }
      else
      {
	  p2 = (P*)malloc(sizeof(P));
	  if(p2==NULL)
	 {
	    printf("\n Memory can not be allocated");
	    getch();
	    exit(0);
	 }
	 p2->no = i+1;
	 p2->priority = p;
	 p2->active = a;
	 p1->next = p2;
	 p2->next = head;
	 p1 = p2;
      }
  }
  p2=head;
  while(1)
  {
	if(p2->active==1)
	{
	  printf("\n\n\t\t Process %d has invoked algorithm",p2->no);
	  break;
	}
	p2=p2->next;
	if(p2==head)  break;
  }
  head1=find_priority(head,head1);
  maxpri = find_max_priority(head1);
  printf("\n\n\t\t Process %d is a Co-ordinator",maxpri);
  while(1)
  {
     printf("\n\t Do you want to continue? (y/n): ");
     flushall();
     scanf("%c",&ch);
     if(ch=='n' || ch=='N')  break;
     p2=head;
     while(1)
     {
	printf("\n\tEnter the process with id %d is active or not (0/1): ",p2->no);
	scanf("%d",&a);
	p2->active=a;
	p2=p2->next;
	if(p2==head)  break;
     }
     p2=head;
     while(1)
     {
	if(p2->active==1)
	{
	  printf("\n\n\t\t Process %d has invoked algorithm",p2->no);
	  break;
	}
	p2=p2->next;
	if(p2==head)  break;
     }
     getch();
     maxpri = find_max_priority(head1);
     printf("\n\n\t\t Process %d is a Co-ordinator",maxpri);
     getch();
  }
}
void main()
{
  int n,i,j,p,maxpri,a,pid,max,o;
  char ch;
  while(1)
  {
     clrscr();
     printf("\n\t\t\t\t **** MENU ****");
     printf("\n\t\t\t 1. Bully's Algorithm");
     printf("\n\t\t\t 2. Ring Algorithm");
     printf("\n\t\t\t 3. Exit");
     printf("\n\t\t Enter your choice: ");
     scanf("%d",&o);
     switch(o)
     {
	case 1:   clrscr();
		  Bully();
		  getch();
		  break;
	case 2:   clrscr();
		  Ring();
		  getch();
		  break;
	case 3:   exit(0);
     }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -