Critical-Section Problem. (Dekker)

  • Thread starter Peon666
  • Start date
In summary, this is the code for Dekker's algorithm, which is the first-known correct solution to the critical-section problem for two processes. It uses two shared variables, flag and turn, to ensure that only one process can access the critical section at a time. The process sets its own flag and then checks the other process's flag. If the other process has set its flag, the process waits until it is its turn to access the critical section. Once it has finished its critical section, it sets the turn to the other process and clears its own flag. This continues in an infinite loop to ensure mutual exclusion.
  • #1
Peon666
108
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
  • #2
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:

1. What is the critical-section problem in computer science?

The critical-section problem refers to the challenge of coordinating the use of shared resources in a multi-threaded or multi-process environment. It arises when multiple processes or threads need to access the same resource, such as a file or a shared variable, and there is a possibility of data inconsistency or race conditions.

2. Who is Dekker and what is his contribution to the critical-section problem?

Edsger Dijkstra and Thoth Dekker were two Dutch computer scientists who independently proposed solutions to the critical-section problem in the early 1960s. Dekker's solution, known as the Dekker algorithm, was the first practical solution to the problem and is still widely used today.

3. How does the Dekker algorithm work?

The Dekker algorithm uses two flags and a turn variable to control access to the critical section. Each process or thread checks the flags to see if the other process is in its critical section. If not, it sets its own flag to signal its intent to enter the critical section. It then checks the turn variable and waits until it is its turn to enter the critical section. Once finished, the process or thread resets its flag and passes the turn to the other process.

4. What are the advantages of the Dekker algorithm?

The Dekker algorithm is simple and easy to implement, making it a popular solution to the critical-section problem. It also allows for parallel execution of processes or threads, as long as they are not accessing the same critical section. Additionally, it avoids deadlock and starvation by ensuring that each process or thread eventually gets its turn in the critical section.

5. What are the limitations of the Dekker algorithm?

One major limitation of the Dekker algorithm is that it does not provide mutual exclusion, meaning that multiple processes or threads can still enter the critical section simultaneously. This can lead to data inconsistency and race conditions. Additionally, the algorithm does not guarantee progress, as a process or thread could continuously be blocked by the other process or thread if they are constantly setting and resetting flags.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
Replies
10
Views
1K
  • Programming and Computer Science
3
Replies
97
Views
7K
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
4
Views
913
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
Back
Top