# N-Process Peterson Algorithm

In this tutorial, we will learn about N-Process Peterson Algorithm. But before knowing the N Process Peterson Algorithm we should learn about "What is Peterson's Algorithm?" The Peterson's Algorithm and Synchronization are the pre - requisites for N Process Peterson Algorithm. So, now let us learn about Peterson's Algorithm.

### Synchronization

Synchronization is the process by which Operating System performs multiple operations. But, these multiple operations are made to share the same memory space. This process is managed by the operating system. So, this process of sharing same memory space for different processes is known as Synchronization.

In every operating system, it is crucial that different processes run in sync with one another. Knowing which process will be permitted to execute its crucial segment part, where they can be modifying some crucial variables at what moment among other processes, is crucial since failing to do so leads to unfavorable consequences or results.

As a result, several strategies are explored and developed to achieve process synchronization, with Peterson's algorithm being one of them. One of the most well-known critical-section situations is the producer-consumer problem.

In this scenario, improper synchronization implementation will cause the count of objects inside the buffer to constantly decrease, producing an undesirable outcome.

## Peterson Algorithm

The Peterson's Algorithm is used for Process Synchronization. It is a critical section problem. This problem has a software based solution traditionally. t is basically used to synchronize two processes.

Peterson's Algorithm preserves and works on basis of these three conditions. They are:

1. Here, every process or job gets its turn in a fairer way. So, here Bounded Waiting is preserved
2. Here, in every cycle only one process is executed and enters the critical section. So, due to this reason Mutual Exclusion is preserved.
3. The fact that a process outside the critical section does not prevent other processes from entering the critical section ensures progress as well.

Algorithm

This is the pre - requisite for the N Process Peterson's Algorithm. Now, let us learn about N Process Peterson's Algorithm.

### N-Process Peterson's Algorithm

This algorithm uses the same process for execution, but there lies a small difference. Here, we use an N sized Array. We also use the same data structure but the difference lies is the use of Array of Nth Size. This algorithm is very large. This algorithm is very large because, the Peterson algorithm has to visit each and every process and continuously refer them and referring to the Turn variable which contains complementary process's number. All this process happens inside the while loop. This is the statement to be noted.

Refer, this algorithm to understand the working of the N Process Peterson's Algorithm.

Algorithm

Explanation

Consider a queue that will hold N processes and has a size of N.

Let's say we create an algorithm as every process pushes itself forward in the line to reach the front at some point, at which point they are permitted to complete their crucial tasks and go on.

Every process pushes itself forward in the line to reach the front at some point, at which point they are permitted to complete their crucial tasks and go on. By doing this, we can ensure that every process, once it enters the queue and reaches the conclusion, is permitted to carry out its crucial portion (front).

There may be two circumstances in play at that time, therefore the process should wait until it reaches the front.

Either some processes are waiting for someone else at the next index in the queue, or they may not be.

Or if any back process doesn't relocate to the location of the active process.

The algorithm's data structure is the same as for the first of two processes, but the Turn and the Flag are now both arrays of size N.

Additionally, when some processes exit the queue or enter another one for another crucial portion after completing their critical section, the queue's size may gradually shrink.

From this explanation, we can clearly understand that the mentioned algorithm obeys all the regulations put up during the process of Synchronization and Concurrent Processes in any Computer environment which uses the Operating System.

Hence, because of this we are able to get to know so much about the N Process Peterson Algorithm. This is all about N Process Peterson's Algorithm. This is how the Processes are synchronized for so many processes.

### Feedback   