Operating Systems: Syed Mansoor Sarwar

You might also like

Download as pps, pdf, or txt
Download as pps, pdf, or txt
You are on page 1of 22

Operating

Systems
Lecture 21
Syed Mansoor Sarwar
Agenda for Today
 Review of previous lecture
 2-Process CS problem solution

 N-Process CS problem

 The Bakery Algorithm

 Hardware solutions

 Recap of lecture
September 14, 201 © Copyright Virtual Univers
9 ity of Pakistan
Review of Lecture 20
 2-Process Critical Section
Problem Solution
 n-Process Critical Section
Problem
 The Bakery Algorithm

September 14, 201 © Copyright Virtual Univers


9 ity 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;
September 14, 201 © Copyright Virtual Univers
9 ity 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
September 14, 201 © Copyright Virtual Univers
9 ity of Pakistan
Bakery Algorithm

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

September 14, 201 © Copyright Virtual Univers


9 ity of Pakistan
Bakery Algorithm
Process Number
P0 3
P1 0
P2 7
P3 4
P4 8
September 14, 201 © Copyright Virtual Univers
9 ity 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)
September 14, 201 © Copyright Virtual Univers
9 1 ity3of Pakistan 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
September 14, 201
get out of its CS
© Copyright Virtual Univers
9 ity of Pakistan
Bakery Algorithm
 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>
September 14, 201 © Copyright Virtual Univers
9 ity of Pakistan
Bakery Algorithm
 Meets all three requirements:
 Mutual Exclusion:
(number[j], j) < (number[i], i) cannot be
true for both Pi and Pj
 Bounded-waiting: At most one
entry by each process (n-1
processes) and then a requesting
process enters its critical section
(First-Come-First-Serve)
September 14, 201 © Copyright Virtual Univers
9 ity of Pakistan
Bakery Algorithm
 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 © Copyright Virtual Univers
September 14, 201
9 ity of Pakistan
Synchronization
Hardware
 Normally, access to a memory
location excludes other accesses to
that same location.
 Extension: designers have
proposed machine instructions that
perform two operations atomically
(indivisibly) on the same memory
location
September 14, 201
(e.g., reading and
© Copyright Virtual Univers
writing).
9 ity of Pakistan
Synchronization
Hardware
 The execution of such an instruction
is also mutually exclusive (even on
Multiprocessors).
 They can be used to provide mutual
exclusion but other mechanisms are
needed to satisfy the other two
requirements of a good solution to
the CS
September 14, 201problem.
© Copyright Virtual Univers
9 ity of Pakistan
Test-And-Set (TSL)
Instruction
 Test and modify a word atomically.

boolean TestAndSet(boolean &target)


{
boolean rv = target;
target = true;
return rv;
} September 14, 201 © Copyright Virtual Univers
9 ity of Pakistan
Solution with TSL
 Structure for Pi (‘lock’ is set to false)

do {
while ( TestAndSet(lock) ) ;
Critical Section
lock = false;
Remainder Section
9 }
September 14, 201 © Copyright Virtual Univers
ity of Pakistan
Solution with TSL
 Is the TSL-based solution good?
No

 Mutual Exclusion: Satisfied


 Progress: Satisfied
 Bounded Waiting: Not satisfied
September 14, 201 © Copyright Virtual Univers
9 ity of Pakistan
Swap Instruction
 Swaps two variables atomically
void swap (boolean &a, boolean &b)
{
boolean temp = a;
a = b;
b = temp;
} September 14, 201 © Copyright Virtual Univers
9 ity of Pakistan
Solution with Swap
 Structure for Pi
 ‘key’ is local and set to false
do {
key = true;
while (key == true) swap(lock,key);
Critical Section
lock = false;
Remainder Section
}9
September 14, 201 © Copyright Virtual Univers
ity of Pakistan
Solution with swap
 Is the swap-based solution
good?

No
 Mutual Exclusion: Satisfied
 Progress: Satisfied
September
Bounded 14, 201 Waiting:
© Not satisfied
Copyright Virtual Univers
9 ity of Pakistan
Recap of Lecture
 The Bakery algorithm
 Synchronization hardware

 The test-and-set and swap


instructions
 Solutions based on the TSL and
swap instructions
September 14, 201 © Copyright Virtual Univers
9 ity of Pakistan
Operating
Systems
Lecture 21
Syed Mansoor Sarwar

You might also like