SlideShare a Scribd company logo
1 of 9
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

What's hot

Applications of calculus in commerce and economics
Applications of calculus in commerce and economicsApplications of calculus in commerce and economics
Applications of calculus in commerce and economicssumanmathews
 
Functions in Economics Part-I.pdf
Functions in Economics Part-I.pdfFunctions in Economics Part-I.pdf
Functions in Economics Part-I.pdfDr. Subhash Unhale
 
Application of Functions in Business and Economics
Application of Functions in Business and EconomicsApplication of Functions in Business and Economics
Application of Functions in Business and EconomicsNorth South University
 
PROGRAM FOR INSERTION SORTING IN ARRAY
PROGRAM FOR INSERTION SORTING IN ARRAY PROGRAM FOR INSERTION SORTING IN ARRAY
PROGRAM FOR INSERTION SORTING IN ARRAY daemian2
 
Basics of Computer graphics lab
Basics of Computer graphics labBasics of Computer graphics lab
Basics of Computer graphics labPriya Goyal
 
C Graphics Functions
C Graphics FunctionsC Graphics Functions
C Graphics FunctionsSHAKOOR AB
 
Write a program to perform translation.
 Write a program to perform translation. Write a program to perform translation.
Write a program to perform translation.Shobhit Saxena
 
ZipCode_Distance_by_Agent_Home_Zip_2015091501
ZipCode_Distance_by_Agent_Home_Zip_2015091501ZipCode_Distance_by_Agent_Home_Zip_2015091501
ZipCode_Distance_by_Agent_Home_Zip_2015091501Robert Davis
 
analog clock C#
analog clock C#analog clock C#
analog clock C#omeed
 
Computer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commandsComputer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commandsVishvjeet Yadav
 
Write a program to perform translation
Write a program to perform translationWrite a program to perform translation
Write a program to perform translationShobhit Saxena
 
Lecture on graphics
Lecture on graphicsLecture on graphics
Lecture on graphicsRafi_Dar
 

What's hot (18)

Applications of calculus in commerce and economics
Applications of calculus in commerce and economicsApplications of calculus in commerce and economics
Applications of calculus in commerce and economics
 
Functions in Economics Part-I.pdf
Functions in Economics Part-I.pdfFunctions in Economics Part-I.pdf
Functions in Economics Part-I.pdf
 
Application of Functions in Business and Economics
Application of Functions in Business and EconomicsApplication of Functions in Business and Economics
Application of Functions in Business and Economics
 
test
testtest
test
 
Introduction to graphics programming in c
Introduction to graphics programming in cIntroduction to graphics programming in c
Introduction to graphics programming in c
 
PROGRAM FOR INSERTION SORTING IN ARRAY
PROGRAM FOR INSERTION SORTING IN ARRAY PROGRAM FOR INSERTION SORTING IN ARRAY
PROGRAM FOR INSERTION SORTING IN ARRAY
 
Arrays
ArraysArrays
Arrays
 
Basics of Computer graphics lab
Basics of Computer graphics labBasics of Computer graphics lab
Basics of Computer graphics lab
 
C Graphics Functions
C Graphics FunctionsC Graphics Functions
C Graphics Functions
 
Ques 8
Ques 8Ques 8
Ques 8
 
Ques 8
Ques 8Ques 8
Ques 8
 
Program uts
Program utsProgram uts
Program uts
 
Write a program to perform translation.
 Write a program to perform translation. Write a program to perform translation.
Write a program to perform translation.
 
ZipCode_Distance_by_Agent_Home_Zip_2015091501
ZipCode_Distance_by_Agent_Home_Zip_2015091501ZipCode_Distance_by_Agent_Home_Zip_2015091501
ZipCode_Distance_by_Agent_Home_Zip_2015091501
 
analog clock C#
analog clock C#analog clock C#
analog clock C#
 
Computer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commandsComputer Science Practical Science C++ with SQL commands
Computer Science Practical Science C++ with SQL commands
 
Write a program to perform translation
Write a program to perform translationWrite a program to perform translation
Write a program to perform translation
 
Lecture on graphics
Lecture on graphicsLecture on graphics
Lecture on graphics
 

Similar to Bakery algorithm

2 BytesC++ course_2014_c4_ arrays
2 BytesC++ course_2014_c4_ arrays2 BytesC++ course_2014_c4_ arrays
2 BytesC++ course_2014_c4_ arrayskinan keshkeh
 
Go vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoFGo vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoFTimur Safin
 
Project 5Programming and Algorithms IICSCI 211, Spring 2.docx
Project 5Programming and Algorithms IICSCI 211, Spring 2.docxProject 5Programming and Algorithms IICSCI 211, Spring 2.docx
Project 5Programming and Algorithms IICSCI 211, Spring 2.docxbriancrawford30935
 
Machine Learning: je m'y mets demain!
Machine Learning: je m'y mets demain!Machine Learning: je m'y mets demain!
Machine Learning: je m'y mets demain!Louis Dorard
 
#includeiostream.h#includestdlib.hstruct customers{.docx
#includeiostream.h#includestdlib.hstruct customers{.docx#includeiostream.h#includestdlib.hstruct customers{.docx
#includeiostream.h#includestdlib.hstruct customers{.docxkatherncarlyle
 
Below I have 2 microservices that compile very well- I need help inte.pdf
Below I have 2 microservices that compile very well-  I need help inte.pdfBelow I have 2 microservices that compile very well-  I need help inte.pdf
Below I have 2 microservices that compile very well- I need help inte.pdfrdzire2014
 
MongoDB World 2019: Just-in-time Validation with JSON Schema
MongoDB World 2019: Just-in-time Validation with JSON SchemaMongoDB World 2019: Just-in-time Validation with JSON Schema
MongoDB World 2019: Just-in-time Validation with JSON SchemaMongoDB
 
In memory OLAP engine
In memory OLAP engineIn memory OLAP engine
In memory OLAP engineWO Community
 

Similar to Bakery algorithm (13)

Arrays matrix 2020 ab
Arrays matrix 2020 abArrays matrix 2020 ab
Arrays matrix 2020 ab
 
2 BytesC++ course_2014_c4_ arrays
2 BytesC++ course_2014_c4_ arrays2 BytesC++ course_2014_c4_ arrays
2 BytesC++ course_2014_c4_ arrays
 
Go vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoFGo vs C++ - CppRussia 2019 Piter BoF
Go vs C++ - CppRussia 2019 Piter BoF
 
Arrays in c++
Arrays in c++Arrays in c++
Arrays in c++
 
Project 5Programming and Algorithms IICSCI 211, Spring 2.docx
Project 5Programming and Algorithms IICSCI 211, Spring 2.docxProject 5Programming and Algorithms IICSCI 211, Spring 2.docx
Project 5Programming and Algorithms IICSCI 211, Spring 2.docx
 
Machine Learning: je m'y mets demain!
Machine Learning: je m'y mets demain!Machine Learning: je m'y mets demain!
Machine Learning: je m'y mets demain!
 
#includeiostream.h#includestdlib.hstruct customers{.docx
#includeiostream.h#includestdlib.hstruct customers{.docx#includeiostream.h#includestdlib.hstruct customers{.docx
#includeiostream.h#includestdlib.hstruct customers{.docx
 
Array-part1
Array-part1Array-part1
Array-part1
 
Below I have 2 microservices that compile very well- I need help inte.pdf
Below I have 2 microservices that compile very well-  I need help inte.pdfBelow I have 2 microservices that compile very well-  I need help inte.pdf
Below I have 2 microservices that compile very well- I need help inte.pdf
 
MongoDB World 2019: Just-in-time Validation with JSON Schema
MongoDB World 2019: Just-in-time Validation with JSON SchemaMongoDB World 2019: Just-in-time Validation with JSON Schema
MongoDB World 2019: Just-in-time Validation with JSON Schema
 
Functional DDD
Functional DDDFunctional DDD
Functional DDD
 
In memory OLAP engine
In memory OLAP engineIn memory OLAP engine
In memory OLAP engine
 
Swift Study #2
Swift Study #2Swift Study #2
Swift Study #2
 

Recently uploaded

Vip profile Call Girls In Lonavala 9748763073 For Genuine Sex Service At Just...
Vip profile Call Girls In Lonavala 9748763073 For Genuine Sex Service At Just...Vip profile Call Girls In Lonavala 9748763073 For Genuine Sex Service At Just...
Vip profile Call Girls In Lonavala 9748763073 For Genuine Sex Service At Just...Monika Rani
 
High Class Escorts in Hyderabad ₹7.5k Pick Up & Drop With Cash Payment 969456...
High Class Escorts in Hyderabad ₹7.5k Pick Up & Drop With Cash Payment 969456...High Class Escorts in Hyderabad ₹7.5k Pick Up & Drop With Cash Payment 969456...
High Class Escorts in Hyderabad ₹7.5k Pick Up & Drop With Cash Payment 969456...chandars293
 
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune WaterworldsBiogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune WaterworldsSérgio Sacani
 
SCIENCE-4-QUARTER4-WEEK-4-PPT-1 (1).pptx
SCIENCE-4-QUARTER4-WEEK-4-PPT-1 (1).pptxSCIENCE-4-QUARTER4-WEEK-4-PPT-1 (1).pptx
SCIENCE-4-QUARTER4-WEEK-4-PPT-1 (1).pptxRizalinePalanog2
 
GBSN - Biochemistry (Unit 1)
GBSN - Biochemistry (Unit 1)GBSN - Biochemistry (Unit 1)
GBSN - Biochemistry (Unit 1)Areesha Ahmad
 
SAMASTIPUR CALL GIRL 7857803690 LOW PRICE ESCORT SERVICE
SAMASTIPUR CALL GIRL 7857803690  LOW PRICE  ESCORT SERVICESAMASTIPUR CALL GIRL 7857803690  LOW PRICE  ESCORT SERVICE
SAMASTIPUR CALL GIRL 7857803690 LOW PRICE ESCORT SERVICEayushi9330
 
Connaught Place, Delhi Call girls :8448380779 Model Escorts | 100% verified
Connaught Place, Delhi Call girls :8448380779 Model Escorts | 100% verifiedConnaught Place, Delhi Call girls :8448380779 Model Escorts | 100% verified
Connaught Place, Delhi Call girls :8448380779 Model Escorts | 100% verifiedDelhi Call girls
 
GBSN - Microbiology (Unit 3)
GBSN - Microbiology (Unit 3)GBSN - Microbiology (Unit 3)
GBSN - Microbiology (Unit 3)Areesha Ahmad
 
Chemical Tests; flame test, positive and negative ions test Edexcel Internati...
Chemical Tests; flame test, positive and negative ions test Edexcel Internati...Chemical Tests; flame test, positive and negative ions test Edexcel Internati...
Chemical Tests; flame test, positive and negative ions test Edexcel Internati...ssuser79fe74
 
Botany 4th semester file By Sumit Kumar yadav.pdf
Botany 4th semester file By Sumit Kumar yadav.pdfBotany 4th semester file By Sumit Kumar yadav.pdf
Botany 4th semester file By Sumit Kumar yadav.pdfSumit Kumar yadav
 
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 bAsymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 bSérgio Sacani
 
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...ssifa0344
 
Forensic Biology & Its biological significance.pdf
Forensic Biology & Its biological significance.pdfForensic Biology & Its biological significance.pdf
Forensic Biology & Its biological significance.pdfrohankumarsinghrore1
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Sérgio Sacani
 
Justdial Call Girls In Indirapuram, Ghaziabad, 8800357707 Escorts Service
Justdial Call Girls In Indirapuram, Ghaziabad, 8800357707 Escorts ServiceJustdial Call Girls In Indirapuram, Ghaziabad, 8800357707 Escorts Service
Justdial Call Girls In Indirapuram, Ghaziabad, 8800357707 Escorts Servicemonikaservice1
 
Botany 4th semester series (krishna).pdf
Botany 4th semester series (krishna).pdfBotany 4th semester series (krishna).pdf
Botany 4th semester series (krishna).pdfSumit Kumar yadav
 
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Lokesh Kothari
 
Pulmonary drug delivery system M.pharm -2nd sem P'ceutics
Pulmonary drug delivery system M.pharm -2nd sem P'ceuticsPulmonary drug delivery system M.pharm -2nd sem P'ceutics
Pulmonary drug delivery system M.pharm -2nd sem P'ceuticssakshisoni2385
 

Recently uploaded (20)

Vip profile Call Girls In Lonavala 9748763073 For Genuine Sex Service At Just...
Vip profile Call Girls In Lonavala 9748763073 For Genuine Sex Service At Just...Vip profile Call Girls In Lonavala 9748763073 For Genuine Sex Service At Just...
Vip profile Call Girls In Lonavala 9748763073 For Genuine Sex Service At Just...
 
High Class Escorts in Hyderabad ₹7.5k Pick Up & Drop With Cash Payment 969456...
High Class Escorts in Hyderabad ₹7.5k Pick Up & Drop With Cash Payment 969456...High Class Escorts in Hyderabad ₹7.5k Pick Up & Drop With Cash Payment 969456...
High Class Escorts in Hyderabad ₹7.5k Pick Up & Drop With Cash Payment 969456...
 
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune WaterworldsBiogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
 
SCIENCE-4-QUARTER4-WEEK-4-PPT-1 (1).pptx
SCIENCE-4-QUARTER4-WEEK-4-PPT-1 (1).pptxSCIENCE-4-QUARTER4-WEEK-4-PPT-1 (1).pptx
SCIENCE-4-QUARTER4-WEEK-4-PPT-1 (1).pptx
 
GBSN - Biochemistry (Unit 1)
GBSN - Biochemistry (Unit 1)GBSN - Biochemistry (Unit 1)
GBSN - Biochemistry (Unit 1)
 
SAMASTIPUR CALL GIRL 7857803690 LOW PRICE ESCORT SERVICE
SAMASTIPUR CALL GIRL 7857803690  LOW PRICE  ESCORT SERVICESAMASTIPUR CALL GIRL 7857803690  LOW PRICE  ESCORT SERVICE
SAMASTIPUR CALL GIRL 7857803690 LOW PRICE ESCORT SERVICE
 
Connaught Place, Delhi Call girls :8448380779 Model Escorts | 100% verified
Connaught Place, Delhi Call girls :8448380779 Model Escorts | 100% verifiedConnaught Place, Delhi Call girls :8448380779 Model Escorts | 100% verified
Connaught Place, Delhi Call girls :8448380779 Model Escorts | 100% verified
 
Site Acceptance Test .
Site Acceptance Test                    .Site Acceptance Test                    .
Site Acceptance Test .
 
GBSN - Microbiology (Unit 3)
GBSN - Microbiology (Unit 3)GBSN - Microbiology (Unit 3)
GBSN - Microbiology (Unit 3)
 
Chemical Tests; flame test, positive and negative ions test Edexcel Internati...
Chemical Tests; flame test, positive and negative ions test Edexcel Internati...Chemical Tests; flame test, positive and negative ions test Edexcel Internati...
Chemical Tests; flame test, positive and negative ions test Edexcel Internati...
 
Botany 4th semester file By Sumit Kumar yadav.pdf
Botany 4th semester file By Sumit Kumar yadav.pdfBotany 4th semester file By Sumit Kumar yadav.pdf
Botany 4th semester file By Sumit Kumar yadav.pdf
 
Clean In Place(CIP).pptx .
Clean In Place(CIP).pptx                 .Clean In Place(CIP).pptx                 .
Clean In Place(CIP).pptx .
 
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 bAsymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
 
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
TEST BANK For Radiologic Science for Technologists, 12th Edition by Stewart C...
 
Forensic Biology & Its biological significance.pdf
Forensic Biology & Its biological significance.pdfForensic Biology & Its biological significance.pdf
Forensic Biology & Its biological significance.pdf
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
 
Justdial Call Girls In Indirapuram, Ghaziabad, 8800357707 Escorts Service
Justdial Call Girls In Indirapuram, Ghaziabad, 8800357707 Escorts ServiceJustdial Call Girls In Indirapuram, Ghaziabad, 8800357707 Escorts Service
Justdial Call Girls In Indirapuram, Ghaziabad, 8800357707 Escorts Service
 
Botany 4th semester series (krishna).pdf
Botany 4th semester series (krishna).pdfBotany 4th semester series (krishna).pdf
Botany 4th semester series (krishna).pdf
 
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
 
Pulmonary drug delivery system M.pharm -2nd sem P'ceutics
Pulmonary drug delivery system M.pharm -2nd sem P'ceuticsPulmonary drug delivery system M.pharm -2nd sem P'ceutics
Pulmonary drug delivery system M.pharm -2nd sem P'ceutics
 

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;