Classical Synchronization Problems

Slides:



Advertisements
Similar presentations
Chapter 6: Process Synchronization
Advertisements

Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
COSC 3407: Operating Systems Lecture 8: Semaphores, Monitors and Condition Variables.
Classic Synchronization Problems
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
Synchronization Principles Gordon College Stephen Brinton.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Classical Problems of Concurrency
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Last Time: Locks & Semaphores Implementing locks Test & Set Busy waiting Block waiting Semaphores Generalization of locks.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
1 Semaphores Special variable called a semaphore is used for signaling If a process is waiting for a signal, it is suspended until that signal is sent.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 6: Process Synchronization Dr. Mohamed Hefeeda.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Monitor  Giving credit where it is due:  The lecture notes are borrowed from Dr. I-Ling Yen at University of Texas at Dallas  I have modified them and.
CS4231 Parallel and Distributed Algorithms AY 2006/2007 Semester 2 Lecture 2 (19/01/2006) Instructor: Haifeng YU.
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.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 23 Classic Synchronization.
1 Processes Chapter Processes 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
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.
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
1 Process Synchronization – Outline Why do processes need synchronization ? What is the critical-section problem ? Describe solutions to the critical-section.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Synchronization Background The Critical-Section.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
OPERATING SYSTEMS DESIGN AND IMPLEMENTATION Third Edition ANDREW S. TANENBAUM ALBERT S. WOODHULL Yan hao (Wilson) Wu University of the Western.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
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.
CS 241 Section Week #7 (10/22/09). Topics This Section  Midterm Statistics  MP5 Forward  Classical Synchronization Problems  Problems.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Review: Monitor Semantics If P does X.wait() and later Q does X.signal(): –Hoare Semantics: Q blocks yielding the monitor immediately to P –Mesa Semantics:
CSC 360 Instructor: Kui Wu More on Process Synchronization Semaphore, Monitor, Condition Variables.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization.
ITEC 502 컴퓨터 시스템 및 실습 Chapter 3-3: Process Synchronization Mi-Jung Choi DPNM Lab. Dept. of CSE, POSTECH.
CS4315A. Berrached:CMS:UHD1 Process Synchronization Chapter 8.
1 Advanced Operating Systems - Fall 2009 Lecture 7 – February 2, 2009 Dan C. Marinescu Office: HEC 439 B. Office hours:
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
INTERPROCESS COMMUNICATION Inter process communication (IPC) is a capability supported by operating system that allows one process to communicate with.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Deadlock and Starvation
Semaphore Synchronization tool that provides more sophisticated ways (than Mutex locks) for process to synchronize their activities. Semaphore S – integer.
Auburn University COMP 3500 Introduction to Operating Systems Synchronization: Part 4 Classical Synchronization Problems.
Deadlock and Starvation
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Concurrency: Mutual Exclusion and Synchronization
Chapter 5: Process Synchronization (Con’t)
Lecture 25 Syed Mansoor Sarwar
Process Synchronization
Module 7a: Classic Synchronization
Chapter 7: Synchronization Examples
Chapter 6 Synchronization Principles
Chapter 7: Synchronization Examples
Presentation transcript:

Classical Synchronization Problems 1. The producer/consumer problem (the bounded buffer problem) 2. The dining philosophers 3. The reader/writer problem

The Bounded Buffer Problem head tail A circular buffer of limited capacity. Queue access: Add to tail, remove from head Models consumer/producer interactions n : number of slots, nitems : number of items

Why Important? It occurs in many situations in real systems: Network interfaces I/O controllers Message-passing others

Solution using Critical Regions Producer: region v when nitems < n do buf [tail++ % n] = item; nitems++; Consumer region v when nitems > 0 do item = buf [head++ % n]; nitems--; return item;

Solution using Monitors Monitor BoundedBuffer char buf [n]; int nitems; int tail, head; condition empty, full; // initialization nitems = tail = head = 0;

Solution with Monitors (cont’d) Produce(item) if(nitems == n) full.wait; buf [tail++ % n]=item; nitems++; empty.signal; Consume(item) if(nitems == 0) empty.wait; item = buf [head++ % n]; nitems--; full.signal;

Dining Philosophers

Definition A philosopher alternates between thinking: eating Gives up the two chopsticks on right & left eating Picks up the two chopsticks on right & left Picks up chopsticks one at a time If chopstick is in use, must wait until neighbor thinks and then picks it up

Why Important? It models competition for resources between threads or processes Key requirement: Philosophers should not starve!!!

A Solution Using Semaphore Semaphore Chopstick[5]; // initialized to 1 while(1) { P(Chopstick[i]); P(Chopstick[(i+1) % 5]); eat V(Chopstick[i]); V(Chopstick[(i+1) % 5]); think }

Solution Using Semaphores (cont’d) Guarantees that no two neighbors will be eating simultaneously But, consider the following interleaving: Philosopher 1 picks chopstick 1 Philosopher 2 picks chopstick 2 Philosopher 3 picks chopstick 3 Philosopher 4 picks chopstick 4 Philosopher 5 picks chopstick 5

Solution Using Critical Regions enum {Eating, Thinking} state[5]; for(i = 0; i < 5; i++) state[i] = Thinking; // Entry(i): region v when state[(i + 1) % 5] == Thinking && state[(i - 1) % 5] == Thinking do state[i] = Eating;

Solution Using Critical Regions // Exit(i): region v when true do state[i] = Thinking; Exclusion is guaranteed & no starvation But: Critical regions are difficult to implement. Exercise: Write a starvation-free solution using monitors

The Reader/Writer Problem Access to data in multithreaded applications: One or more threads can read a data record Only one thread can write Why important? Transaction systems File systems

Two Types of Locks on Same Item Shared: for readers Exclusive: for writers Rules: When the exclusive lock is held, no one else can acquire access in any form When the shared lock is held, others can acquire the shared lock, but no one can acquire the exclusive lock

Two Important Flavors Reader preferred: Writer preferred: If a new reader comes in & no one is currently writing, it gets through even if some writer is waiting to get access Writer preferred: If a new writer comes in, it gets through as soon as possible

Example 1 r1 r2 r3 w4 r5 w6 r7 (lock requests by threads 1 -- 7) Reader preferred: r1 r2 r3: go ahead w4: wait r5: go ahead w6: wait r7: go ahead r1 r2 r7 r3 r5 w4, w6

Example 2 r1 r2 r3 w4 r5 w6 r7 (lock requests by threads 1 -- 7) Writer preferred: r1 r2 r3: go ahead w4: wait r5: wait w6: wait r7: wait r1 r2 r3 w4, w6, r5, r7

Solution Using Critical Regions Read() { region v when !writing && !waiting do reading++; // read region v when true do reading--; }

Solution Using Critical Regions Write() { region v when true do waiting++; region v when !writing && !reading do { waiting--; writing = 1 } // write region v when true do writing = 0; }

Solution 2 Read() { region v when true do waiting++; region v when !writing do {waiting--; reading++;} // read region v when true do reading--; }

Solution 2 (cont’d) Write() { region v when !reading && !writing && !waiting do writing = 1; // write region v when true do writing = 0; }

Some Important Properties By definition, there is no solution that can be starvation-free, why? There are several variations on the problem definition to avoid starvation and add more exotic features. Solution using monitors: see quiz