0% found this document useful (0 votes)
159 views4 pages

Lamport's Bakery Algorithm

Lamport's Bakery Algorithm is a mutual exclusion algorithm that uses an array of booleans and integers to determine which process can enter the critical section. Each process first sets its choosing value to true, picks a unique number, then sets choosing back to false. It then waits for all other processes to finish their choosing sections and for any other process with a lower number or equal number but lower ID to finish its critical section before entering its own critical section. This ensures only one process is in the critical section at a time and that processes are served in number/ID order.

Uploaded by

balai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
159 views4 pages

Lamport's Bakery Algorithm

Lamport's Bakery Algorithm is a mutual exclusion algorithm that uses an array of booleans and integers to determine which process can enter the critical section. Each process first sets its choosing value to true, picks a unique number, then sets choosing back to false. It then waits for all other processes to finish their choosing sections and for any other process with a lower number or equal number but lower ID to finish its critical section before entering its own critical section. This ensures only one process is in the critical section at a time and that processes are served in number/ID order.

Uploaded by

balai
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lamport's Bakery Algorithm

// choosing is an array of n booleans, initially false


// number is an array of n ints, initially 0
do
{
choosing[i] = true;
number[i] = max( number[0],number[1],...,number[n-1] )
+ 1; // possible to receive same number?
choosing[i] = false;
for( int j = 0; j < n; j++ )
{
while( choosing[j] ); // NOP
while( number[j] != 0 &&
(number[j],j) < (number[i],i) ); // NOP
}
doCriticalSection();
number[i] = 0;
doRemainderSection();
} while ( true );

(a,b) < (c,d) translates to (a < c) || (a == c && b < d)


2 Processes using Lamport’s Bakery Algorithm
# Process 0 Process 1

1 choosing[0] = true;
2 Number[0] = 1 choosing[1] = true;
3 Choosing[0] = false; Number[1] = max(1) + 1 = 2
4 LOOP 0: Choosing[0] = false; Choosing[1] = false;
5 Number[0] = 1 → 1 != 0 → TRUE && Loop 0: Choosing[0] = false;
*Number[0] = 1 < Number[0] = 1 → FALSE → break
6 Loop 1: Choosing[1] = false; Number[0] = 1 → 1 != 0 → TRUE &&
Number[0] = 1 < Number[1] = 2 → TRUE
7 Number[1] = 2 → 2 != 0 → TRUE && Number[0] = 1 → 1 != 0 → TRUE &&
Number[1] = 2 < Number[0] = 1 → FALSE → Breaks Number[0] = 1 < Number[1] = 2 → TRUE
8 doCriticalSection(); Number[0] = 1 → 1 != 0 → TRUE &&
Number[0] = 1 < Number[1] = 2 → TRUE]
9 Number[0] = 0; Number[0] = 1 → 1 != 0 → TRUE &&
Number[0] = 1 < Number[1] = 2 → TRUE
10 doRemainderSection() Number[0] = 0 → 0 != 0 → FALSE &&
Number[0] = 1 < Number[1] = 2 → TRUE
11 Loop 1: Choosing[1] = false;
12 Number[1] = 2 → 2 != 0 → TRUE &&
Number[1] = 2 < Number[1] = 2 → FALSE
13+ doCriticalSection(); …
2 Processes with Same Number Example
# Process 0 Process 1
1 choosing[0] = true; choosing[1] = true;
2 Number[0] = 1 Number[1] = 1
3 Choosing[0] = false; Choosing[1] = false;
4 Loop 0: choosing[0] = false; Loop 0: choosing[0] = false;
5 Number[0] = 1 != 0 ➔ TRUE && Number[0] = 1 != 0 ➔ TRUE &&
Number[0] = 1 < Number[0] = 1 ➔ ➔ Number[0] = 1 < Number[1] = 1 ➔
Number[0] = 1 == Number[0] == 1 && NumberID = 0 < NumberID = 0 → FALSE Number[0] = 1 == Number[1] == 1 && NumberID = 0 < NumberID = 1 → TRUE
→ BREAK
6 Loop 1: choosing[0] = false; loop

7 Number[1] = 1 != 0 ➔ TRUE && loop


Number[1] = 1 < Number[0] = 1 ➔ ➔
Number[1] = 1 == Number[0] == 1 && NumberID = 1 < NumberID = 0 → FALSE
→ BREAK
8 doCriticalSection() loop
9 Number[0] = 0 loop
10 remainder Number[0] = 0 != 0 ➔ FALSE &&
Number[0] = 1 < Number[1] = 1 ➔
Number[0] = 1 == Number[1] == 1 && NumberID = 0 < NumberID = 1 → TRUE
11 Loop 1: choosing[0] = false;
12 Number[1] = 1 != 0 ➔ TRUE &&
Number[1] = 1 < Number[1] = 1 ➔ ➔
Number[1] = 1 == Number[1] == 1 && NumberID = 1 < NumberID = 1 → FALSE
→ BREAK
13+ doCriticalSection
Key Points
• “while( choosing[j] );”
• Makes sure the code is not in the choosing section
• “while( number[j] != 0 &&”
• Checks if process j is still trying to access the
critical section
• Will be FALSE if process j is done
• “(number[j],j) < (number[i],i) );”
• If the ticket number is smaller than the ticket number of
the process it is in, then it will loop
• Ensures that the smaller process ID goes first
• If the process ID of the current process is bigger, it will not
run

You might also like