Peterson Algo1, Peterson Algo 2 and Peterson Algo 3
Peterson Algo1, Peterson Algo 2 and Peterson Algo 3
#petersons algo 1
#include<stdio.h>
int main(void)
{
//input the choice from the user to run which process
int choice=0;
int i=0;
for(i=0;i<10;i++)
{
printf("Enter the number of the process : ");
scanf("%d",&choice);
if(run_process(choice))
;
}
return 0;
}
/*Sample Output
Enter the process no.: 0
0 is executing critical section.
Enter the process no.: 1
1 is executing critical section.
Enter the process no.: 0
0 is executing critical section.
Enter the process no.: 1
1 is executing critical section.
Enter the process no.: 0
0 is executing critical section.
Enter the process no.: 0
0 can not execute critical section.
Enter the process no.: 1
1 is executing critical section.
Enter the process no.: 1
1 can not execute critical section.
Enter the process no.: 1
1 can not execute critical section.
Enter the process no.: 0
0 is executing critical section.
*/
#petersons algo 2:
#include<stdio.h>
#include<stdbool.h>
bool flag[2];
int main(void)
{
//input the choice from the user to run which process
int p_no=0;
int i=0;
for(i=0;i<10;i++)
{
printf("Enter the process no.: ");
scanf("%d",&p_no);
run_process(p_no);
}
return 0;
}
}
/*Sample Output
Enter the process no.: 0
0 is executing CS.
Enter the process no.: 1
1 is executing CS.
Enter the process no.: 0
0 is executing CS.
Enter the process no.: 1
1 is executing CS.
Enter the process no.: 0
0 is executing CS.
Enter the process no.: 1
1 is executing CS.
Enter the process no.: 0
0 is executing CS.
Enter the process no.: 1
1 is executing CS.
Enter the process no.: 1
1 is executing CS.
Enter the process no.: 0
0 is executing CS.
*/
Peterson algo 3 :
//peterson algo-3
//This algo will work even when both process are interested.
//It will give chance to one process at a time which will depend on the value of turn.
#include<stdio.h>
#include<stdbool.h>
int turn;
bool flag[2];
int main(void)
{
short int i=0,p_no=0;
flag[0]=false,flag[1]=false;
for(i=0;i<5;i++)
{
printf("Enter the process no.: ");
scanf("%hd",&p_no);
run_process(p_no);
}
return 0;
}
/*Sample Output
Enter the process no.: 0
0 is executing C.S.
Enter the process no.: 1
1 is executing C.S.
Enter the process no.: 1
1 is executing C.S.
Enter the process no.: 0
0 is executing C.S.
Enter the process no.: 1
1 is executing C.S.
*/