CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.

Slides:



Advertisements
Similar presentations
Chapter 6: Process Synchronization
Advertisements

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
Synchronization Principles Gordon College Stephen Brinton.
Process Synchronization CS 502 Spring 99 WPI MetroWest/Southboro Campus.
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Chapter 7: Process Synchronization
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
What we will cover… Process Synchronization Basic Concepts
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 6: Process Synchronization Dr. Mohamed Hefeeda.
Lecture 11 Chapter 6: Process Synchronization (cont)
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.
More Synchronisation Last time: bounded buffer, readers-writers, dining philosophers Today: sleeping barber, monitors.
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.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
Synchronization II: CPE Operating Systems
Chapter 6 Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Silberschatz and Galvin  Operating System Concepts Module 6: Process Synchronization Background The Critical-Section Problem Synchronization.
Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems of 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.
Chapter 6: Process 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.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
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. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition, Chapter 6: Synchronization.
CS307 Operating Systems Process Synchronization Fan Wu Department of Computer Science and Engineering Shanghai Jiao Tong University Spring 2012.
Chapter 6: Synchronization. 6.2Operating System Principles Module 6: Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization.
Copyright © 2006 by The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill Technology Education Lecture 6 Operating Systems.
Chapter 6: Process Synchronization. 6.2 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization.
7.1 Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization Hardware Semaphores Classical Problems of Synchronization.
CSE Operating System Principles Synchronization.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 22 Semaphores Classic.
1 Advanced Operating Systems - Fall 2009 Lecture 7 – February 2, 2009 Dan C. Marinescu Office: HEC 439 B. Office hours:
Chapter 71 Monitor for Reader/Writers  Synchronizes access if all processes follow.  If some don’t go through monitor and database is accessed directly,
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.
Deadlock and Starvation
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
Lecture 5 Process Synchronization Erick Pranata
Chapter 5: Process Synchronization
Deadlock and Starvation
Chapter 7: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6-7: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Process Synchronization
Chapter 5: Process Synchronization (Con’t)
Part II: Process Management Chapter 7 Process Synchronization
Lecture 25 Syed Mansoor Sarwar
Chapter 6: Process Synchronization
Chapter 5: Process Synchronization
Semaphore Originally called P() and V() wait (S) { while S <= 0
CMPT 300: Operating Systems I
Presentation transcript:

CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables

CSC 360 Instructor: Kui Wu Agenda 1.Review of Mutex 2.Semaphore 3.Examples 4.Monitor 5.Condition Variables 6.Dining Philosophers 7.Implementation of Monitors 8.Implementation of Condition Variables

CSC 360 Instructor: Kui Wu 2 1. Mutex (1) Mutual exclusion (mutex) only two states –unlocked: there is no thread in critical section –locked: there is one thread in critical section state change is atomic –if it is unlocked, it can be locked by at most one thread when entering the critical section –if it is locked, it can “only” be unlocked by the locking thread when leaving the critical section

CSC 360 Instructor: Kui Wu 3 Mutex: more Mutex procedures – create a mutex variable (initially unlocked) – (some threads) attempt to lock the mutex only one can lock the mutex – others may be blocked and waiting the one with the mutex – execute the critical section – unlock the mutex variable eventually – destroy the mutex variable 1. Mutex (2)

CSC 360 Instructor: Kui Wu 4 2. Semaphores (1) Semaphore API Semaphore S – integer variable –binary semaphore –counting semaphore two indivisible (atomic) operations –also known as P() and V() wait (S): while S<= 0 do no-op; S--; signal (S): S++; Q: busy-wait problem?

CSC 360 Instructor: Kui Wu 5 2. Using semaphores (2) Mutual exclusion binary semaphore shared data semaphore mutex; // initially mutex = 1 process Pi do { wait(mutex); /* critical section */ signal(mutex); /* remainder section */ } while (1); Resource access counting semaphore initially, the number of resource instances

CSC 360 Instructor: Kui Wu 6 2. Semaphore implementation (3) Semaphores without busy waiting block(): block the caller process wakeup(): wakeup another process wait(S): S.value--; if (S.value < 0) { add this process to S.L; block(); } signal(S): S.value++; if (S.value <= 0) { remove a process P from S.L; wakeup(P); }

CSC 360 Instructor: Kui Wu 7 2. More on using semaphores (4) Ordered execution initially, flag = 0; P1: …; do_me_first; signal (flag); P2: …; wait (flag); then_follow_on; Caution deadlock –wait (A); wait (B); …; signal (A); signal (B); –wait (B); wait (A); …; signal (B); signal (A); starvation

CSC 360 Instructor: Kui Wu 8 3. Examples (1): The producer- consumer problem With semaphore while (true) { // produce an item wait (empty); wait (mutex); // add the item to the buffer signal (mutex); signal (full); } while (true) { wait (full); wait (mutex); // remove an item signal (mutex); signal (empty); // consume the item }

CSC 360 Instructor: Kui Wu 9 3. Examples (2):The readers-writers problem First readers-writers problem no readers kept waiting unless writer is writing while (true) { wait (wrt) ; // writing is performed signal (wrt) ; } while (true) { wait (mutex) ; readcount ++ ; if (readcount == 1) wait (wrt) ; signal (mutex); // reading is performed wait (mutex) ; readcount - - ; if (readcount == 0) signal (wrt) ; signal (mutex) ; }

CSC 360 Instructor: Kui Wu Example (3): dining philosophers Shared data Initially all values are 1 semaphore chopstick[5]; Philosopher i: do { wait(chopstick[i]) wait(chopstick[(i+1) % 5]) … eat … signal(chopstick[i]); signal(chopstick[(i+1) % 5]); … think … } while (1); Q: any possible problems?

CSC 360 Instructor: Kui Wu CSc 360Overview Monitor A high-level abstraction (OO design) that provides a convenient and effective mechanism for process synchronization Only one process may be active within the monitor at a time monitor monitor-name { // shared variable declarations procedure P1 (…) { …. } … procedure Pn (…) {……} Initialization code ( ….) { … } … } Any problem?

CSC 360 Instructor: Kui Wu CSc 360Overview Condition variables No busy-waiting! condition variables Two operations on a condition variable x.wait () – a process that invokes the operation is suspended. x.signal () – resumes one of processes (if any) that invoked x.wait ()

CSC 360 Instructor: Kui Wu 13 Shared data semaphore chopstick[5];// initially 1 Using semaphores, for Philosopher i: do { wait(chopstick[i]) wait(chopstick[(i+1) % 5]) … eat … signal(chopstick[i]); signal(chopstick[(i+1) % 5]); … think … } while (1); Any problem? 6. Dining philosophers (D.P.) problem (1)

CSC 360 Instructor: Kui Wu D.P. problem (2): monitors monitor DP { enum { THINKING, HUNGRY, EATING} state [5] ; condition self [5]; void pickup (int i) { state[i] = HUNGRY; test(i); if (state[i] != EATING) self [i].wait; } void putdown (int i) { state[i] = THINKING; // test left and right neighbors test((i + 4) % 5); test((i + 1) % 5); }

CSC 360 Instructor: Kui Wu DP monitor (3): more void test (int i) { if ( (state[(i + 4) % 5] != EATING) && (state[i] == HUNGRY) && (state[(i + 1) % 5] != EATING) ) { state[i] = EATING ; self[i].signal () ; // no effect if not blocked } initialization_code() { for (int i = 0; i < 5; i++) state[i] = THINKING; } Any problem? Using monitors dp.pickup (i)... EAT... dp.putdown (i)

CSC 360 Instructor: Kui Wu Implementation of monitors Variables semaphore mutex; // for the monitor, initially = 1 semaphore next; // for suspended processes, initially = 0 int next-count = 0;// # of suspended processes Each procedure F will be replaced by wait(mutex); // wait for the access to the monitor … body of F; … if (next-count > 0)// whether there are suspended processes signal(next); else // free the monitor to other processes signal(mutex); Mutual exclusion within a monitor is ensured

CSC 360 Instructor: Kui Wu Implementing Condition Variables For each condition variable x, we have: semaphore x-sem; // initially = 0 int x-count = 0; The operation x.wait can be implemented as: x-count++; if (next-count > 0) signal(next); // wake up the first one of the suspended q else signal(mutex);// free the monitor wait(x-sem);// join the x queue x-count--; The operation x.signal can be implemented as: if (x-count > 0) {// no effect if x is not blocked next-count++; signal(x-sem);// wake up the first one of the x queue wait(next);// join the suspended queue next-count--; }