Critical-Section Problem. (Dekker)

  • Thread starter Thread starter Peon666
  • Start date Start date
Click For Summary
SUMMARY

The discussion focuses on Dekker's algorithm, the first known correct software solution to the critical-section problem for two processes. The algorithm utilizes two shared variables, a boolean array `flag[2]` and an integer `turn`, to manage access to the critical section. The code ensures that only one process can enter the critical section at a time by using flags to indicate intent and a turn variable to enforce order. The provided code aligns with the correct implementation found on Wikipedia, highlighting the importance of proper synchronization in concurrent programming.

PREREQUISITES
  • Understanding of concurrency and synchronization in operating systems
  • Familiarity with boolean logic and control structures in programming
  • Knowledge of shared memory concepts in multi-threaded environments
  • Basic understanding of Dekker's algorithm and its historical significance
NEXT STEPS
  • Study the implementation details of Dekker's algorithm in various programming languages
  • Explore other synchronization algorithms such as Peterson's algorithm and Lamport's bakery algorithm
  • Learn about mutexes and semaphores for process synchronization
  • Investigate the impact of race conditions and how to prevent them in concurrent programming
USEFUL FOR

Computer science students, software engineers, and anyone interested in understanding process synchronization and concurrent programming techniques.

Peon666
Messages
107
Reaction score
0
I read this code in my OS book but I'm having trouble understanding it. Would someone explain to me what's happening in this code? This code is the first-known correct software solution to the critical-section problem for two processes and was developed by Dekker.

Code:
boolean flag[2];
int turn;

// Above two variables are shared by the two processes.

do {
    flag[i]=TRUE;
    while (flag[j]){
        if (turn == j){
            while (turn == j)
               ;// do nothing
            flag[i]=FALSE;
        }
    }

// critical section

turn = j;
flag[i]=FALSE;

// remainder section.

}while (TRUE);

I'm totally clueless about what's going on in this code.
 
Technology news on Phys.org
The example you show isn't quite right, wiki shows the proper version:

http://en.wikipedia.org/wiki/Dekker's_algorithm

This matches the wiki version:

Code:
volatile boolean flag[2] = {FALSE, FALSE}  // global shared variable
volatile int turn = 0;                     // global shared variable

static int i, j;

// initialize
// for process 0, i = 0, j = 1
// for process 1, i = 1, j = 0

do{

// non-critical section

    flag[i] = TRUE;              // set flag[i] to request ownership
    while(flag[j] == TRUE){      // while other process has set flag[j]
        if(turn != i){           //   if turn != this process
             flag[i] = FALSE;    //     clear flag[i] so other process can run
             while(turn != i);   //     wait for other process to change turn
             flag[i] = TRUE;     //     set flag[i] to request ownership
        }
    }                            // wait until other process clears flag[j]
 
// critical section

    turn = j;                    // set turn to other process
    flag[i] = FALSE;             // clear flag[i] so other process can run

// non-critical section

}while (TRUE);
 
Last edited:

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
2
Views
3K
Replies
10
Views
2K
Replies
2
Views
2K
  • · Replies 97 ·
4
Replies
97
Views
9K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 38 ·
2
Replies
38
Views
5K