You are on page 1of 25

# Bakery Algorithm - Proof

OS Winter 06

## Bakery algorithm of Lamport

 Critical section algorithm for any n>1  Each time a process is requesting an
entry to CS, assign it a ticket which is
Unique and monotonically increasing

##  Let the process into CS in the order of

their numbers

OS Winter 03

Choosing a ticket
Shared : int number[ n ]; boolean choosing [n];
number [i ] ! max( number[0], number [1], - number [ n  1])  1;

## ( nu ber[ j ], j ) (nu ber[i ], i )

Process need to know that somebody perhaps chose a smaller number:
choosing[i ] ! true; nu ber[i ] ! max(nu ber[0], nu ber[1], - nu ber[n  1])  1; choosing[i ] ! false;
OS Winter 03

## Bakery algorithm for n processes

Process Pi do { choosing[i ] ! true; number[i ] ! max(number[0], number[1],- number[n  1])  1; choosing[i ] ! false; for ( j ! 0; j n; j   ) { while(choosing[ j ]); while(( number[ j ]! ! 0) & &((number[ j ], j ) (number[i ], i ))); } critical section number[i ] ! 0; remainder section } while(1)
OS Winter 03

## Bakery algorithm for n processes

R
Process Pi do { choosing[i ] ! true; T D number[i ] ! max(number[0], number[1],- number[n  1])  1; choosing[i ] ! false; for ( j ! 0; j n; j   ) { T-D while(choosing[ j ]); while(( number[ j ]! ! 0) & &((number[ j ], j ) (number[i ], i ))); } C critical section number[i ] ! 0; E remainder section } while(1)
OS Winter 03

## Structure of the algorithm

 R code prior to using Bakery algorithm  T creating of a ticket and awaiting for
permission to enter critical section
D creation of a number (first part of a ticket) T-D awaiting for the permission to enter critical section

##  C critical section itself  E code executed upon exit from critical

section
OS Winter 03

Basic Lemma
Lemma 1:
For any i { j , i
i

is in C and

is in C (T - D), then

## (nu ber [i ], i ) ( nu ber [ j ], j )

OS Winter 03

Lemma 1 - proof
 Denote s time point where lemmas conditions
hold  At time s, P is in C (critical section) i
number[i] has been selected and still exists number[j] has been selected and still exists Exist time point t1<s, where P performs a check i (choosing[j]==0) and passes it Exists time point t2, t1<t2<s, where P performs a i check (number[j]!=0) and (number[i],i)<(number[j],j)
OS Winter 03

## Lemma 1 proof (cont)

 Since at time t1 exists (choosing[j]==0)
one of the following has to be true
CASE A: number[j] was chosen after time t1 and before time s CASE B: number[j] was chosen before t1

OS Winter 03

## Lemma 1 proof CASE A

 Since at time t1
checks for permission to enter critical section, computation of number[i] was computed before that and persists until s  Thus, at the time j starts to compute its number[j], it has to take into account of max value of number[i]. So it creates a value which is greater then number[i] at least by 1, and it persists until time s  That is (number[i],i)<(number[j],j) at time s
OS Winter 03

## Lemma 1 proof CASE B

 Both number[i] and number[j] were computed
before t1, thus also before time t2 and persisted until s  At time t2 Pi performed check (number[j]!=0) & (number[j],j)<(number[i],i), which failed, since P is in C at time s i  number[j] was chosen before t2 and persisted, thus first part of the check could not fail, also i and j are different, so  (number[i],i)<(number[j],j) at time s
OS Winter 03

Lemmata 2,3,4
 Lemma 2 mutual exclusion:
Bakery Algorithm satisfies mutual exclusion property

 Lemma 3 progress
Bakery Algorithm guarantees progress

 Lemma 4 fairness
Bakery Algorithm guarantees lockoutfreedom
OS Winter 03

Lemma 2 Proof
 Assume in contradiction that there are two
different processes that have entered critical section  Then conditions of Lemma 1 are true for both processes simmetrically that is (number[i],i)<(number[j],j]) and (number[j],j)<(number[i],i) : contradiction  We conclude that mutual exclusion is satisfied

OS Winter 03

Lemma 3 Proof
 Suppose progress is not guaranteed  Then eventually a point is reached after which
all processes are in T or R  By the code, all the processes in T eventually complete D and reach T-D  Then the process with the lowest (number,ID) pair is not blocked from reaching C, that is enters critical section  We conclude that Bakery algorithm satisfies Progress
OS Winter 03

Lemma 4 Proof
 Consider a particular process     
never reaches C The process eventually completes D and reaches T-D After that any new process that enters D perceives number[i] and chooses a higher number Thus, since P does not reach C, none of these new i processes reach C either But by Lemma 3 there must be continuing progress, that is infinitely many entries to C Contradiction: P blocks the entry to C i
OS Winter 03

Pi in T and suppose it

Remark on Fairness
 A process that obtained a ticket (number[k],k)
will wait at most for (n-1) turns, when other processes will enter the critical section  For example if all the processes obtained their tickets at the same time they will look like (q,1),(q,2)(q,n) In which case process n will wait for processes 1 n 1 to complete the critical section

OS Winter 03

Bibliography
 Nancy Ann Lynch, Distributed
Algorithms, JMC 243.9 LY53  Leslie Lamport A new solution of Dijkstras concurrent programming problem Communications of the ACM 17(8):453-455, 1974
OS Winter 03

Hardware primitives
 Elementary building blocks capable of
performing certain steps atomically
Should be universal to allow for solving versatile synchronization problems

##  Numerous such primitives were

identified:
OS Winter 03

Test-and-Set (TS)
boolean test-and-set(boolean &lock) { temp=lock; lock=TRUE; return temp; } reset(boolean &lock) { lock=FALSE; }
OS Winter 03

## Critical section using TS

Shared boolean lock, initially FALSE do { while(test-and-set(&lock)); critical section; Check yourself! reset(&lock); Is mutual exclusion reminder section; satisfied? } while(1); Is progress satisfied? Is fairness satisfied?
OS Winter 03

Discussion
 Satisfies Mutual Exclusion and Progress  Does not satisfies Fairness  Provides exclusion among unbounded
number of processes
Process IDs and number are unknown

 Busy waiting
Burning CPU cycles while being blocked

OS Winter 03

s: shared, a: local int FAA(int &s, int a) { temp=s; s=s+a; return temp; FAA can be used } as a ticket machine
OS Winter 03

## Critical section using FAA

Shared: int s, turn; Initially: s = 0; turn=0; Process Pi code: Entry: me = FAA(s,1); while(turn < me); // busy wait for my turn Critical section Check yourself! Exit: Is mutual exclusion FAA(turn,1); satisfied?
OS Winter 03

## Is progress satisfied? Is fairness satisfied?

Discussion
 Satisfies all three properties  Supports unbounded number of
processes  Unbounded counter  Busy waiting

OS Winter 03

## Problems with studied synchronization methods

 Critical section framework is inconvenient
for programming  Performance penalty
Busy waiting Too coarse synchronization

##  Using hardware primitives directly results

in non-portable code

OS Winter 03