SlideShare a Scribd company logo
Bakery Algorithm
• The idea is similar to a line at the bakery
• A customer takes a number greater than
numbers of other customers
• Each of the threads gets a unique identifier
Bakery Algorithm
Thread i
flag[i]=true;
number[i] = max(number[0], …,number[n-1])+1;
while ($ k!= i
flag[k] && (number[i],i) > (number[k],k)) {};
critical section
flag[i] = false;
Bakery Algorithm
flag[i]=true;
number[i] = max(number[0], …,number[n-1])+1;
while ($ k!= i
flag[k] && (number[i],i) > (number[k],k)) {};
critical section
flag[i] = false;
Doorway
Bakery Algorithm
flag[i]=true;
number[i] = max(number[0], …,number[n-1])+1;
while ($ k!= i
flag[k] && (number[i],i) > (number[k],k)) {};
critical section
flag[i] = false;
I’m interested
Bakery Algorithm
flag[i]=true;
number[i] = max(number[0], …,number[n-1])+1;
while ($ k!= i
flag[k] && (number[i],i) > (number[k],k)) {};
critical section
flag[i] = false;
Take an number
numbers are always increasing!
Bakery Algorithm
flag[i]=true;
number[i] = max(number[0], …,number[n-1])+1;
while ($ k!= i
flag[k] && (number[i],i) > (number[k],k)) {};
critical section
flag[i] = false;
Someone is interested
Bakery Algorithm
flag[i]=true;
number[i] = max(number[0], …,number[n-1])+1;
while ($ k!= i
flag[k] && (number[i],i) > (number[k],k)) {};
critical section
flag[i] = false;
There is someone with a lower
number and identifier.
pair (a,b) > (c,d) if a>c, or a=c and b>d
(lexicographic order)
Deadlock freedom
• The bakery algorithm is deadlock free
• Some waiting thread A has a unique least
(number[A],A) pair, and that thread can enter
the critical section
FIFO
• The bakery algorithm is first-come-first-served
• If DA  DB then A’s number is earlier
– writeA(number[A])  readB(number[A]) 
writeB(number[B])  readB(flag[A])
• So B is locked out while flag[A] is true
flag[i]=true;
number[i] = max(number[0], …,number[n-1])+1;
while ($ k!= i
flag[k] && (number[i],i) > (number[k],k)) {};
critical section
flag[i] = false;

More Related Content

PPT
Advanced Functions 12
DOCX
Boundary Fill Algorithm in C
PDF
OOP 2012 - Hint: Dynamic allocation in c++
PDF
1 borland c++ 5.02 by aramse
PDF
Flood Filling Algorithm in C
PPTX
Sorting Technique
PDF
Functions in Economics Part II.pdf
PPTX
Applications of calculus in commerce and economics ii
Advanced Functions 12
Boundary Fill Algorithm in C
OOP 2012 - Hint: Dynamic allocation in c++
1 borland c++ 5.02 by aramse
Flood Filling Algorithm in C
Sorting Technique
Functions in Economics Part II.pdf
Applications of calculus in commerce and economics ii

What's hot (18)

PPTX
Applications of calculus in commerce and economics
PDF
Functions in Economics Part-I.pdf
PDF
Application of Functions in Business and Economics
PPT
PPTX
Introduction to graphics programming in c
DOCX
PROGRAM FOR INSERTION SORTING IN ARRAY
DOCX
PDF
Basics of Computer graphics lab
PDF
C Graphics Functions
PDF
PDF
DOCX
Program uts
DOCX
Write a program to perform translation.
PDF
ZipCode_Distance_by_Agent_Home_Zip_2015091501
DOCX
analog clock C#
DOCX
Computer Science Practical Science C++ with SQL commands
DOCX
Write a program to perform translation
PPT
Lecture on graphics
Applications of calculus in commerce and economics
Functions in Economics Part-I.pdf
Application of Functions in Business and Economics
Introduction to graphics programming in c
PROGRAM FOR INSERTION SORTING IN ARRAY
Basics of Computer graphics lab
C Graphics Functions
Program uts
Write a program to perform translation.
ZipCode_Distance_by_Agent_Home_Zip_2015091501
analog clock C#
Computer Science Practical Science C++ with SQL commands
Write a program to perform translation
Lecture on graphics
Ad

Similar to Bakery algorithm (20)

PPTX
Bakery Algorithm
PPTX
15- Bakery-Algorithm.pptx
PPT
Bakery algorithm in operating system
PPT
Bakery algorithm
PPTX
Food Order in a Restaurant - Data Structure Project
PDF
Turing Lecture - The Computer Science of Concurrency - The Early Years : Notes
PPTX
Presentation on Bakery Algorithm
PPTX
Bakers and Philosophers
PPT
Os module 2 c
PPT
Os module 2 c
PDF
Process Synchronization
PPT
Ch7: Process Synchronization
PPT
Ch7 Process Synchronization galvin
PPTX
Module 2_Conditional Statements.pptx
PDF
Operating System-Ch6 process synchronization
PPT
Queue (1)(1).ppt
PPT
17. Trees and Graphs
KEY
Algorithms are Recipes
PPTX
Python Programming Essentials - M16 - Control Flow Statements and Loops
PPT
Operating System
Bakery Algorithm
15- Bakery-Algorithm.pptx
Bakery algorithm in operating system
Bakery algorithm
Food Order in a Restaurant - Data Structure Project
Turing Lecture - The Computer Science of Concurrency - The Early Years : Notes
Presentation on Bakery Algorithm
Bakers and Philosophers
Os module 2 c
Os module 2 c
Process Synchronization
Ch7: Process Synchronization
Ch7 Process Synchronization galvin
Module 2_Conditional Statements.pptx
Operating System-Ch6 process synchronization
Queue (1)(1).ppt
17. Trees and Graphs
Algorithms are Recipes
Python Programming Essentials - M16 - Control Flow Statements and Loops
Operating System
Ad

Recently uploaded (20)

PPTX
G5Q1W8 PPT SCIENCE.pptx 2025-2026 GRADE 5
PPTX
DRUG THERAPY FOR SHOCK gjjjgfhhhhh.pptx.
PPT
protein biochemistry.ppt for university classes
PPTX
ognitive-behavioral therapy, mindfulness-based approaches, coping skills trai...
PPTX
Taita Taveta Laboratory Technician Workshop Presentation.pptx
PPTX
Introduction to Fisheries Biotechnology_Lesson 1.pptx
PDF
Cosmic Outliers: Low-spin Halos Explain the Abundance, Compactness, and Redsh...
PPT
POSITIONING IN OPERATION THEATRE ROOM.ppt
PPTX
neck nodes and dissection types and lymph nodes levels
PDF
SEHH2274 Organic Chemistry Notes 1 Structure and Bonding.pdf
PDF
IFIT3 RNA-binding activity primores influenza A viruz infection and translati...
DOCX
Q1_LE_Mathematics 8_Lesson 5_Week 5.docx
PDF
CAPERS-LRD-z9:AGas-enshroudedLittleRedDotHostingaBroad-lineActive GalacticNuc...
PDF
ELS_Q1_Module-11_Formation-of-Rock-Layers_v2.pdf
PPTX
Protein & Amino Acid Structures Levels of protein structure (primary, seconda...
PDF
Mastering Bioreactors and Media Sterilization: A Complete Guide to Sterile Fe...
PDF
Unveiling a 36 billion solar mass black hole at the centre of the Cosmic Hors...
PPTX
Cell Membrane: Structure, Composition & Functions
PPTX
Derivatives of integument scales, beaks, horns,.pptx
PPTX
The KM-GBF monitoring framework – status & key messages.pptx
G5Q1W8 PPT SCIENCE.pptx 2025-2026 GRADE 5
DRUG THERAPY FOR SHOCK gjjjgfhhhhh.pptx.
protein biochemistry.ppt for university classes
ognitive-behavioral therapy, mindfulness-based approaches, coping skills trai...
Taita Taveta Laboratory Technician Workshop Presentation.pptx
Introduction to Fisheries Biotechnology_Lesson 1.pptx
Cosmic Outliers: Low-spin Halos Explain the Abundance, Compactness, and Redsh...
POSITIONING IN OPERATION THEATRE ROOM.ppt
neck nodes and dissection types and lymph nodes levels
SEHH2274 Organic Chemistry Notes 1 Structure and Bonding.pdf
IFIT3 RNA-binding activity primores influenza A viruz infection and translati...
Q1_LE_Mathematics 8_Lesson 5_Week 5.docx
CAPERS-LRD-z9:AGas-enshroudedLittleRedDotHostingaBroad-lineActive GalacticNuc...
ELS_Q1_Module-11_Formation-of-Rock-Layers_v2.pdf
Protein & Amino Acid Structures Levels of protein structure (primary, seconda...
Mastering Bioreactors and Media Sterilization: A Complete Guide to Sterile Fe...
Unveiling a 36 billion solar mass black hole at the centre of the Cosmic Hors...
Cell Membrane: Structure, Composition & Functions
Derivatives of integument scales, beaks, horns,.pptx
The KM-GBF monitoring framework – status & key messages.pptx

Bakery algorithm

  • 1. Bakery Algorithm • The idea is similar to a line at the bakery • A customer takes a number greater than numbers of other customers • Each of the threads gets a unique identifier
  • 2. Bakery Algorithm Thread i flag[i]=true; number[i] = max(number[0], …,number[n-1])+1; while ($ k!= i flag[k] && (number[i],i) > (number[k],k)) {}; critical section flag[i] = false;
  • 3. Bakery Algorithm flag[i]=true; number[i] = max(number[0], …,number[n-1])+1; while ($ k!= i flag[k] && (number[i],i) > (number[k],k)) {}; critical section flag[i] = false; Doorway
  • 4. Bakery Algorithm flag[i]=true; number[i] = max(number[0], …,number[n-1])+1; while ($ k!= i flag[k] && (number[i],i) > (number[k],k)) {}; critical section flag[i] = false; I’m interested
  • 5. Bakery Algorithm flag[i]=true; number[i] = max(number[0], …,number[n-1])+1; while ($ k!= i flag[k] && (number[i],i) > (number[k],k)) {}; critical section flag[i] = false; Take an number numbers are always increasing!
  • 6. Bakery Algorithm flag[i]=true; number[i] = max(number[0], …,number[n-1])+1; while ($ k!= i flag[k] && (number[i],i) > (number[k],k)) {}; critical section flag[i] = false; Someone is interested
  • 7. Bakery Algorithm flag[i]=true; number[i] = max(number[0], …,number[n-1])+1; while ($ k!= i flag[k] && (number[i],i) > (number[k],k)) {}; critical section flag[i] = false; There is someone with a lower number and identifier. pair (a,b) > (c,d) if a>c, or a=c and b>d (lexicographic order)
  • 8. Deadlock freedom • The bakery algorithm is deadlock free • Some waiting thread A has a unique least (number[A],A) pair, and that thread can enter the critical section
  • 9. FIFO • The bakery algorithm is first-come-first-served • If DA  DB then A’s number is earlier – writeA(number[A])  readB(number[A])  writeB(number[B])  readB(flag[A]) • So B is locked out while flag[A] is true flag[i]=true; number[i] = max(number[0], …,number[n-1])+1; while ($ k!= i flag[k] && (number[i],i) > (number[k],k)) {}; critical section flag[i] = false;