Uploaded by topohaw319

OS Lec 12 Bakery-Algorithm

advertisement
1
CSC351-Operating System
FALL 2023
2
Text Book

Operating system concepts by
Abraham Silberschatz, Galvin, Gagne,
9th edition
Lahore Garrison University
3
►Process
Preamble
(Past lesson
brief)
Lahore Garrison University
Synchronization
►PeterSon’s
Solution
►Algorithm
1
►Algorithm
2
►Algorithm
3
4
Agenda

Critical Section (Review)

Introduction to Bakery Algorithm

Bakery Algorithm Notations

Bakery Algorithm Data Structure
Critical Section (Review)

Critical section is a code segment that accesses shared variables and has to be executed an
atomic action.
 Only one process must be executing its critical section at a given time.
Do
{
Entry section
Critical section
Exit section
Remainder-section
}
While (true)
Critical Section Solution
(Review)
 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.

Bounded waiting:
There is a limit on the number of times that other process are allowed to enter
their critical section.
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.

I f processes Pi and Pj receive the same number, if i
< j, then Pi is served first; else Pj is served first.
Bakery Algorithm
Pi

Pj
The ticket numbering scheme always generates numbers in the increasing order
of enumeration; i.e., 1, 2, 3, 4, 5 ..
Bakery Algorithm
(Ticket # 25, p1)
(Ticket # 24, p2)
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 lowest ticket
number.
9
Bakery Algorithm (Cont.)
(Ticket # 24, p1)

(Ticket # 24, p2)
In the above diagram we can see that there are two processes p1 and p2.
 The process 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 Structure)

Data Structures
boolean choosing[n];
int number[n];

These data structures are initialized to false and 0, respectively

When pi is waiting to enter its critical section then number[i] will be non zero.
Bakery Algorithm
Notations

(ticket #, process id #)
(a,b) < (c,d) if a < c or
if a == c and b < d

max (a0,…, an-1) is a number, k, such that k  ai for i = 0, …, n–1
Bakery Algorithm

Structure of Pi
Before choosing a number, every
process will first set its choosing to
true and later will set it to false.
do {
choosing[i] = true;
number[i] = max(number[0],number[1],…,number [n – 1]) + 1;
choosing[i] = false;
for (j = 0; j < n; j++) {
This loop is going to compare the (no,
id) pair of Pi with (no, id) pair of all
while (choosing[j]) ;
other processes and finally selects
while ( (number[ j ] != 0) && ((number[j], j) < (number[i],
i))goes
) ; to the CS
which process
If Pj is having a number equal to 0, i.e. Pj is not
interested to enter its CS. So }break this while
loop, go back to for loop, increment j and
check next process
Critical Section
number[i] = 0;
Remainder section
} while (1);
If Pj is interested to go to its CS, then check its ordered
pair, with ordered pair of Pi. If Pj’s (no, id) pair is less than
Pi’s pair then Pi wait in this loop, else move up to for
loop, increment j and check next process.
After leaving the CS, Pi set its number
to 0, showing that it is now not
interested to enter its CS
Bakery Algorithm (Example)
Process
P0
P1
P2
P3
P4
Number
3
0
7
4
8
Bakery Algorithm (Example)
P0
P1
(3,0) < (3,0) (3,0) < (0,1)
P2
P3
P4
(3,0) < (7,2) (3,0) < (4,3) (3,0) < (8,4)
(0,1) < (3,0) (0,1) < (0,1) (0,1) < (7,2) (0,1) < (4,3) (0,1) < (8,4)
(7,2) < (3,0) (7,2) < (0,1)
(7,2) < (7,2) (7,2) < (4,3) (7,2) < (8,4)
(4,3) < (3,0) (4,3) < (0,1)
(4,3) < (7,2) (4,3) < (4,3) (4,3) < (8,4)
(8,4) < (3,0) (8,4) < (0,1)
(8,4) < (7,2) (8,4) < (4,3) (8,4) < (8,4)
(a, b) < (c, d)
If a < c
OR
If a == c AND b < d
Condition True = 1
Condition False = 0
P0
P0
P1
P2
P3
P4
(3,0) < (3,0)
(3,0) < (0,1)
(3,0) < (7,2)
(3,0) < (4,3)
(3,0) < (8,4)
(0,1) < (3,0)
(0,1) < (0,1)
(0,1) < (7,2)
(0,1) < (4,3)
(0,1) < (8,4)
(7,2) < (3,0)
(7,2) < (0,1)
(7,2) < (7,2)
(7,2) < (4,3)
(7,2) < (8,4)
(4,3) < (3,0)
(4,3) < (0,1)
(4,3) < (7,2)
(4,3) < (4,3)
(4,3) < (8,4)
(8,4) < (3,0)
(8,4) < (0,1)
(8,4) < (7,2)
(8,4) < (4,3)
(8,4) < (8,4)
P1
P2
P3
P4
P0
0
0
1
1
1
P1
1
0
1
1
1
P2
0
0
0
0
1
P3
0
0
1
0
1
P4
0
0
0
0
0
Check Column Wise,
If all entries are zero;
The process is complete,
Remove this process from both
Column and Row.
Note: It does not matter If row has
all zeros. Row is dependent on
Column.
P1 is complete;
After removing P1, see that P0 has
all entries as zero.
Hence,
P0 is complete;
After removing P0, see that P3 has
all entries as zero.
Hence,
P3 is complete;
After removing P3, see that P4 has
all entries as zero.
Hence,
P2 is complete.
After removing P2, see that P4 is
the only process which has zero
entry.
Hence, P4 is complete.
P0
P1
P2
P3
P4
P0
0
0
1
1
1
P1
1
0
1
1
1
P2
0
0
0
0
1
P3
0
0
1
0
1
P4
0
0
0
0
0
P0
P2
P3
P4
P0
0
1
1
1
P2
0
0
0
1
P3
0
1
0
1
P4
0
0
0
0
P2
P3
P4
P2
0
0
1
P3
1
0
1
P4
0
0
0
All processes have been
completed in order.
P2
P4
P2
0
1
P4
0
0
P4
P4
0
Bakery Algorithm (Example)
Bakery Algorithm
Process
Number
P0
3
P1
0
P2
7
P3
4
P4
P4
(3,0) < (8,4)
P0
P2
8
P3
(3,0) < (3,0)
(3,0) < (7,2)
(3,0) < (4,3)
Number[1] = Number[1] = Number[1] = Number[1] =
0
0
0
0
(7,2) < (3,0)
(7,2) < (7,2)
(7,2) < (4,3)
(7,2) < (8,4)
(4,3) < (3,0)
(4,3) < (7,2)
(4,3) < (4,3)
(4,3) < (8,4)
(8,4) < (3,0)
(8,4) < (7,2)
(8,4) < (4,3)
(8,4) < (8,4)
1
3
2
4
Bakery Algorithm

P1 not interested to get into its critical section  number[1] is
0

P2, P3, and P4 wait for P0

P0 gets into its CS, get out, and sets its number to 0

P3 get into its CS and P2 and P4 wait for it to get out of its CS

P2 gets into its CS and P4 waits for it to get out

P4 gets into its CS

Sequence of execution of processes:
<P0, P3, P2, P4>
Bakery Algorithm

Meets all three requirements:

Mutual Exclusion:
(number[j], j) < (number[i], i) cannot be true for both Pi and
Pj

Progress:
 Decision takes complete execution of the ‘for loop’ by
one process
 No process in its ‘Remainder Section’ (with its number
set to 0) participates in the decision making

Bounded-waiting:
At most one entry by each process (n-1 processes) and
then
a requesting process enters its critical section (FirstCome- First-Serve)
22
Summary

Critical Section Problem

Critical Section Solution

Bakery Algorithm Data Structure

Bakery Algorithm Example

Bakery Algorithm Results
23
Reference

To cover this topics , different reference material has
been used for consultation.

Operating systems concept by Abraham silberchatz
edition 9

Tutorialspoint.com

Google.com
Lahore Garrison University
Download