What is Peterson's solution?

Peterson's solution ensures mutual exclusion. It is implemented in user mode and no hardware support is required therefore it can be implemented on any platform. Now Peterson’s solution uses two variables: interest and Turn variable.

Now we will first see Peterson solution algorithm and then see how any two processes P and Q get mutual exclusion using Peterson solution.

#define N 2
#define TRUE 1
#define FALSE 0
int interested[N]=False
int turn;
void Entry_Section(int process)
   int other;
   interested[process]= TRUE ;
   turn = process;
   while(interested[other]==TRUE && Turn=process);
void exit_section(int process)


There will be two processes and the process number of the first process=0 and the process number of the second process is equal to 1.

So, if process 1 calls entry_section then other = 1-process =1-1=0.

If process 0 calls then other = 1-process = 1-0 = 1

Now, since the process which called entry_section it means that process want to execute a critical section then that process will set interested[process]=TRUE

So, if process 1 called entry section then interested[1]=TRUE

If process 0 is called entry section then interested[0]=TRUE

After declaring that process is interesting it will set its turn. So, if process 1 is called then turn =1.

Then, while (interested[other]==TRUE && Turn=process); will be executed.

In this line, the process checks whether other processes are interested or not. If that process is interested then interested[other]==TRUE will be true then the process thinks that it may happen that another process is executing the critical section.

For that, it will go into a loop until another process is not interesting. Now if another process becomes interested then interested[other]==TRUE

It will become False and the process will enter into a critical section. So, in this way, only one process may enter into the critical section. Therefore, mutual exclusion is guaranteed in Peterson's solution. While exiting the critical section process will set interest as False.

Updated on: 01-Dec-2021

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started