CS703 - Advanced Operating Systems

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

1 Semaphores and Monitors CIS450 Winter 2003 Professor Jinhua Guo.
COSC 3407: Operating Systems Lecture 8: Semaphores, Monitors and Condition Variables.
Multi-Object Synchronization. Main Points Problems with synchronizing multiple objects Definition of deadlock – Circular waiting for resources Conditions.
– 1 – , F’02 Traditional View of a Process Process = process context + code, data, and stack shared libraries run-time heap 0 read/write data Program.
CS162 Operating Systems and Systems Programming Lecture 8 Readers-Writers Language Support for Synchronization February 13, 2006 Prof. Anthony D. Joseph.
CS162 Operating Systems and Systems Programming Lecture 8 Readers-Writers Language Support for Synchronization September 26, 2005 Prof. John Kubiatowicz.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
CS162 Operating Systems and Systems Programming Lecture 8 Readers-Writers Language Support for Synchronization September 23, 2009 Prof. John Kubiatowicz.
CS162 Operating Systems and Systems Programming Lecture 8 Readers-Writers Language Support for Synchronization September 25, 2006 Prof. John Kubiatowicz.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Concurrent HTTP Proxy with Caching Ashwin Bharambe Monday, Dec 4, 2006.
Monitor Solutions to Classical Problems. 2 Announcements CS 415 Projects graded. –Mean 80.7, High 90 out of 90 CS 414 Homework due Monday.
Semaphores and Bounded Buffer Andy Wang Operating Systems COP 4610 / CGS 5765.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Semaphores and Bounded Buffer. Semaphores  Semaphore is a type of generalized lock –Defined by Dijkstra in the last 60s –Main synchronization primitives.
1 Concurrent Programming. 2 Outline Semaphores for Shared Resources –Producer-consumer problem –Readers-writers problem Example –Concurrent server based.
Processes and Threads-V Concurrency and Synchronization.
SYNCHRONIZATION-2 Slides from: Computer Systems: A Programmer's Perspective, 2nd Edition by Randal E. Bryant and David R. O'Hallaron, Prentice Hall, 2011.
1 Using Semaphores CS 241 March 14, 2012 University of Illinois Slides adapted in part from material accompanying Bryant & O’Hallaron, “Computer Systems:
Kernel Locking Techniques by Robert Love presented by Scott Price.
CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables February 2, 2011 Ion Stoica
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Week 16 (April 25 th ) Outline Thread Synchronization Lab 7: part 2 & 3 TA evaluation form Reminders Lab 7: due this Thursday Final review session Final.
CS162 Operating Systems and Systems Programming Lecture 7 Readers-Writers Language Support for Synchronization February 13, 2008 Prof. Anthony D. Joseph.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
1 Condition Variables CS 241 Prof. Brighten Godfrey March 16, 2012 University of Illinois.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
COSC 3407: Operating Systems Lecture 9: Readers-Writers and Language Support for Synchronization.
CS162 Operating Systems and Systems Programming Lecture 8 Readers-Writers Language Support for Synchronization Friday 11, 2010 Ion Stoica
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
Concurrency Threads Synchronization Thread-safety of Library Functions Anubhav Gupta Dec. 2, 2002 Outline.
CS162 Section 2. True/False A thread needs to own a semaphore, meaning the thread has called semaphore.P(), before it can call semaphore.V() False: Any.
Operating Systems NWEN 301 Lecture 6 More Concurrency.
1 CMSC Laundry List P/W/F, Exam, etc. Instructors: HSG, HH, MW.
CS162 Operating Systems and Systems Programming Lecture 9 Synchronization, Readers/Writers example, Scheduling September 26th, 2016 Prof. Anthony D.
CS703 - Advanced Operating Systems
2 July 2015 Charles Reiss
CPS110: Reader-writer locks
Threads Synchronization Thread-safety of Library Functions
CS703 – Advanced Operating Systems
PARALLEL PROGRAM CHALLENGES
“Language Mechanism for Synchronization”
Synchronization and Scheduling
CS162 Operating Systems and Systems Programming Lecture 6 Readers/Writers Problem, Working in Teams February 10, 2014 Anthony D. Joseph
CS 3214 Computer Systems Lecture 21 Godmar Back.
Semaphores and Condition Variables
Monitors, Condition Variables, and Readers-Writers
Processes and Monitors in Mesa (Lecture 6, cs262a)
Introduction to Operating Systems
Sarah Diesburg Operating Systems COP 4610
Lecture 14: Pthreads Mutex and Condition Variables
September 17, 2012 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 6 Readers/Writers Problem,
Programming with Threads
Thread Implementations; MUTEX
Anthony D. Joseph and Ion Stoica
Programming with Threads Dec 5, 2002
Threading And Parallel Programming Constructs
Lecture 14: Pthreads Mutex and Condition Variables
September 12, 2012 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables.
Thread Implementations; MUTEX
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
Chapter 7: Synchronization Examples
EECE.4810/EECE.5730 Operating Systems
Don Porter Portions courtesy Emmett Witchel
Review The Critical Section problem Peterson’s Algorithm
Presentation transcript:

CS703 - Advanced Operating Systems By Mr. Farhan Zaidi

Lecture No. 12

Overview of today’s lecture Readers/writers problem Solving readers/writers problem using condition variables Pros and cons of the solution Duality of synchronization primitives Implementing condition variables using semaphores as building blocks Thread safety and reentrant functions Ways to solve thread un-safety problem of library functions Thread un-safe functions in C library Recap of lecture

Readers/Writers (2) Constraints 1. Readers can access database when no writers (Condition okToRead) 2. Writers can access database when no readers or writers (Condition okToWrite) 3. Only one thread manipulates state variables at a time.

Readers/Writers(3) State variables: Condition okToRead = NIL Basic structure of solution Reader wait until no writers access database check out -- wake up waiting writer Writer wait until no readers or writers check out -- wake up waiting readers or writer State variables: # of active readers -- AR = 0 # of active writers -- AW = 0 # of waiting readers -- WR = 0 # of waiting writers -- WW = 0 Condition okToRead = NIL Condition okToWrite = NIL Lock lock = FREE

Readers/Writers (4) Code: Reader() { lock.Acquire(); while ((AW + WW) > 0) { // check if safe to read // if any writers, wait WR++; okToRead.Wait(&lock); WR--; } AR++; lock.Release(); Access DB AR--; If (AR == 0 && WW > 0)//if no other readers still // active, wake up writer okToWrite.Signal(&lock);

Readers/Writers (5) Writer() { // symmetrical lock.Acquire(); while ((AW + AR) > 0) { // check if safe to write // if any readers or writers, wait WW++; okToWrite->Wait(&lock); WW--; } AW++; lock.Release(); Access DB // check out AW--; if (WW > 0) // give priority to other writers okToWrite->Signal(&lock); else if (WR > 0) okToRead->Broadcast(&lock);

Questions 1. Can readers or writers starve? Who and Why? 2. Why does checkRead need a while?

semaphores and monitors Illustrate the differences by considering: can we build monitors out of semaphores? After all, semaphores provide atomic operations and queueing. Does this work? Wait() { semaphore - > P(); } Signal() { semaphore - > V(); } Condition variables only work inside of a lock.. Does this work? Wait(Lock *lock) { lock->Release(); Semaphore - > P(); Lock - > Acquire(); } Signal() { Semaphore - > V();

semaphores and monitors(2) What if thread signals and no one is waiting? No op. What if thread later waits? Thread waits. What if thread V's and no one is waiting? Increment. What if thread later does P? Decrement and continue. In other words, P + V are commutative -- result is the same no matter what order they occur. Condition variables are NOT commutative. That's why they must be in a critical section --need to access state variables to do their job. Does this fix the problem? Signal() { if semaphore queue is not empty semaphore->V(); } For one, not legal to look at contents of semaphore queue. But also: race condition -- signaller can slip in after lock is released, and before wait. Then waiter never wakes up! Need to release lock and go to sleep atomically. Is it possible to implement condition variables using semaphores? Yes!!!

Semaphore mutex = 1; // This lock is outside of the condition object { Semaphore lock = 1; Seamphore waitSem = 0; Int numWaiters = 0; } wait(cond, mutex) signal (cond, mutex) { { P(cond.lock); P(cond.lock); cond.numWaiters++; if (cond.numWaiters > 0) V(cond.lock); { V(mutex); V(cond. waitSem); P(cond.waitSem); P(cond.lock); } cond.numWaiters - -; V(cond.lock); V(cond.lock); P(mutex); }

Thread Safety Class 1: Failing to protect shared variables. Functions called from a thread must be thread-safe. We identify four (non-disjoint) classes of thread-unsafe functions: Class 1: Failing to protect shared variables. Class 2: Relying on persistent state across invocations. Class 3: Returning a pointer to a static variable. Class 4: Calling thread-unsafe functions.

Thread-Unsafe Functions Class 1: Failing to protect shared variables. Fix: Use P and V semaphore operations. Issue: Synchronization operations will slow down code.

Thread-Unsafe Functions (cont) Class 3: Returning a ptr to a static variable. Fixes: 1. Rewrite code so caller passes pointer to struct. Issue: Requires changes in caller and callee. 2. Lock-and-copy Issue: Requires only simple changes in caller (and none in callee) However, caller must free memory. struct hostent *gethostbyname(char name) { static struct hostent h; <contact DNS and fill in h> return &h; } hostp = Malloc(...)); gethostbyname_r(name, hostp); struct hostent *gethostbyname_ts(char *p) { struct hostent *q = Malloc(...); P(&mutex); /* lock */ p = gethostbyname(name); *q = *p; /* copy */ V(&mutex); return q; }

Thread-Unsafe Functions Class 4: Calling thread-unsafe functions. Calling one thread-unsafe function makes an entire function thread-unsafe. Fix: Modify the function so it calls only thread-safe functions

Reentrant Functions A function is reentrant iff it accesses NO shared variables when called from multiple threads. Reentrant functions are a proper subset of the set of thread-safe functions. Thread-safe functions Thread-unsafe functions Reentrant functions

Thread-Safe Library Functions All functions in the Standard C Library are thread-safe. Examples: malloc, free, printf, scanf Most Unix system calls are thread-safe, with a few exceptions: Thread-unsafe function Class Reentrant version asctime 3 asctime_r ctime 3 ctime_r gethostbyaddr 3 gethostbyaddr_r gethostbyname 3 gethostbyname_r inet_ntoa 3 (none) localtime 3 localtime_r rand 2 rand_r