int main()
{
	int i,j;
	printf("\nEnter no. of process::");
	scanf("%d",&n);
	for(i=1;i<=n;i++)
	{
		printf("\nEnter Process %d is Alive or not(0/1)::",i);
		scanf("%d",&list[i]);
		if(list[i])
			cdr=i;
	}
	display();
	do
	{
		printf("\n1.BULLY ALGORITHM \n2.RING ALGORITHM\n3.Display\n4.EXIT\nEnter your choice::");
		scanf("%d",&j);
		switch(j)
		{
			case 1:
				bully();
				break;
			case 2:
				ring();
			case 3:
				display();
				break;
			case 4:
				exit(1);
		}
	}while(j!=4);
	return 0;

}
Beispiel #2
0
int bully(int place)
{
	int i;
	for(i=0;i<num;i++)	//Sending election message to processes having greater priority
	{
		if(process[i]>process[place])
			printf("\nElection message sent from %d to %d\n",process[place],process[i]);
		else		//else set the flag as these processes cannot be the co-ordinator
			flag[i]=1;
	}
	
	for(i=0;i<num;i++)	//The process that has greater priority takes over the process that just sent the election message 
	{
		if(flag[i]!=1&&process[i]>process[place])
		{
			printf("\nTakeover message sent from %d to %d\n",process[i],process[place]);
			flag[place]=1;
		}
	}		//Call the bully algorithm for those process who received a election message from a low priority process
	for(i=0;i<num;i++)
	{
		if(flag[i]==0)
			place = bully(i);
	}
	return place;
}
int main()
{
	int i,choice = 1;

	printf("\nEnter no. of Nodes : ");
	scanf("%d",&no_proc);
	for(i=1;i<=no_proc;i++)
	{
		printf("\nEnter Node %d IP  : ",i);
		scanf("%s",node[i].list);
		node[i].alive = !(ping_node(node[i].list));
		printf("\nGot the host %s is",node[i].list);
		node[i].alive==0? printf(" Dead\n") : printf(" Alive\n");


	}
	for(i=no_proc;i>1;i--)
	{
		if(node[i].alive==1)
		{
			CoOrdinator_id = i;
			break;
		}
	}
	if(i==0)
		printf("All dead.........");
	display();
	while(choice<4)
	{
		printf("\n---Election Algorithym---");
		printf("\n1. Bully Algo");
		printf("\n2. Ring Algo");
		printf("\n3. Display");
				printf("\n4.Exit");
		printf("\nEnter your choice : ");
		scanf("%d",&choice);

		switch(choice)
		{
			case 1:
					bully();
					break;
			case 2:
					ring();
					break;
			case 3:
					display();
					break;
			case 4:
					exit(0);
					break;

		}
	}
	return 0;
}
int main()
{
	pid_t pid;
	int n, i,j=0,k, eid, op;
	printf("\nEnter the number of processes:");
	scanf("%d",&n);
	printf("\n Sr.No\tProcess ID");
	for(i=0;i<n;i++)
	{
		pid=fork();
		if(pid)
		{
			p[i].id=pid;
			p[i].pno=i;
			printf("\n %d \t %d", p[i].pno, p[i].id);
			j++;
			continue;
		}
		else
		{
			while(1){}
		}
	}
	printf("\n Coordinator elected is process %d with pid=%d\n", p[n-1].pno, p[n-1].id);
	k=kill(p[n-1].id, SIGCHLD);
	sleep(5);
	if(k==0)
	{
		printf("\nProcess no %d with pid=%d had crashed", p[n-1].pno, p[n-1].id);
	}
	
		printf("\nSelect Election Algorithm:");
		printf("\n1.Ring Algorithm");
		printf("\n2.Bully Algorithm");
		printf("\n3.Exit\n");
		scanf("%d", &op);
		switch(op)
		{
			case 1:
			ring(n);
			break;
			case 2:
			bully(p[n-1].pno, n);
			break;
			case 3:
			break;
		}
}	
Beispiel #5
0
int main()
{
	int i=0,max=-1,place=-1,crash=-1,elect=-1,j=0;
	printf("Enter the number of processes:\t");
	scanf("%d",&num);
	printf("Enter the priorities of the processes:\t");
	for(i=0;i<num;i++)
	{
		scanf("%d",&process[i]);
		if(process[i]>max)
		{
			max = process[i];
			place = i;
		}
	}
	printf("The current Co-ordinatr process is: %d\n",process[place]);
	printf("Enter the process that crashed:\t");
	scanf("%d",&crash);
	
	for(i=0;i<num;i++)		//finding and marking the process flag as 1
		if(crash == process[i])
			flag[i] = 1;
	
	printf("\nEnter the process that starts election:\t");
	scanf("%d",&elect);
	
	for(i=0;i<num;i++)	//The process that starts the election algorithm feels that it's having max priority and hence we place the co-ordinator in place
		if(elect==process[i])
			place = i;
		
	place = bully(place);
	printf("\nThe current Co-ordinator is: %d\n",process[place]);
	
	for(i=0;i<num;i++)	//Victory message is sent from the winner to each other process
	{
		if(i!=place && process[i]<process[place])
			printf("\nVictory message from %d to %d\n",process[place],process[i]);
	}
	return 0;
}