Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 21 Syed Mansoor Sarwar

Similar presentations


Presentation on theme: "Lecture 21 Syed Mansoor Sarwar"— Presentation transcript:

1 Lecture 21 Syed Mansoor Sarwar
Operating Systems Lecture 21 Syed Mansoor Sarwar

2 © Copyright Virtual University of Pakistan
Agenda for Today Review of previous lecture 2-Process CS problem solution N-Process CS problem The Bakery Algorithm Hardware solutions Recap of lecture 8 April 2019 © Copyright Virtual University of Pakistan

3 © Copyright Virtual University of Pakistan
Review of Lecture 20 2-Process Critical Section Problem Solution n-Process Critical Section Problem The Bakery Algorithm 8 April 2019 © Copyright Virtual University of Pakistan

4 © 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; 8 April 2019 © Copyright Virtual University of Pakistan

5 © Copyright Virtual University of Pakistan
Bakery Algorithm Critical Section for (j = 0; j < n; j++) { while (choosing[j]) ; while ( (number[j] != 0) && ((number[j], j) < (number[i], i)) ) ; } 8 April 2019 © Copyright Virtual University of Pakistan

6 © Copyright Virtual University of Pakistan
Bakery Algorithm remainder section } while (1); number[i] = 0; 8 April 2019 © Copyright Virtual University of Pakistan

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

8 © 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 (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) 8 April 2019 © Copyright Virtual University of Pakistan 1 3 2 4

9 © Copyright Virtual University of Pakistan
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 8 April 2019 © Copyright Virtual University of Pakistan

10 © Copyright Virtual University 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> 8 April 2019 © Copyright Virtual University of Pakistan

11 © Copyright Virtual University 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) 8 April 2019 © Copyright Virtual University of Pakistan

12 © Copyright Virtual University 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 8 April 2019 © Copyright Virtual University of Pakistan

13 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 (e.g., reading and writing). 8 April 2019 © Copyright Virtual University of Pakistan

14 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 problem. 8 April 2019 © Copyright Virtual University of Pakistan

15 Test-And-Set (TSL) Instruction
Test and modify a word atomically. boolean TestAndSet(boolean &target) { boolean rv = target; target = true; return rv; } 8 April 2019 © Copyright Virtual University of Pakistan

16 © Copyright Virtual University of Pakistan
Solution with TSL Structure for Pi (‘lock’ is set to false) do { while ( TestAndSet(lock) ) ; Critical Section lock = false; Remainder Section 8 April 2019 } © Copyright Virtual University of Pakistan

17 © Copyright Virtual University of Pakistan
Solution with TSL Is the TSL-based solution good? No Mutual Exclusion: Satisfied Progress: Satisfied Bounded Waiting: Not satisfied 8 April 2019 © Copyright Virtual University of Pakistan

18 © Copyright Virtual University of Pakistan
Swap Instruction Swaps two variables atomically void swap (boolean &a, boolean &b) { boolean temp = a; a = b; b = temp; } 8 April 2019 © Copyright Virtual University of Pakistan

19 © Copyright Virtual University 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 } 8 April 2019 © Copyright Virtual University of Pakistan

20 © Copyright Virtual University of Pakistan
Solution with swap Is the swap-based solution good? No Mutual Exclusion: Satisfied Progress: Satisfied Bounded Waiting: Not satisfied 8 April 2019 © Copyright Virtual University of Pakistan

21 © Copyright Virtual University 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 8 April 2019 © Copyright Virtual University of Pakistan

22 Lecture 21 Syed Mansoor Sarwar
Operating Systems Lecture 21 Syed Mansoor Sarwar


Download ppt "Lecture 21 Syed Mansoor Sarwar"

Similar presentations


Ads by Google