Dekker’s & Peterson’s Solutions in OS: Critical Section Explained Simply

Operating System Chapter 9 · Two-Process Algorithm Arena
Dekker’s & Peterson’s Solutions in OS: Critical Section Explained Simply

Dekker’s and Peterson’s algorithms are classical software solutions to the two-process critical-section problem. Both use shared variables to decide which process may enter the critical section when P0 and P1 compete at the same time. Dekker’s solution uses two interest flags plus a turn variable and temporarily withdraws a process when both want to enter. Peterson’s solution is simpler: each process raises its flag, gives the other process priority through the turn variable, and waits only when the other process is interested and has priority. Under the classical textbook assumptions of atomic reads/writes and proper memory visibility, both are designed to satisfy mutual exclusion, progress and bounded waiting for two processes.

Dekker's solution Peterson's solution flag[] turn Mutual exclusion Bounded waiting
Why these algorithms exist

Why Was the Critical-Section Problem Difficult?

Dekker’s & Peterson’s Solutions in OS: Critical Section Explained Simply

Two processes may want to enter the same critical section at nearly the same time. A simple idea such as “check a variable and then enter” can fail because the check and update may happen in an unsafe order.

One shared lock variable Both processes can read “free” before either process changes the variable.
Strict alternation Forces turns even when one process does not want to enter, so progress can suffer.
Only two interest flags If both raise their flags together and simply wait for the other flag to fall, both can wait forever.

Dekker and Peterson combine “interest” and “priority” so two processes can coordinate without using a hardware lock instruction.

Revise Critical Section & Mutual Exclusion in OS if mutual exclusion, progress and bounded waiting are not clear yet.

The two-process arena

What Do flag[] and turn Actually Mean?

Process P0 flag[0] = true means P0 wants to enter.
TURNbreaks a tie when both are interested
Process P1 flag[1] = true means P1 wants to enter.
flag[0]P0's interest
turntie-breaking priority
flag[1]P1's interest
flag = “I want to enter” · turn = “if both want to enter, who should wait?”
Before the algorithms

What Must a Correct Two-Process Solution Guarantee?

Mutual Exclusion P0 and P1 must never be inside the same critical section at the same time.
Progress If the critical section is empty and a process wants to enter, the decision should not be postponed forever by an uninterested process.
Bounded Waiting After a process asks to enter, the other process should not keep entering forever before it gets a turn.

These are exactly the three conditions introduced in the critical-section chapter.

Algorithm 01 · Dekker

What Is Dekker’s Solution?

Dekker’s solution uses two interest flags and a turn variable. If both processes want the critical section, the process whose turn is not favored temporarily withdraws its interest, waits for its turn, and then tries again.

That temporary withdrawal is the special idea that makes Dekker different from a simple two-flag solution.

Dekker = interest flags + turn + temporary withdrawal
Dekker pseudocode

Dekker’s Algorithm for P0 and P1

Process P0

while (true) {
    flag[0] = true;

    while (flag[1]) {
        if (turn == 1) {
            flag[0] = false;

            while (turn == 1) {
                ; // wait
            }

            flag[0] = true;
        }
    }

    // critical section

    turn = 1;
    flag[0] = false;

    // remainder section
}

Process P1

while (true) {
    flag[1] = true;

    while (flag[0]) {
        if (turn == 0) {
            flag[1] = false;

            while (turn == 0) {
                ; // wait
            }

            flag[1] = true;
        }
    }

    // critical section

    turn = 0;
    flag[1] = false;

    // remainder section
}

Read the code as a conversation. “I want to enter” is the flag. “If both want to enter, whose turn is it?” is the turn variable. The process that loses the tie backs off, then tries again later.

Dekker walkthrough

Dekker’s Solution: Both Processes Request at the Same Time

Assume both flags become true and turn = 0.

P0 sets flag[0] = trueP0 announces that it wants to enter.
P1 sets flag[1] = trueP1 also wants to enter.
P1 sees turn = 0P1 is not favored, so it temporarily changes flag[1] to false.
P0 sees flag[1] = falseP0 can leave its waiting loop and enter the critical section.
P0 finishesP0 gives turn to P1 and lowers flag[0].
P1 retriesP1 raises its flag again and can now enter when conditions allow.

This tie-breaking and backing-off behavior prevents the “both flags true forever” deadlock of the naive two-flag approach.

Why Dekker works

How Does Dekker Satisfy the Three Requirements?

RequirementHow Dekker Handles It
Mutual ExclusionIf both processes are interested, turn decides which process backs off, so they do not both enter.
ProgressIf only one process is interested, the other flag is false and the interested process can enter.
Bounded WaitingAfter leaving, a process gives turn to the other process, preventing unlimited repeated entry by one side when both compete.
Algorithm 02 · Peterson

What Is Peterson’s Solution?

Peterson’s solution is a simpler two-process mutual-exclusion algorithm that uses two interest flags and one turn variable. A process first says “I want to enter,” then gives the other process priority if both request together.

For process Pi, let j be the other process.

flag[i] = true · turn = j · wait while flag[j] and turn == j
Peterson pseudocode

Peterson’s Algorithm for P0 and P1

Process P0

while (true) {
    flag[0] = true;
    turn = 1;

    while (flag[1] && turn == 1) {
        ; // wait
    }

    // critical section

    flag[0] = false;

    // remainder section
}

Process P1

while (true) {
    flag[1] = true;
    turn = 0;

    while (flag[0] && turn == 0) {
        ; // wait
    }

    // critical section

    flag[1] = false;

    // remainder section
}

Peterson’s algorithm is shorter because it does not need Dekker’s “lower my flag, wait for turn, raise my flag again” pattern.

Peterson dry run

Peterson’s Solution: Both Processes Request Together

Suppose P0 and P1 both set their flags to true.

P0 writes turn = 1P0 politely gives P1 priority if both compete.
P1 writes turn = 0P1 then gives P0 priority. The last write determines the current value of turn.
Assume turn is now 0P1 checks flag[0] && turn == 0, so P1 waits.
P0 checks its conditionflag[1] is true, but turn == 1 is false, so P0 enters.
P0 exits and sets flag[0] = falseP1’s waiting condition becomes false.
P1 enters nextBoth are served without entering together.
Peterson state logic

When Does Peterson Allow a Process to Enter?

Other Process Interested?turn Favors Other?What Happens?
NoDoes not matterEnter immediately
YesNoEnter
YesYesWait
Peterson waits only when BOTH are true: other is interested AND turn favors other
Why Peterson works

How Does Peterson Satisfy Mutual Exclusion, Progress & Bounded Waiting?

RequirementReason
Mutual ExclusionIf both flags are true, turn can favor only one side at a time, so one process waits.
ProgressIf the other process is not interested, its flag is false and the interested process does not wait.
Bounded WaitingA requesting process gives the other side priority through turn, preventing one process from repeatedly overtaking forever under the classical assumptions.
Big comparison

Dekker’s vs Peterson’s Solution

Dekker’s Solution

Uses flags and turn, but includes a back-off step where a process temporarily clears its flag when it loses the tie.

Peterson’s Solution

Uses flags and turn in a shorter form. A process raises its flag and directly gives the other process priority through turn.

PointDekkerPeterson
Processes supportedTwoTwo
Shared variablesTwo flags + turnTwo flags + turn
Main special ideaBack off by lowering flag when losing turnGive other process priority through turn
Code complexityMore complexSimpler
Busy waitingYesYes
Hardware atomic lock required?No, under textbook assumptionsNo, under textbook assumptions
Main teaching valueShows careful software tie-breakingElegant classical software solution
Interactive contention explorer

See Which Process Enters in Peterson’s Algorithm

Select a simple contention case. The visual explains the decision using flags and turn.

Only P0 Wants to Enter

flag[0] = true flag[1] = false P0 checks: flag[1] && turn == 1 false && anything = false P0 does not wait.

Decision

P0 enters immediately P1 is not interested, so P0's waiting condition is false regardless of the current turn value.
PROGRESS MAINTAINED
Why not use them everywhere?

Limitations of Dekker’s and Peterson’s Algorithms

Only two processesThe basic textbook versions solve mutual exclusion for exactly two processes.
Busy waitingA waiting process repeatedly checks shared variables and consumes CPU time.
Memory-order assumptionsModern compilers and CPUs can reorder memory operations unless the implementation uses suitable synchronization semantics.
Not typical application-level practiceReal software normally uses mutexes, semaphores, monitors or language/runtime synchronization primitives.
Harder to maintainHand-written shared-variable algorithms are easier to get wrong than tested synchronization primitives.
Teaching focusTheir biggest value is understanding mutual exclusion logic, fairness and software synchronization.

So Peterson’s algorithm is excellent for learning the idea, but it should not be treated as a drop-in replacement for modern synchronization libraries.

Important technical assumption

Why Do Textbooks Mention Atomic Reads and Writes?

Both algorithms depend on processes seeing shared values such as flag and turn in the intended order.

The classical proof assumes reads and writes of these shared variables behave atomically and with memory-order behavior suitable for the algorithm.

On modern hardware, ordinary variables can be affected by compiler optimizations, caches and memory reordering. Practical implementations therefore need proper atomic or memory-ordering primitives.

Dekker vs strict alternation

Why Is Dekker Better Than Strict Alternation?

Strict Alternation

P0 and P1 must take turns even if one process does not want to enter. This can violate progress.

Dekker

A process can enter when the other process is not interested. turn is mainly used to settle actual competition.

Good synchronization should coordinate competition, not force useless waiting.
Peterson vs simple two flags

Why Is Peterson Better Than “Just Use Two Flags”?

Imagine this naive rule:

flag[i] = true · while(flag[j]) wait

If P0 and P1 both set their flags to true at the same time, each sees the other flag as true and both wait forever.

Peterson adds turn to break this tie. When both are interested, turn decides which waiting condition stays true and which process can proceed.

Relation with semaphores

Dekker/Peterson vs Semaphore

PointDekker/PetersonSemaphore
TypeClassical software algorithmsSynchronization primitive
Basic versionTwo processesCan coordinate many processes/threads
Main shared dataFlags and turnSemaphore state plus waiting mechanism
WaitingBusy waitingCan use blocking or spinning depending implementation
Typical modern useMainly educational/theoreticalCommon synchronization concept and implementation tool

For semaphore basics, revise Semaphores in OS: Wait, Signal, Test-and-Set & Synchronization.

Common exam traps

Common Student Mistakes

flag does not mean “inside critical section”It means the process wants to enter.
turn alone is not enoughStrict alternation can force unnecessary waiting.
Two flags alone are not enoughBoth processes can become stuck if both raise flags together.
Peterson sets turn to the other processProcess Pi writes turn = j, not turn = i.
Peterson uses AND in the wait conditionIt waits only when the other process is interested and turn favors the other.
Dekker temporarily lowers its flagThis is the back-off step when it loses the tie.
Both are two-process algorithmsDo not present the basic form as a general N-process solution.
Both use busy waitingThey do not block the waiting process like a scheduler-supported semaphore can.
Exam answer blueprint

How to Write Dekker’s & Peterson’s Solutions in an Exam

2 marks

Short definition

State that each is a two-process software solution to the critical-section problem using shared variables.

5 marks

One algorithm

Write variables, pseudocode, one simultaneous-request example and explain mutual exclusion.

8-10 marks

Comparison answer

Explain failed simple approaches, Dekker, Peterson, mutual exclusion/progress/bounded waiting, busy waiting and a comparison table.

Fast memory trick

Dekker backs off · Peterson gives the other side turn
Quick practice

Practice Questions With Answers

1. What problem do Dekker’s and Peterson’s algorithms solve?

They solve the two-process critical-section problem using shared software variables under classical textbook assumptions.

2. What does flag[i] mean?

flag[i] = true means process Pi is interested in entering the critical section.

3. What is the purpose of the turn variable?

turn breaks a tie when both processes want to enter the critical section.

4. What is special about Dekker’s solution?

When both processes compete, the process that loses the turn temporarily lowers its interest flag, waits, and then tries again.

5. What is Peterson’s waiting condition for Pi?

Pi waits while flag[j] is true and turn equals j, where j is the other process.

6. Why are two flags alone not enough?

If both processes set their flags to true at the same time and each waits for the other flag to become false, both can wait forever.

7. Do Dekker and Peterson use busy waiting?

Yes. Waiting processes repeatedly check shared conditions.

8. How many processes does basic Peterson’s solution support?

The basic textbook Peterson solution supports two processes.

9. Which is simpler, Dekker or Peterson?

Peterson’s solution is generally simpler and shorter to understand and write.

10. Are these algorithms commonly used directly in modern application code?

Usually no. They are mainly studied to understand software mutual exclusion; real programs normally use synchronization primitives provided by the language, runtime or operating system.

Revision matrix

One-Minute Revision

DekkerTwo flags + turn + back-off.
PetersonTwo flags + turn, simpler logic.
flag[i]Pi wants critical-section entry.
turnBreaks a tie when both compete.
Mutual exclusionOnly one process enters at a time.
ProgressUninterested process should not block the interested one.
Bounded waitingNo unlimited overtaking.
Busy waitingWaiting process repeatedly checks variables.
Dekker back-offLosing process temporarily lowers its flag.
Peterson waitWait while other interested AND turn favors other.
Basic scopeTwo processes only.
Modern lessonUse tested synchronization primitives in real software.
Frequently asked questions

Frequently Asked Questions

What is Dekker’s algorithm in OS?

Dekker’s algorithm is a classical two-process software mutual-exclusion solution that uses two interest flags and a turn variable, including a back-off step when both processes compete.

What is Peterson’s algorithm in OS?

Peterson’s algorithm is a two-process critical-section solution that uses two interest flags and a turn variable to provide mutual exclusion, progress and bounded waiting under classical assumptions.

What is the main difference between Dekker and Peterson?

Dekker uses a more complex back-off mechanism when both processes compete, while Peterson solves the same two-process problem with a shorter flag-and-turn rule.

Why does Peterson use a turn variable?

The turn variable breaks the tie when both processes want to enter the critical section at the same time.

Do Dekker and Peterson satisfy bounded waiting?

Yes, under the classical textbook assumptions, both are designed to satisfy mutual exclusion, progress and bounded waiting for two processes.

Why are Peterson and Dekker mostly studied theoretically today?

They are excellent for understanding software mutual exclusion, but real systems normally use tested mutexes, semaphores and atomic synchronization primitives that also handle modern memory-ordering requirements.

Next classical problem

What Should You Learn Next?

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Join Telegram Exam updates and free resources