Peterson Algorithm and Implementation of Algorithm
Peterson Algorithm and Implementation of Algorithm
Synchronization Concepts
In uniprocessor systems the only way concurrency is achieved is through interleavings of instruction
In that case ,an instruction executions of one process may affect those of other processes if the instruction executions reference the same piece of shared data
In multiprocess systems,programs execute and access data from their respective private address spaces.
From time to time ,they execute system call service routines from kernel space.
The system call may access the same kernel data . Here also problems will arise because of concurrency .The operating system must ensure that process see consistent values of shared data .The data values must satisfy a priori specified integrity constraints
Race Condition
A situation where the outcome is unpredictable like in a race ,is called race condition It is also known as data race.A data conflict occurs when proccess access the same shared data concurrently and at least one modifies the data.
When the conflicting accesss are not synchronized a data race is said to occur
Critical section
That part of the program where shared resources are accessed When a process executes code that manipulates shared data (or resource), we say that the process is in a critical section (CS) (for that resource) The value of shared variables collectively determine the state of critical section A state is said to be valid or consistent if it satisfies some integrity constraint
Process 1 Process 2
CS1
CS2
Time
A solution to a synchronization problem entails designing these protocols for processes to be followed before entering and after exiting the critical section
The steps in the protocol are executed in the parts are called entry section and exit section
A solution to a critical section problem involves designing codes for the entry and exit sections
Three Key Requirements for a Valid Solution to the Critical Section Problem
Safety :The intented condition will never be violated .For example in mutual exclusion at any time, at most one process can be executing critical section (CS) code Progress: If no process is in its CS and there are one or more processes that wish to enter the CS, this selection cannot be postponed indefinitely
no process in its remainder section can participate in this decision
Bounded Waiting: After a process P has made a request to enter its CS, there is a limit on the number of times that the other processes are allowed to enter their CS, before Ps request is granted
(otherwise the process could suffer from starvation)
Types of Solutions
No special OS mechanisms
(software approach.)
algorithms whose correctness relies only on the assumption that only one process at a time can access a memory location
Hardware solutions
rely on special machine instructions for locking
Analysis
Achieves Mutual Exclusion (busy wait)
But Progress requirement is not satisfied since it requires strict alternation of CSs.
If one process requires its CS more often than the other, it cant get it.
Analysis
Mutual Exclusion is satisfied but not the progress requirement For the (interleaved) sequence:
flag[0]:=true flag[1]:=true
Both processes will wait forever
RS forever
Petersons Algorithm
Process P0: repeat flag[0]:=true;
// 0 wants in
turn:= 1;
// 0 gives a chance to 1
turn:=0;
// 1 gives a chance to 0
RS forever
RS forever
Proof (progress)
Progress requirement:
Pi can be kept out of CS only if stuck in while loop
flag[j] = true and turn = j.
If Pj has set flag[j], it is also in its while loop, then either Pi or Pj will go depending on value of turn Therefore the progress condition is met
turn:=j;
// but you can go first!
RS forever
What if Pj tries again, and has time to reset flag[ j]=true before Pi gets to its CS?
It must also set turn=i
RS forever
Petersons Algorithm
Process P0: repeat flag[0]:=true;
// 0 wants in
turn:= 1;
// 0 gives a chance to 1
turn:=0;
// 1 gives a chance to 0
RS forever
RS forever
Proof (progress)
Progress requirement:
Pi can be kept out of CS only if stuck in while loop
flag[j] = true and turn = j.
If Pj has set flag[j], it is also in its while loop, then either Pi or Pj will go depending on value of turn Therefore the progress condition is met
turn:=j;
// but you can go first!
RS forever
What if Pj tries again, and has time to reset flag[ j]=true before Pi gets to its CS?
It must also set turn=i