Readers-Writers Problem Akos Ledeczi EECE 354, Fall 2012 Vanderbilt University.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Resource management and Synchronization Akos Ledeczi EECE 354, Fall 2010 Vanderbilt University.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
1 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
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
Monitors and Blocking Synchronization By Tal Walter.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Concurrency: Deadlock and Starvation Chapter 6. Revision Describe three necessary conditions for deadlock Which condition is the result of the three necessary.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Classical Synchronization Problems. Paradigms for Threads to Share Data We’ve looked at critical sections –Really, a form of locking –When one thread.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
1 Concurrency: Deadlock and Starvation Chapter 6.
10/04/2011CS4961 CS4961 Parallel Programming Lecture 12: Advanced Synchronization (Pthreads) Mary Hall October 4, 2011.
CS 149: Operating Systems February 17 Class Meeting Department of Computer Science San Jose State University Spring 2015 Instructor: Ron Mak
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic,
LAB 11: Task Synchronization Chung-Ta King National Tsing Hua University CS 4101 Introduction to Embedded Systems.
Nachos Phase 1 Code -Hints and Comments
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Maekawa’s algorithm Divide the set of processes into subsets that satisfy the following two conditions: i  S i  i,j :  i,j  n-1 :: S i  S j.
Memory Management Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 2 Processes and Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Scheduling in µC/OS-III Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Classical problems.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
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.
4061 Session 23 (4/10). Today Reader/Writer Locks and Semaphores Lock Files.
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.
Cpr E 308 Spring 2004 Real-time Scheduling Provide time guarantees Upper bound on response times –Programmer’s job! –Every level of the system Soft versus.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Lecture 11: Synchronization (Chapter 6, cont)
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
CIS Operating Systems Synchronization Professor Qiang Zeng Fall 2015.
Operating Systems Inter-Process Communications. Lunch time in the Philosophy Department. Dining Philosophers Problem (1)
COSC 3407: Operating Systems Lecture 9: Readers-Writers and Language Support for Synchronization.
Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Message Passing Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization.
1 Processes and Threads Part II Chapter Processes 2.2 Threads 2.3 Interprocess communication 2.4 Classical IPC problems 2.5 Scheduling.
Homework-6 Questions : 2,10,15,22.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Synchronization Akos Ledeczi EECE 6354, Fall 2013 Vanderbilt University.
Big Picture Lab 4 Operating Systems C Andras Moritz
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
Real-Time Threads Time dependent, usually wrt deadline –Periodic –Aperiodic –Sporadic Priority scheduled Fault tolerant.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
CS703 - Advanced Operating Systems
CPS110: Reader-writer locks
Process Synchronization
Monitors, Condition Variables, and Readers-Writers
Lecture 16: Readers-Writers Problem and Message Passing
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Akos Ledeczi EECE 6354, Fall 2017 Vanderbilt University
Akos Ledeczi EECE 6354, Fall 2017 Vanderbilt University
Semaphore Originally called P() and V() wait (S) { while S <= 0
CS703 - Advanced Operating Systems
Lecture 16: Readers-Writers Problem and Message Passing
CSE 153 Design of Operating Systems Winter 2019
“The Little Book on Semaphores” Allen B. Downey
Akos Ledeczi EECE 6354, Fall 2017 Vanderbilt University
Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University
Presentation transcript:

Readers-Writers Problem Akos Ledeczi EECE 354, Fall 2012 Vanderbilt University

Problem A number of tasks need to access a shared resource (database, data structure etc.) Some of them want to modify it (writers), some only need to read it (readers) Asymmetric problem: 1. Any number of readers can be in the critical section simultaneously. 2. Each writer must have exclusive access to the critical section.

First Try OSMutex access; int readers = 0; OSMutex rdCtrl; void write() { … OSMutexPend(&access,0,OS_OPT_PEND_BLOCKING,&err); /* critical section for writers */ OSMutexPost(&access,OS_OPT_POST_1,&ts,&err); … } void read() { … OSMutexPend(&rdCtrl,0,OS_OPT_PEND_BLOCKING,&err); readers++; if (readers == 1) OSMutexPend(&access,0,OS_OPT_PEND_BLOCKING,&err); /* first in locks */ OSMutexPost(&rdCtrl,OS_OPT_POST_1,&ts,&err); /* critical section for readers */ OSMutexPend(&rdCtrl,0,OS_OPT_PEND_BLOCKING,&err); readers--; if (readers == 0) OSMutexPost(&access,OS_OPT_POST_1,&ts,&err); /* last out unlocks */ OSMutexPost(&rdCtrl,OS_OPT_POST_1,&ts,&err); … }

Trouble A high-priority writer will be prevented from accessing the critical section. It will have to wait for the current readers to finish (which is OK), but it will be passed by potentially lower-priority readers arriving later. The writer can even be completely starved by a steady stream of readers. Priority inheritance does not work here because only the first reader reserves the mutex, so its priority can be elevated by a waiting writer, but all the other readers do not hold the mutex, so they can access the critical section regardless of their and the waiting writer’s priority

Second Try OSMutex access; OSMutex gateKeeper; int readers = 0; OSMutex rdCtrl; void write() { … OSMutexPend(&gateKeeper,&err);/* to prevent readers from cutting in */ OSMutexPend(&access,0,OS_OPT_PEND_BLOCKING,&err); /* critical section for writers */ OSMutexPost(&gateKeeper,&ts,&err); OSMutexPost(&access,OS_OPT_POST_1,&ts,&err); … } void read() { … OSMutexPend(&gateKeeper,&err); OSMutexPost(&gateKeeper,&ts,&err); /* unchanged from previous version */ … }

Trouble A high-priority reader arriving later to the gate will be prevented from entering. It will have to wait for the potentially lower-priority current readers to finish and then for the potentially lower-priority writer to write before it gets a chance to proceed. The first part in “normal” cases would be OK, but here this new reader should be able to access the critical section without waiting, as multiple readers are allowed.

The “Ultimate” Solution Read Manager inherits priority of highest–priority waiting Reader (our code does this, Flags do not support this unlike Mutex-s) Wr MRd Data Mutex Event Flag Group (ANY SET trigger) Message Queue (LIFO, Broadcast) (10) (1) (2) (4) (3) (5) (7) (12) (9) (8) (11) (6)

Remarks Readers can arrive even after the Manager has been triggered: – They still elevate the priority of the Manager while it is still waiting for Writers. – They will still get the result of the actual read operation as long as they get to pending on the message queue before the Manager posts the result. Potential problem: since the Manager can hold the mutex, µC/OS-III can elevate its priority to that of a waiting Writer. When it resets it, it will set it to the value it was right before it elevated it. If we elevated it further because of a new high-priority Reader, it may get set to a wrong value. In this case, however, I do not think it can actually happen. Why a message queue, if we flush it and post LIFO anyways? If it was just a shared global variable, it would be the same (at least very similar) readers-writer problem that we began with in the first place…