Winter Semester 2026–27 · VIT-AP · BACSE106 · Operating Systems · Dr. Krithika L.B

The Bakery algorithm

Tokens, tie-breaking and a step-by-step n-process trace.

Module 3 Session 3.2 Lesson 4 of 4

The Bakery algorithm

Lamport's n-process solution: take a token, wait for every smaller token, enter.

Rule

Pi may enter when, for every other Pj, either number[j] == 0 (not interested) or (number[i], i) < (number[j], j). The pair comparison means: smaller number first; equal numbers → smaller id first.

Three processes, two of which draw the same numberStep through the interleaving one instruction at a time.

Why each requirement holds

Mutual exclusion

Pairs (number, id) are all distinct, so exactly one process has the smallest pair among those interested.

Progress

The process with the smallest pair never waits for anyone, so some process always enters.

Bounded waiting

A process that takes a token after Pi gets a larger number, so it cannot overtake Pi; Pi waits at most n − 1 entries.

Quick check

number[] = {0, 3, 0, 3, 2} for P0..P4. Who enters first?

Smallest number is 2 (P4).

…and second?

(3, 1) < (3, 3).

What does choosing[j] protect against?

Otherwise Pi could see 0 and enter together with Pj.