Readers/Writers Problem. Readers/Writers: Scenario 1. Readers/Writers Problem. Today: Synchronization for Readers/Writers Problem

Similar documents
Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Synchronization Principles II

Semaphores (by Dijkstra)

Process Synchronization

Synchronization Principles

Roadmap. Readers-Writers Problem. Readers-Writers Problem. Readers-Writers Problem (Cont.) Dining Philosophers Problem.

Roadmap. Tevfik Ko!ar. CSC Operating Systems Fall Lecture - XI Deadlocks - II. Louisiana State University

Roadmap. Bounded-Buffer Problem. Classical Problems of Synchronization. Bounded Buffer 1 Semaphore Soln. Bounded Buffer 1 Semaphore Soln. Tevfik Ko!

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

Process Synchronization

Chapter 6: Process Synchronization

EI 338: Computer Systems Engineering (Operating Systems & Computer Architecture)

Chapter 7: Process Synchronization!

CS370: System Architecture & Software [Fall 2014] Dept. Of Computer Science, Colorado State University

Last Class: Synchronization Problems!

PESIT Bangalore South Campus

CHAPTER 6: PROCESS SYNCHRONIZATION

Chapter 6: Process Synchronization. Operating System Concepts 9 th Edit9on

9/30/2014. CS341: Operating System High Level Construct: Monitor Deadlock Conditions Prevention, Avoidance Detection and Recovery

Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - X Deadlocks - I. University at Buffalo. Synchronization structures

Roadmap. Problems with Semaphores. Semaphores. Monitors. Monitor - Example. Tevfik Koşar. CSE 421/521 - Operating Systems Fall 2012

Introduction to Operating Systems

Chapter 6: Process Synchronization. Module 6: Process Synchronization

CS 537 Lecture 8 Monitors. Thread Join with Semaphores. Dining Philosophers. Parent thread. Child thread. Michael Swift

Lesson 6: Process Synchronization

CS3502 OPERATING SYSTEMS

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

Module 6: Process Synchronization

Chapter 7: Process Synchronization. Background. Illustration

CS370 Operating Systems

Process Synchronization

Semaphores. A semaphore is an object that consists of a. methods (e.g., functions): signal and wait. semaphore. method signal. counter.

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

Process Synchronization

Chapter 6: Process Synchronization

Chapter 7: Process Synchronization. Background

Process Synchronization(2)

Chapter 5: Process Synchronization

Chapter 6: Process Synchronization

CS370 Operating Systems

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

Chapter 6 Synchronization

Process Synchronization

CS 361 Concurrent programming Drexel University Spring 2000 Lecture 14. The dining philosophers problem

Interprocess Communication By: Kaushik Vaghani

Chapter 6 Process Synchronization

Lecture 3: Synchronization & Deadlocks

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Chapter 5: Process Synchronization

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Process Synchronization. CISC3595, Spring 2015 Dr. Zhang

Chapter 6: Process Synchronization. Operating System Concepts 8 th Edition,

5 Dining Philosophers. Template for Philosopher. Naive Solution. while (food available) /*pick up forks*/ eat; /*put down forks*/ think awhile;

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Process Synchronization(2)

Process Synchronization

Prof. Hui Jiang Dept of Computer Science and Engineering York University

The dining philosophers problem. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 13

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

Outline for Today. 5 Dining Philosophers. Template for Philosopher. Naive Solution. Objective: Administrative details:

High-level Synchronization

Semaphore. Originally called P() and V() wait (S) { while S <= 0 ; // no-op S--; } signal (S) { S++; }

More Synchronization; Concurrency in Java. CS 475, Spring 2018 Concurrent & Distributed Systems

Chapter 6: Process Synchronization

Chapter 6: Process Synchronization

Synchronization Basic Problem:

Lecture 5: Inter-process Communication and Synchronization

Background. Module 6: Process Synchronization. Bounded-Buffer (Cont.) Bounded-Buffer. Background

Process Synchronization(2)

Chapter 5: Process Synchronization

Operating systems. Lecture 12

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Process Co-ordination OPERATING SYSTEMS

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Process Synchronization. studykorner.org

Real-Time Operating Systems M. 5. Process Synchronization

More Types of Synchronization 11/29/16

UNIT II PROCESS MANAGEMENT 9

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

CSE Opera,ng System Principles

Process Synchronization

Chapter 6: Synchronization. Operating System Concepts 8 th Edition,

Enforcing Mutual Exclusion Using Monitors

Chapter 6: Synchronization

Che-Wei Chang Department of Computer Science and Information Engineering, Chang Gung University

Process Synchronization

3C03 Concurrency: Starvation and Deadlocks

OS Process Synchronization!

Module 6: Process Synchronization

Lecture 9: Thread Synchronizations. Spring 2016 Jason Tang

Sections 01 (11:30), 02 (16:00), 03 (8:30) Ashraf Aboulnaga & Borzoo Bonakdarpour

Processes. Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra

Concurrency pros and cons. Concurrent Programming Problems. Mutual Exclusion. Concurrency is good for users

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall

Process Coordination

Silberschatz and Galvin Chapter 6

5 Classical IPC Problems

Chapter 2 Processes and Threads

Concurrent Programming Issues & Readers/Writers

Dealing with Issues for Interprocess Communication

Transcription:

Today: Synchronization for Readers/Writers Problem An object is shared among may threads, each belonging to one of two classes: Readers: read data, never modify it Writers: read data and modify it Using a single lock on the data object is overly restrictive => Want many readers reading the object at once Allow only one writer at any point How do we control access to the object to permit this protocol? Correctness criteria: Each read or write of the shared data must happen within a critical section. Guarantee mutual exclusion for writers. Allow multiple readers to execute in the critical section at once. class ReadWrite { public: void Read(); Readers/Writers Problem void Write(); private: int readers; // counts readers Semaphore mutex; // controls access to readers Semaphore wrt; // controls entry to first // writer or reader ReadWrite::ReadWrite { readers = 0; mutex->value = 1; wrt->value = 1; 1 2 Readers/Writers Problem ReadWrite::Write(){ wrt.wait(); // any writers or readers? <perform write> wrt.signal(); // enable others ReadWrite::Read(){ mutex.wait(); // ensure mutual exclusion readers += 1; // another reader if (readers == 1) wrt->wait(); // block writers mutex.signal(); <perform read> mutex.wait(); // ensure mutual exclusion readers -= 1; // reader done if (readers == 0) wrt.signal();// enable writers mutex.signal(); Readers/Writers: Scenario 1 R1: R2: W1: 3 4

Readers/Writers: Scenario 2 R1: R2: W1: Reader/Writers: Scenario 3 R1: R2: W1: 5 6 Readers/Writers Solution: Discussion Readers/Writers Solution Favoring Writers Implementation notes: 1. The first reader blocks if there is a writer; any other readers who try to enter block on mutex. 2. The last reader to exit signals a waiting writer. 3. When a writer exits, if there is both a reader and writer waiting, which goes next depends on the scheduler. 4. If a writer exits and a reader goes next, then all readers that are waiting will fall through (at least one is waiting on wrt and zero or more can be waiting on mutex). 5. Does this solution guarantee all threads will make progress? Alternative desirable semantics: Let a writer enter its critical section as soon as possible. ReadWrite::Write(){ write_mutex.wait(); // ensure mutual exclusion writers += 1; // another pending writer if (writers == 1) // block readers read_block.wait(); write_mutex.signal(); write_block.wait(); // ensure mutual exclusion <perform write> write_block.signal(); write_mutex.wait(); // ensure mutual exclusion writers -= 1; // writer done if (writers == 0) // enable readers read_block.signal(); write_mutex.signal(); 7 8

Readers/Writers Solution Favoring Writers Readers/Writers: Scenario 4 ReadWrite::Read(){ write_pending->wait(); // ensures at most one reader will go // before a pending write read_block->wait(); read_mutex->wait(); // ensure mutual exclusion readers += 1; // another reader if (readers == 1) // synchronize with writers write_block->wait(); read_mutex->signal(); read_block->signal(); write_pending->signal(); <perform read> read_mutex->wait(); // ensure mutual exclusion readers -= 1; // reader done if (readers == 0) // enable writers write_block->signal(); read_mutex->signal(); R1: R2: W1: W2: 9 10 Readers/Writers: Scenario 5 Reader/Writers: Scenario 6 R1: R2: W1: W2: R1: R2: W1: W2: 11 12

Readers/Writers using Monitors (Java) Readers/Writers using Monitors (Java) class ReaderWriter { private int numreaders = 0; private int numwriters = 0; private synchronized void prepareto { while ( numwriters > 0 ) wait (); numreaders++; private synchronized void donereading () { numreaders--; if ( numreaders == 0 ) notify (); public... somereadmethod () { // reads NOT synchronized: multiple readers prepareto; <do the reading> private void prepareto { numwriters++; while ( numreaders!= 0 ) wait (); private void donewriting () { numwriters--; notify (); public synchronized void somewritemethod (...) { donereading (); // syncronized => only one writer prepareto; <do the writing> donewriting (); 13 14 Read/write Locks pthreads and Java support read/write locks A thread can acquire a read lock or a write lock Multiple threads can hold the same read lock concurrently Only one thread can hold a write lock Java: ReadWriteLock class readlock() writelock() pthread routines: pthread_rwlock_init() pthread_rwlock_rdlock() pthread_rwlock_wrlock() pthread_rwlock_unlock()! Dining Philosophers It s lunch time in the philosophy dept Five philosophers, each either eats or thinks Share a circular table with five chopsticks Thinking: do nothing Eating => need two chopsticks, try to pick up two closest chopsticks Block if neighbor has already picked up a chopstick After eating, put down both chopsticks and go back to thinking 15 16

Dining Philosophers v1 Dining Philosophers v2 (monitors) Semaphore chopstick[5]; do{ wait(chopstick[i]); // left chopstick wait(chopstick[(i+1)%5 ]); // right chopstick // eat signal(chopstick[i]); // left chopstick signal(chopstick[(i+1)%5 ]); // right chopstick // think while(true); monitor DP {! enum { THINKING; HUNGRY, EATING) state [5] ;! condition self [5]; void synchronized pickup (int i) {! state[i] = HUNGRY;! test(i);! if (state[i]!= EATING)! self [i].wait;!! void synchronized putdown (int i) {! state[i] = THINKING; //test left and right neighbors! test((i + 4) % 5);! test((i + 1) % 5); void test (int i) { if ( (state[(i + 4) % 5]!= EATING)&& (state[i] == HUNGRY) && (state[(i + 1) % 5]!= EATING) ) {! state[i] = EATING ;!! self[i].signal () ; initialization_code() {! for (int i = 0; i < 5; i++)! state[i] = THINKING;! 17 18 Dining Philosophers (semaphores) Dining Philosophers (contd) 19 20

Summary Readers/writers problem: Allow multiple readers to concurrently access a data Allow only one writer at a time Two possible solutions using semaphores Favor readers Favor writers Starvation is possible in either case! Dining philosophers: mutually exclusive access to multiple resources 21