0% found this document useful (0 votes)
20 views13 pages

Lamport Baker

The Bakery algorithm provides a deadlock-free mutual exclusion solution using timestamps. [1] It works by having each thread pick a new timestamp that is larger than any others currently in use, and then wait until its timestamp is the smallest. [2] The algorithm proves to be deadlock-free, first-come-first-served, and satisfies mutual exclusion. [3] However, it requires reading all n timestamps on each lock and unlock operation, making it impractical for real-world use.
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)
20 views13 pages

Lamport Baker

The Bakery algorithm provides a deadlock-free mutual exclusion solution using timestamps. [1] It works by having each thread pick a new timestamp that is larger than any others currently in use, and then wait until its timestamp is the smallest. [2] The algorithm proves to be deadlock-free, first-come-first-served, and satisfies mutual exclusion. [3] However, it requires reading all n timestamps on each lock and unlock operation, making it impractical for real-world use.
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/ 13

Lamport’s Bakery Algorithm

class Bakery implements Lock {


boolean[] flag;
Smallest non-zero label
Label[] label;
Used to gets to go
public Bakery (int n) {
declare
interest, flag = new boolean[n];
as label = new Label[n];
earlier
for (int i = 0; i < n; i++) {
flag[i] = false; label[i] = 0;
}
}
Lamport’s Bakery Algorithm

I want the cs Doorway

class Bakery implements Lock {



public void lock() {
flag[i] = true;
label[i] = max(label[0], …,label[n-1])+1;
while (exists k flag[k]
&& (label[i],i) > (label[k],k)) {};
}

Pick a new cket, later than any already in use

Wait unl the new cket is minimal


Uses lexicographic order to break symmetry
Lamport’s Bakery Algorithm

class Bakery implements Lock {

public void unlock() {


flag[i] = false;
}
}
The Bakery Algorithm, Correctness
Lemma: The Bakery algorithm is deadlock free
Proof: Eventually a waing thread will hold the least (label,
thread) pair. QED

Lemma: The Bakery algorithm is rst-come-rst-served


Proof: First through the doorway gets the smallest “cket”. QED

Lemma: The Bakery algorithm sases mutual exclusion


Proof: Check the textbook.
Timestamps
Bakery algorithm:
• Timestamp: (label, thread) pair
• Timestamp domain unbounded for correctness

Two operaons needed:


• scan: Sample the label array
• label: Produce a new label, strict upper bound of scanned
labels

Possible to do this using only bounded data?


Bounded Timestamps
Possible to build bounded mestamp system which
• Is wait-free – i.e. no locks
• For mulple threads
(cf. Dolev-Shavit-SIAM-97)

Here only sequenal soluon


• Not wait-free
• So useless for the Bakery algorithm
• Sll of interest
Two Thread Bounded Precedence Graph

A A

A B: A has precedence over B B A: B has precedence over A

Note that precedence here is not transive!


Three Thread Bounded Precedence Graph?

A C

But is not transive!


Eh? Want A C as well? What if B wants precedence?
Three Thread Bounded Precedence Graph
0

0
2 1

0 0

2 1
2 1 2 1

Edge from all vertex in cycle 2 to any vertex in cycle 1 etc


Three Thread Bounded Precedence Graph

0 0

0 0
2 1 2 1

0 0
C B C
0 0

2 1 2 1
2 1 2 1 2 1 2 1
A B A

B moves
Three Thread Bounded Precedence Graph

C
0 0

0 0
2 1 2 1

0 0
C
0 0

2 1 2 1
2 1 2 1 2 1 2 1
A B A B

C moves
n Thread Bounded Precedence Graph
And so on … 0
0
n thread graph size is 3n-1 (!) 2 1
0 0
2 1
2 1 2 1

0 0
0 0
2 1 2 1
0 0 0 0
2 1 2 1
2 1 2 1 2 1 2 1
So Is the Bakery Algorithm the Way to Go?
• The Bakery Algorithm is
– Succinct,
– Elegant, and
– Fair.
• Q: So why isn’t it praccal?
• A: Well, you have to read n disnct variables

Art of Multiprocessor
57
Programming

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy