answersLogoWhite

0

#include<stdio.h>

#include<conio.h>

#include<process.h>

struct proc

{

int live;

int identifier;

}process[10];

int n,cordinator=1;

/******************* DISPLAY PROCESSES **********************/

void display()

{

int i;

printf("\n PROCESSES ARE\n\n");

printf("Processes ");

for(i=1;i<=n;i++)

{

printf("P%d\t",i);

}

printf("\nlive ");

for(i=1;i<=n;i++)

{

printf("%d\t",process[i].live);

}

printf("\nidentifier ");

for(i=1;i<=n;i++)

{

printf("%d\t",process[i].identifier);

}

}

/************ BULLY ALGORITHM ****************************/

void bully()

{

int ch,c,id,i=0,cordinator,init,max=-99;

cordinator=i;

for(i=1;i<=n;i++)

{

if(process[cordinator].identifier<process[i].identifier&& process[i].live==1)

cordinator=i;

}

printf("\n\n CURRENT CO-ORDINATOR IS=P%d",cordinator);

while(ch!=4)

{

printf("\n\n\n *** BULLY ALGORITHM ***");

printf("\n1.Crash a Process\n2.Activate Process\n3.Display\n4.Exit");

printf("\nENTER UR CHOICE");

scanf("%d",&ch);

switch(ch)

{

case 1:printf("\n Enter the process id to crash");

scanf("%d",&id);

if(process[id].live==0)

{

printf("\n Already crashed process");

}

else

{

process[id].live=0;

printf("\n process P%d is crashed",id);

if(id==cordinator)

{ while(1)

{

printf("\n Enter process id who intiates election");

scanf("%d",&init);

if(process[init].live==0)

{

printf("\n the selected process is crashed");

}

else

{

for(i=1;i<=n;i++)

{

if(i!=init&& process[i].identifier>process[init].identifier)

printf("\n Election MSG sent from %d to %d",init,i);

}

for(i=1;i<=n;i++)

{

if(i!=init)

{

if(process[i].identifier>process[init].identifier&&process[i].live!=0)

{

printf("\n OK from %d to %d",i,init);

}

}

}

for(i=1;i<=n;i++)

{

if(max<process[i].identifier && process[i].live!=0)

{

cordinator=i;

max=process[i].identifier;

}

}

printf("\n\n NEW CO-ORDINATOR IS=P%d",cordinator);

break;

}

}

}

}

break;

case 2:printf("\n Enter process id to activate");

scanf("%d",&id);

if(process[id].live==1)

{

printf("\n Process %d is already active",id);

}

else

{

process[id].live=1;

printf("\n Process %d activated",id);

}

if(process[id].identifier>process[cordinator].identifier)

{cordinator=id;

printf("\n NEW CO-ORDINATOR IS=P%d\n\n",id);

}

break;

case 3:display();

break;

case 4:break;

}

}

}

/************ RING ALGORITHM ****************************/

void ring()

{

int ch,c,id,i=0,init,max=-99,last;

// cordinator=i;

for(i=1;i<=n;i++)

{

if(process[cordinator].identifier<process[i].identifier&&process[i].live==1)

cordinator=i;

}

printf("\n\n CURRENT CO-ORDINATOR IS=P%d",cordinator);

while(ch!=4)

{

printf("\n\n\n *** RING ALGORITHM ***");

printf("\n1.Crash a Process\n2.Activate Process\n3.Display\n4.Exit");

printf("\nENTER UR CHOICE");

scanf("%d",&ch);

switch(ch)

{

case 1:printf("\n Enter the process id to crash");

scanf("%d",&id);

if(process[id].live==0)

{

printf("\n Already crashed process");

}

else

{

process[id].live=0;

printf("\n process P%d is crashed",id);

if(id==cordinator)

{ while(1)

{

printf("\n Enter process id who intiates election");

scanf("%d",&init);

if(process[init].live==0)

{

printf("\n the selected process is crashed");

}

else

{ last=init;

printf("\nElection MSG sent from =%d",last);

for(i=init+1;i<=n;i++)

{

if(i!=init)

printf(" ->%d",i);

}

for(i=1;i<init;i++)

{

if(i!=init)

printf("->%d",i);

last=i;

}

/*

for(i=1;i<=n;i++)

{

if(i!=init)

{

if(process[i].identifier>process[init].identifier&&process[i].live!=0)

{

printf("\n OK from %d to %d",i,init);

}

}

} */

// max=process[init].identifier;

for(i=init+1;i<=n;i++)

{

if(max<process[i].identifier && process[i].live==1)

{

cordinator=i;

max=process[i].identifier;

}

}

for(i=1;i<=init;i++)

{

if(max<process[i].identifier && process[i].live==1)

{

cordinator=i;

max=process[i].identifier;

}

}

printf("\n\n NEW CO-ORDINATOR IS=P%d",cordinator);

break;

}

}

}

}

break;

case 2:printf("\n Enter process id to activate");

scanf("%d",&id);

if(process[id].live==1)

{

printf("\n Process %d is already active",id);

}

else

{

process[id].live=1;

printf("\n Process %d activated",id);

if(process[id].identifier>process[cordinator].identifier)

{

printf("\n NEW CO-ORDINATOR IS=P%d\n\n",id);

cordinator=id;

}

}

break;

case 3:display();

break;

case 4:break;

}

}

}

void main()

{

int ch,i,c;

clrscr();

printf("\n ENTER NO. OF PROCESSES");

scanf("%d",&n);

for(i=1;i<=n;i++)

{

printf("\nEnter P%d process live or not(0/1)",i);

scanf("%d",&process[i].live);

printf("\nEnter P%d process identifier",i);

scanf("%d",&process[i].identifier);

}

display();

while(1)

{

printf("\n\n\n**** ELECTION ALGORITHM ****");

printf("\n1.BULLY ALGORITHM\n2.RING ALGORITHM\n3.EXIT");

printf("\n\n ENTER UR CHOICE");

scanf("%d",&ch);

switch(ch)

{

case 1:bully();

break;

case 2: ring();

break;

case 3:exit(0);

}

}

}

User Avatar

Wiki User

13y ago

What else can I help you with?

Continue Learning about Engineering

What is c plus plus program use to convert algorithm in to c plus plus program?

You can't convert an algorithm into code. That is the job of the programmer, not the language. Algorithm's are expressed in plain-English and typically use pseudocode to broadly demonstrate the implementation of the algorithm. However, it is the programmer's job to convert these algorithms into working code. Pseudocode isn't a programming language as such, but it uses structures and statements that are familiar to any programmer and can be easily translated into any language. However, pseudocode is not a standard so there are many different ways to present pseudocode to the programmer. Moreover, pseudocode is generalised and is far too generic to be converted directly into any one language, never mind C++, which can take advantage of the underlying hardware to produce more efficient algorithms than would otherwise be implied by the pseudocode alone. Hence the need for plain-English algorithms in conjunction with the pseudocode. Programmer's can process all this information far more easily than any computer can. Even if you could program a converter for one algorithm, there's no guarantee it would work for any other algorithm. The time spent programming an algorithm converter would be far better spent simply translating the algorithm yourself.


Algorithm of push and pop in c plus plus?

pop push c++ programming


What is mean by complexity in c plus plus programming?

Complexity is a measure of how long an algorithm is expected to take and/or how much space is required to complete the task. It is not specific to C++ -- the language is immaterial -- it only applies to algorithms. Complexity is often expressed in big O notation, where O(1) is constant time (the best that can be expected of any algorithm).


In computer language C plus plus is related to?

C++ is related to C, the language from which it is derived.


Does Visual Java plus plus and Java Builder is different from the Java language?

Yes!Visual Java plus plus and Java Builder is different from the Java language?

Related Questions

How do you write an Algorithm for a C plus plus Program?

You don't write an algorithm for a C++ program, unless you are documenting the C++ program after-the-fact. The normal procedure is to write the algorithm first, in a language independent fashion, and then translate that stated algorithm into C++ code, or into whatever language you wish.


What is c plus plus program use to convert algorithm in to c plus plus program?

You can't convert an algorithm into code. That is the job of the programmer, not the language. Algorithm's are expressed in plain-English and typically use pseudocode to broadly demonstrate the implementation of the algorithm. However, it is the programmer's job to convert these algorithms into working code. Pseudocode isn't a programming language as such, but it uses structures and statements that are familiar to any programmer and can be easily translated into any language. However, pseudocode is not a standard so there are many different ways to present pseudocode to the programmer. Moreover, pseudocode is generalised and is far too generic to be converted directly into any one language, never mind C++, which can take advantage of the underlying hardware to produce more efficient algorithms than would otherwise be implied by the pseudocode alone. Hence the need for plain-English algorithms in conjunction with the pseudocode. Programmer's can process all this information far more easily than any computer can. Even if you could program a converter for one algorithm, there's no guarantee it would work for any other algorithm. The time spent programming an algorithm converter would be far better spent simply translating the algorithm yourself.


How does algorithm and c plus plus program get along?

They are bosom-friends.


Algorithm of push and pop in c plus plus?

pop push c++ programming


What is mean by complexity in c plus plus programming?

Complexity is a measure of how long an algorithm is expected to take and/or how much space is required to complete the task. It is not specific to C++ -- the language is immaterial -- it only applies to algorithms. Complexity is often expressed in big O notation, where O(1) is constant time (the best that can be expected of any algorithm).


How do you find a largest algorithm in c plus plus?

#define max (a, b) ((a) &gt;= (b)) ? (a) : (b)


What is dry run in c plus plus terminology?

A manual check of the algorithm to ensure its correctness.


How do you check a user's text input in C plus plus?

Use an SLR parser algorithm.


What is GCD in C Plus Plus?

A C++ implementation of the Binary GCD (Stern's) algorithm is shown in the Related Link below.


What are the interpreter in c plus plus?

C++ is a compiled language, not an interpreted language.


When was Plus - programming language - created?

Plus - programming language - was created in 1976.


Is c plus plus a complied or interpreted language?

C++ is generally a compiled language.