Docsity

PETERSON'S SOLLUTIONS, Study notes of Operating Systems

Toggle sidebar
bg1
Peterson's solution is a synchronization algorithm used in operating systems
to ensure mutual exclusion, meaning that only one process can access a
shared resource at a time. It was proposed by Gary Peterson in 1981 as an
improvement on the earlier Dekker's algorithm.
Peterson's solution works by using two shared variables, namely "turn" and
"flag". The "turn" variable determines which process should execute its
critical section next, while the "flag" variable indicates whether a process is
ready to enter its critical section or not.
The algorithm is as follows:
javascriptCopy code
do { flag[i] = true ; turn = j; while (flag[j] && turn == j); // critical section flag[i] = false ; //
remainder section } while ( true );
Here, i and j are the two processes that are sharing the resource, and
flag[i] and flag[j] are the corresponding flags for the processes. The
algorithm starts by setting flag[i] to true to indicate that process i is ready
to enter its critical section. It then sets turn to j to indicate that process j
should execute its critical section next.
In the while loop, the algorithm checks whether flag[j] is true and whether
turn is equal to j. If both conditions are true, it means that process j is ready
to enter its critical section and has the right to do so. Otherwise, process i
continues to loop and checks again.
Once a process enters its critical section, it executes the code that accesses
the shared resource. After the critical section, the process sets flag[i] to
false to indicate that it has finished using the resource, and then proceeds to
the remainder section where it executes any non-critical code. The other
process can then proceed to enter its critical section if it is ready to do so.
Peterson's solution is a simple and effective algorithm for achieving mutual
exclusion in a two-process system. However, it can be extended to more
than two processes, although it becomes more complex and can suffer from
deadlock and starvation problems in certain situations.

Partial preview of the text

Download PETERSON'S SOLLUTIONS and more Study notes Operating Systems in PDF only on Docsity!

Peterson's solution is a synchronization algorithm used in operating systems to ensure mutual exclusion, meaning that only one process can access a shared resource at a time. It was proposed by Gary Peterson in 1981 as an improvement on the earlier Dekker's algorithm. Peterson's solution works by using two shared variables, namely "turn" and "flag". The "turn" variable determines which process should execute its critical section next, while the "flag" variable indicates whether a process is ready to enter its critical section or not. The algorithm is as follows: javascriptCopy code do { flag[i] = true ; turn = j; while (flag[j] && turn == j); // critical section flag[i] = false ; // remainder section } while ( true ); Here, i and j are the two processes that are sharing the resource, and flag[i] and flag[j] are the corresponding flags for the processes. The algorithm starts by setting flag[i] to true to indicate that process i is ready to enter its critical section. It then sets turn to j to indicate that process j should execute its critical section next. In the while loop, the algorithm checks whether flag[j] is true and whether turn is equal to j. If both conditions are true, it means that process j is ready to enter its critical section and has the right to do so. Otherwise, process i continues to loop and checks again. Once a process enters its critical section, it executes the code that accesses the shared resource. After the critical section, the process sets flag[i] to false to indicate that it has finished using the resource, and then proceeds to the remainder section where it executes any non-critical code. The other process can then proceed to enter its critical section if it is ready to do so. Peterson's solution is a simple and effective algorithm for achieving mutual exclusion in a two-process system. However, it can be extended to more than two processes, although it becomes more complex and can suffer from deadlock and starvation problems in certain situations.