0% found this document useful (0 votes)
60 views7 pages

Peterson Algo1, Peterson Algo 2 and Peterson Algo 3

The document describes three algorithms for implementing mutual exclusion in operating systems: Peterson's algorithm 1 uses a global turn variable to determine which process can enter the critical section; algorithm 2 uses an array of flags to indicate which process is waiting to enter; algorithm 3 is an improvement that allows both processes to be waiting by considering the value of turn. Sample output is provided for each algorithm demonstrating processes alternating access to the critical section.

Uploaded by

Sid Tyagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views7 pages

Peterson Algo1, Peterson Algo 2 and Peterson Algo 3

The document describes three algorithms for implementing mutual exclusion in operating systems: Peterson's algorithm 1 uses a global turn variable to determine which process can enter the critical section; algorithm 2 uses an array of flags to indicate which process is waiting to enter; algorithm 3 is an improvement that allows both processes to be waiting by considering the value of turn. Sample output is provided for each algorithm demonstrating processes alternating access to the critical section.

Uploaded by

Sid Tyagi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Peterson algo1 ,Peterson algo 2 and Peterson algo 3

#petersons algo 1
#include<stdio.h>

int run_process(int p_no);


//taking a global variable
int turn=0;

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;
}

int run_process(int p_no)


{
if(turn != p_no)
{
printf("%d can not execute critical section.\n",p_no);
return 0;
}
printf("%d is executing critical section.\n",p_no);

//update the value of turn


if(turn==0) turn=1;
else turn=0;
return 1;
}

/*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];

void run_process(int p_no);

int main(void)
{
//input the choice from the user to run which process
int p_no=0;

//initializing the flag variables with false


flag[0]=false,flag[1]=false;

int i=0;
for(i=0;i<10;i++)
{
printf("Enter the process no.: ");
scanf("%d",&p_no);

run_process(p_no);
}
return 0;
}

void run_process(int p_no)


{
flag[p_no]=true; //declare your own interset
int j=0;
if(p_no==0) j=1;
if(flag[j]) //wait if the other guy is interested
{
printf("%d can not execute CS.\n",p_no);
return ;
}

printf("%d is executing CS.\n",p_no);


flag[p_no]=false; //declare that you lost interest

}
/*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];

void run_process(short int p_no);

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;
}

void run_process(short int p_no)


{
//declaring the process is interested
flag[p_no]=true;
int j=0;
if(p_no==0) j=1;
int turn=j;
if(flag[j]&&turn==j)
{
printf("%d can not execute C.S.\n",p_no);
return;
}
printf("%d is executing C.S.\n",p_no);
flag[p_no]=false; //declare that not interested

/*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.
*/

You might also like