0% found this document useful (0 votes)
57 views20 pages

Operating Systems: Syed Mansoor Sarwar

Uploaded by

Ibrahim Choudary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views20 pages

Operating Systems: Syed Mansoor Sarwar

Uploaded by

Ibrahim Choudary
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 20

Operating

Systems
Lecture 20
Syed Mansoor Sarwar
Agenda for Today
 Review of previous lecture
 2-Process Critical Section
Problem (continued)
 n-Process Critical Section
Problem
 The Bakery Algorithm

 Recap of lecture
14 September 2019 © Copyright Virtual University of
Pakistan
Review of Lecture 19
 Process Synchronization
 The Critical Section Problem

 2-Process Critical Section


Problem Solutions

14 September 2019 © Copyright Virtual University of


Pakistan
Algorithm 3
 Combined shared variables of
algorithms 1 and 2.
 boolean flag[2]; // Set to false
 int turn=0;

14 September 2019 © Copyright Virtual University of


Pakistan
Algorithm 3
 Process Pi
do {
flag[i] = true;
turn = j;
while (flag[j] && turn == j) ;
critical section
flag[i] = false;
remainder section
} while (1);
14 September 2019 © Copyright Virtual University of
Pakistan
Algorithm 3
 Meets all three requirements:
Mutual Exclusion: ‘turn’ can
have one value at a given time
(0 or 1)
Bounded-waiting: At most
one entry by a process and
then the second process
enters into its CS
14 September 2019 © Copyright Virtual University of
Pakistan
Algorithm 3
 Progress: Exiting process sets
its ‘flag’ to false … comes back
quickly and set it to true again
… but sets turn to the number
of the other process

14 September 2019 © Copyright Virtual University of


Pakistan
n-Process Critical
Section Problem
 Consider a system of n
processes (P0, P1 ... Pn-1).
 Each process has a segment
of code called a critical section
in which the process may
change shared data.
14 September 2019 © Copyright Virtual University of
Pakistan
n-Process Critical
Section Problem
 When one process is executing
its critical section, no other
process is allowed to execute in
its critical section.
 The critical section problem is to
design a protocol to serialize
executions of critical sections.
14 September 2019 © Copyright Virtual University of
Pakistan
Bakery Algorithm
 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.
14 September 2019 © Copyright Virtual University of
Pakistan
Bakery Algorithm
 The ticket numbering
scheme always generates
numbers in the increasing
order of enumeration; i.e.,
1, 2, 3, 4, 5 ...

14 September 2019 © Copyright Virtual University of


Pakistan
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
14 September 2019 © Copyright Virtual University of
Pakistan
Bakery Algorithm
Data Structures
 boolean choosing[n];
 int number[n];
These data structures are
initialized to false and 0,
respectively
14 September 2019 © Copyright Virtual University of
Pakistan
Bakery Algorithm
Structure of Pi
do {
choosing[i] = true;
number[i] = max(number[0],
number[1], …,
number [n – 1]) + 1;
choosing[i] = false;
14 September 2019 © Copyright Virtual University of
Pakistan
Bakery Algorithm

for (j = 0; j < n; j++) {


while (choosing[j]) ;
while ( (number[j] != 0) &&
((number[j], j) < (number[i], i)) ) ;
}
Critical Section
14 September 2019 © Copyright Virtual University of
Pakistan
Bakery Algorithm

number[i] = 0;
remainder section
} while (1);

14 September 2019 © Copyright Virtual University of


Pakistan
Bakery Algorithm
Process Number
P0 3
P1 0
P2 7
P3 4
P4 8
14 September 2019 © Copyright Virtual University of
Pakistan
Bakery Algorithm

P0 P2 P3 P4
(3,0) < (3,0) (3,0) < (7,2) (3,0) < (4,3) (3,0) < (8,4)

Number[1] = 0 Number[1] = 0 Number[1] = 0 Number[1] = 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)
14 September 2019 © Copyright Virtual University of
Pakistan
Recap of Lecture
 n-Process Critical Section
Problem
 The Bakery Algorithm

14 September 2019 © Copyright Virtual University of


Pakistan
Operating
Systems
Lecture 20
Syed Mansoor Sarwar

You might also like