Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

Constants:

______________________________________________
• Critical Section
• Hardware Requirements for Bakery Algorithm
• Software Requirements for Bakery Algorithm
• Introduction to Bakery Algorithm
• Bakery Algorithm Notations
• Bakery Algorithm Data Structures
Critical Section

• Critical section is a code segment that access


shared variables and has to be executed an atomic
action.
• Only one process must be executing its critical
section at a given time
Critical section Example
___________________________________________________

Do
{
Entry section
Critical section
Exit section
Remainder section
}
While(true)
Critical Section Solution Critriea
__________________________________________

• Mutual Exclusion: Only one process should execute in its


critical section.
• Progress: If no process is executing in its critical section
and some process wish to enter the critical section. Then
only those processes that are not executing in its remainder
section can participate.
Critical Section Solutions Critriea:
___________________________________________________

• Bounded waiting :  A bound must exist on the number


of times that other processes are allowed to enter their
critical sections after a process has made a request to
enter its critical section and before that request is
granted.
Introduction to Bakery Algorithm:
___________________________________________________

• What is Bakery Algorithm?


• Devised by “Leslie Lamport”
Before entering its critical section , process receives a ticket
number. Holder of the smallest ticket number enters the critical
section
If processes Pi and Pj receive the same number, If i < j, then Pi is
served first ; else Pj is served first.
Bakery Algorithm
_________________________________________

The ticket numbering scheme always generate numbers in


the increasing order of enumeration i.e., 1,2,3,4,5,…..
Bakery Algorithm
_________________________________________

• Notations (ticket#, process id#)


(a,b) < (c,d) if a < c or if a==c and b < d
Max (,………) is a number, k , such that k > for i =0,
….., n-1
Bakery Algorithm
_____________________________________

In the above diagram we can see that there are two processes p1
and p2. the process p2 will go to the critical section because it has
loweset ticket number
Bakery Algorithm
_________________________________________

In the above diagram we can see that there are two processes p1 and
p2 the processs p1&p2 have same ticket number. Then process ID
will be checked. P1 process has lowest ID and will go to critical
section First.
Bakery Algorithm
_____________________________________

Data structures
• Boolean choosing[n];
• Int number[n]
• These data structures are initialized to false and 0, respectively
Bakery Algorithm
_________________________________________

Structure of
do {
choosing[i] =true;
number[i] = max(number[0],number[1],……, number[n-1])
+ 1;
choosing[i] = false;
Bakery Algorithm
_________________________________________

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);
Bakery Algorithm
_____________________________________

• Working example:Let, five process p1, p2, p3,


p4,p5 are waiting to enter the critical section whereas
the ticket number has been assigned to them as
p1=3,p2=0,p3=1,p4=2,p5=3. Define an order in which
these processes will enter in critical section?
Bakery Algorithm
_________________________________________

• Solution:

You might also like