Process Synchronization Two Process Solution) (Algo-3) l Decker’s Algorithm.

Process Synchronization Two Process Solution) (Algo-3) l Decker’s Algorithm.



play-rounded-fill play-rounded-outline play-sharp-fill play-sharp-outline
pause-sharp-outline pause-sharp-fill pause-rounded-outline pause-rounded-fill
00:00

Dekker’s Algorithm – Process Synchronization (Two-Process Solution) (Algo-3)

Dekker’s Algorithm is one of the earliest known solutions to the critical section problem for two processes running in a shared memory environment. It ensures mutual exclusion, progress, and bounded waiting.

Problem: Process Synchronization

When multiple processes access shared resources, they need to be synchronized to avoid race conditions and ensure consistency.

Solution: Dekker’s Algorithm

Dekker’s Algorithm uses the following mechanisms:

  1. Flags (want[2]) – Indicates if a process wants to enter the critical section.
  2. Turn Variable – Specifies which process has priority.

Algorithm Steps:

1. Initialization:

  • want[0] = want[1] = false (Initially, both processes do not want to enter.)
  • turn = 0 (Process 0 gets priority initially.)

2. Entry Section:

  • A process sets its want[i] = true (indicating interest).
  • If the other process (j) also wants to enter:
    • The process waits if it is not its turn.
    • If it’s not its turn, it sets want[i] = false and retries later.

3. Critical Section (CS):

  • The process executes its critical section.

4. Exit Section:

  • The process sets want[i] = false.
  • Gives the turn to the other process (turn = j).

Dekker’s Algorithm (Code Implementation in C++)

#include <iostream>
#include <thread>
#include <atomic>

using namespace std;

atomic<bool> want[2] = {false, false};
atomic<int> turn = 0;

void process(int i) {
int j = 1 - i; // The other process
for (int k = 0; k < 5; k++) { // Simulating multiple iterations
want[i] = true;
while (want[j]) {
if (turn != i) {
want[i] = false;
while (turn != i); // Busy wait
want[i] = true;
}
}

// Critical Section
cout << "Process " << i << " is in the Critical Section.\n";

// Exit Section
turn = j;
want[i] = false;
}
}

int main() {
thread t1(process, 0);
thread t2(process, 1);

t1.join();
t2.join();

return 0;
}

Key Properties of Dekker’s Algorithm

Mutual Exclusion – Only one process enters the critical section at a time.
Progress – If no process is in CS, one can enter without indefinite waiting.
Bounded Waiting – No process suffers indefinite postponement.

Limitations

 Works only for two processes.
 Uses busy waiting (inefficient CPU utilization).
 Not suitable for modern multi-core architectures.

Would you like an explanation of an alternative algorithm like Peterson’s Algorithm or Lamport’s Bakery Algorithm?

Process Synchronization Two Process Solution) (Algo-3) l Decker’s Algorithm.

Process Synchronization



Leave a Reply

Your email address will not be published. Required fields are marked *

error: