Lecture 25 Syed Mansoor Sarwar

Slides:



Advertisements
Similar presentations
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Advertisements

Ch 7 B.
Chapter 6: Process Synchronization
Dr. Kalpakis CMSC 421, Operating Systems. Fall Process Synchronization.
02/27/2004CSCI 315 Operating Systems Design1 Process Synchronization Deadlock Notice: The slides for this lecture have been largely based on those accompanying.
Monitors A high-level abstraction that provides a convenient and effective mechanism for process synchronization Only one process may be active within.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
Silberschatz, Galvin and Gagne  Operating System Concepts Background Shared-memory solution to bounded-buffer problem allows at most n – 1 items.
Synchronization Principles Gordon College Stephen Brinton.
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
1 Last Time: Locks & Semaphores Implementing locks Test & Set Busy waiting Block waiting Semaphores Generalization of locks.
6/12/2015Page 1 Inter-Process Communication and Synchronization B. Ramamurthy.
Chapter 7: Process Synchronization
Dining Philosophers, Monitors, and Condition Variables
Process Synchronization
What we will cover… Process Synchronization Basic Concepts
02/25/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Monitors High-level synchronization construct that allows the safe sharing of an abstract data type among concurrent processes. monitor monitor-name {
1 CSC 539: Operating Systems Structure and Design Spring 2005 Process synchronization  critical section problem  synchronization hardware  semaphores.
Critical Problem Revisit. Critical Sections Mutual exclusion Only one process can be in the critical section at a time Without mutual exclusion, results.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 14: October 14, 2010 Instructor: Bhuvan Urgaonkar.
Synchronization II: CPE Operating Systems
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Silberschatz and Galvin  Operating System Concepts Module 6: Process Synchronization Background The Critical-Section Problem Synchronization.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Synchronization Background The Critical-Section.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 7 Process Synchronization Slide 1 Chapter 7 Process Synchronization.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 24 Critical Regions.
Fall 2000M.B. Ibáñez Lecture 08 High Level mechanisms for process synchronization Critical Regions Monitors.
Operating Systems Lecture Notes Synchronization Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Semaphores Ref: William Stallings G.Anuradha. Principle of a Semaphore Two or more processes can cooperate by means of simple signals, such that a process.
7.1 Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization.
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
崑山科技大學資管系 Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization.
Operating Systems Unit 4: – Dining Philosophers – Deadlock – Indefinite postponement Operating Systems.
Deadlock and Starvation
Chapter 7: Process Synchronization
Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer.
Chapter 5: Process Synchronization – Part 3
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Chapter 5: Process Synchronization
Deadlock and Starvation
Chapter 7: Process Synchronization
Chapter 6-7: Process Synchronization
Chapter 6: Process Synchronization
Chapter 5: Process Synchronization
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Chapter 6: Process Synchronization
Chapter 5: Process Synchronization (Con’t)
Part II: Process Management Chapter 7 Process Synchronization
Chapter 6: Process Synchronization
Semaphore Originally called P() and V() wait (S) { while S <= 0
Lecture 22 Syed Mansoor Sarwar
Module 7a: Classic Synchronization
CMPT 300: Operating Systems I
Chapter 7: Synchronization Examples
More IPC B.Ramamurthy 4/15/2019.
Lecture 26 Syed Mansoor Sarwar
Chapter 6 Process Synchronization
Presentation transcript:

Lecture 25 Syed Mansoor Sarwar Operating Systems Lecture 25 Syed Mansoor Sarwar

© Copyright Virtual University of Pakistan Agenda for Today Review of previous lecture Dining philosophers problem High-level synchronization constructs Critical region Monitor Recap of lecture 30 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Review of Lecture 24 Counting semaphores Classical synchronization problems Bounded buffer problem Readers and writers problem Dining philosophers problem 30 November 2018 © Copyright Virtual University of Pakistan

Dining Philosophers Problem Five philosophers who spend their lives just thinking and eating. Only five chopsticks are available to the philosophers 30 November 2018 © Copyright Virtual University of Pakistan

Dining Philosophers Problem Each philosopher thinks. When he becomes hungry, he sits down and picks up the two chopsticks that are closest to him and eats. After a philosopher finishes eating, he puts down the chopsticks and starts to think. 30 November 2018 © Copyright Virtual University of Pakistan

Dining-Philosophers Problem 30 November 2018 © Copyright Virtual University of Pakistan

Dining-Philosophers Problem Shared data : semaphore chopstick[5]; // Initialized to 1 30 November 2018 © Copyright Virtual University of Pakistan

Dining-Philosophers Problem Philosopher i do { wait(chopstick[i]) wait(chopstick[(i+1)% 5]) eat signal(chopstick[i]); signal(chopstick[(i+1)% 5]) think } while (1); 30 November 2018 © Copyright Virtual University of Pakistan

Possibility of Deadlock If all philosophers become hungry at the same time and pick up their left chopstick, a deadlock occurs. 30 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Possible Solutions Allow at most four philosophers to be sitting simultaneously at the table. Allow a philosopher to pick up his/her chopsticks only if both chopsticks are available (to do this she must pick them up in a critical section) 30 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Possible Solutions Use an asymmetric solution; that is, an odd philosopher picks up first her left chopstick, whereas an even philosopher picks up her right chopstick and then her left chopstick. 30 November 2018 © Copyright Virtual University of Pakistan

Possibility of Starvation Two philosophers who are fast eaters and fast thinkers, and lock the chopsticks before others every time. 30 November 2018 © Copyright Virtual University of Pakistan

High-level Synchronization Constructs Shift the responsibility of enforcing mutual exclusion from the programmer (where it resides when semaphores are used) to the compiler. 30 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Critical Regions A critical region is a section of code that is always executed under mutual exclusion. 30 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Critical Regions They consist of two parts: Variables that must be accessed under mutual exclusion. A new language statement that identifies a critical region in which the variables are accessed. 30 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan The region Statement A shared variable v of type T, is declared as: v: shared T; region v when B do S; Variable v is accessed only inside S when B is true and when S is being executed, no other process can access variable v. 30 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan The region Statement When a process tries to execute the region statement, the Boolean expression B is evaluated. If B is true, statement S is executed. If it is false, the process is delayed until B becomes true and no other process is in the region associated with v. 30 November 2018 © Copyright Virtual University of Pakistan

Bounded Buffer Example Shared data struct buffer { int pool[n]; int count, in, out; } 30 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Producer Process Producer process inserts nextp into the shared buffer region buffer when (count < n) { pool[in] = nextp; in:= (in+1) % n; count++; } 30 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Consumer Process Consumer process removes an item from the buffer and puts it in nextc region buffer when (count > 0) { nextc = pool[out]; out = (out+1) % n; count--; } 30 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan The region Statement What happens when two region statements are executed simultaneously? region v when B do S1; region v when B do S2; 30 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Monitors High-level synchronization construct that allows the safe sharing of an abstract data type among concurrent cooperating processes. Only one process at a time is active within a monitor. 30 November 2018 © Copyright Virtual University of Pakistan

Schematic View of a Monitor 30 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Monitors monitor monitor-name { shared variable declarations procedure P1 (…) { . . . } procedure P2 (…) { . . . } … initialization code } 30 November 2018 © Copyright Virtual University of Pakistan

Monitors with Condition Variables Additional synchronization constructs are needed to model some synchronization problems. They can be modeled with condition variables. condition x, y; 30 November 2018 © Copyright Virtual University of Pakistan

Monitors with Condition Variables Only two operations can be performed on condition variables, wait and signal. x.wait: Process invoking this operation is suspended until another process invokes x.signal. 30 November 2018 © Copyright Virtual University of Pakistan

Monitors with Condition Variables x.signal: Resumes exactly one suspended process. If no process is suspended, this is a null operation, i.e., the state of x is unaffected. 30 November 2018 © Copyright Virtual University of Pakistan

Monitor with Condition Variables 30 November 2018 © Copyright Virtual University of Pakistan

Dining Philosophers Example monitor dp { enum {thinking, hungry, eating} state[5]; condition self[5]; void pickup(int i) // Following slides void putdown(int i) // Following slides void test(int i) // Following slides void init() { for (int i = 0; i < 5; i++) state[i] = thinking; } 30 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Dining Philosophers void pickup(int i) { state[i] = hungry; test(i); if (state[i] != eating) self[i].wait(); } 30 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Dining Philosophers void putdown(int i) { state[i] = thinking; // test left and right // neighbors test((i+4) % 5); test((i+1) % 5); } 30 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Dining Philosophers void test(int i) { if ((state[(i+4)%5]!= eating) && (state[i] == hungry) && (state[(i+1)%5]!= eating)) { state[i] = eating; self[i].signal(); } 30 November 2018 © Copyright Virtual University of Pakistan

© Copyright Virtual University of Pakistan Recap of Lecture Dining philosophers problem High-level language constructs Critical region Monitor 30 November 2018 © Copyright Virtual University of Pakistan

Lecture 25 Syed Mansoor Sarwar Operating Systems Lecture 25 Syed Mansoor Sarwar