2 Klass Peterson Algoritm -
enters a loop: while (flag[j] && turn == j); . It stays here as long as the other process is interested it is currently that process's turn. Critical Section : Once the loop condition is false, Picap P sub i enters the critical section. Exit : Upon leaving, Picap P sub i
: If no one is in the critical section and a process wants to enter, it will not be blocked by processes outside their critical sections.
sets flag[i] = false , allowing the other process to proceed if it was waiting. 2 klass peterson algoritm
To be an effective solution for the critical-section problem, Peterson’s algorithm satisfies three vital criteria:
: An integer indicating whose turn it is to enter the critical section. Step-by-Step Logic For each process Picap P sub i is the other process): Declare Intent : Picap P sub i sets flag[i] = true to signal it wants to enter. Yield Turn : Picap P sub i enters a loop: while (flag[j] && turn == j);
sets turn = j , graciously giving the other process the first opportunity to enter. : Picap P sub i
: Both processes can never be in the critical section at the same time because the turn variable cannot be two values simultaneously. Exit : Upon leaving, Picap P sub i
: A boolean array where flag[i] = true indicates that process Picap P sub i wants to enter its critical section.