1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Synchronization NOTE to instructors: it is helpful to walk through an example such as readers/writers locks for illustrating the use of condition variables.
Ch 7 B.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
1 Mutual Exclusion: Primitives and Implementation Considerations.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 13: October 12, 2010 Instructor: Bhuvan Urgaonkar.
6.5 Semaphore Can only be accessed via two indivisible (atomic) operations wait (S) { while S
Monitors Chapter 7. The semaphore is a low-level primitive because it is unstructured. If we were to build a large system using semaphores alone, the.
CS444/CS544 Operating Systems Synchronization 2/21/2006 Prof. Searleman
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts Amherst Operating Systems CMPSCI 377 Lecture.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Computer Science 162 Discussion Section Week 3. Agenda Project 1 released! Locks, Semaphores, and condition variables Producer-consumer – Example (locks,
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.
9/8/2015cse synchronization-p1 © Perkins DW Johnson and University of Washington1 Synchronization Part 1 CSE 410, Spring 2008 Computer Systems.
CS510 Concurrent Systems Introduction to Concurrency.
Cosc 4740 Chapter 6, Part 3 Process Synchronization.
Implementing Synchronization. Synchronization 101 Synchronization constrains the set of possible interleavings: Threads “agree” to stay out of each other’s.
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.
Midterm 1 – Wednesday, June 4  Chapters 1-3: understand material as it relates to concepts covered  Chapter 4 - Processes: 4.1 Process Concept 4.2 Process.
1 Previous Lecture Review n Concurrently executing threads often share data structures. n If multiple threads are allowed to access shared data structures.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
CSCI-375 Operating Systems Lecture Note: Many slides and/or pictures in the following are adapted from: slides ©2005 Silberschatz, Galvin, and Gagne Some.
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.
13/03/07Week 21 CENG334 Introduction to Operating Systems Erol Sahin Dept of Computer Eng. Middle East Technical University Ankara, TURKEY URL:
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.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
CSE 153 Design of Operating Systems Winter 2015 Midterm Review.
Practice Chapter Five.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
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.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
Deadlock and Starvation
CS703 - Advanced Operating Systems
CS703 – Advanced Operating Systems
Deadlock and Starvation
Monitors, Condition Variables, and Readers-Writers
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Critical section problem
Implementing Mutual Exclusion
CSE 451: Operating Systems Autumn Lecture 8 Semaphores and Monitors
Implementing Mutual Exclusion
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
CSE 153 Design of Operating Systems Winter 19
CS333 Intro to Operating Systems
EECE.4810/EECE.5730 Operating Systems
Don Porter Portions courtesy Emmett Witchel
Presentation transcript:

1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This allows avoid using ad hoc Kernel synchronization techniques like non-preemptive kernel allows to implement in multiprocessors  problems programming with semaphores is error prone - the code is often cryptic: F a semaphore combines the counting mechanism and synchronization mechanism

2 Lecture 13: Locks and Condition Variables  problems with semaphores  locks and CVs definition and usage solutions to synchronization problems implementation

3 Semaphore= Lock+Condition Variable  programming with semaphores is deadlock-prone P(fridge);V(fridge);if (noMilk){ buy milk; buy milk; noMilk=false; noMilk=false; } }P(fridge); are these programs correct? what’s wrong with them?  solution — new synchronization constructs locks — provide mutual exclusion condition variables — provide conditional synchronization Like semaphores, locks and condition variables are language- independent, and are available in many programming environments

4 Locks provide mutually exclusive access to shared data:  a lock can be “locked” or “unlocked” (sometimes called “busy” and “free”) initially it is unlocked  a thread is said to have (own) the lock if it successfully executed acquire statement.  if other threads attempt to acquire a lock - they are suspended  to achieve mutually exclusive access to variables threads should access them only inside acquire/release statements Thread A Thread B acquire(milk ); if (noMilk) buy milk; buy milk; release(milk); release(milk );

5 Conditional Lock Release Queue::Add(int *item){ lock->acquire(); /* add item to queue */ lock->release(); } Queue::Remove(){ int *item; lock->acquire(); if (!queue->empty()){ /* remove item from queue */ } lock->release(); return(item); }  Queue::Remove will only return an item if there’s already one in the queue  if the queue is empty, it might be more desirable for Queue::Remove to wait until there is something to remove  can’t just go to sleep – while holding lock – prevents other threads from updating the queue  solution: a condition variable lets a thread sleep inside a critical section, by releasing the lock while the thread sleeps

6 Condition Variables  condition variable (CV) coordinates events  three basic operations on CVs: wait - blocks the thread and releases the associated lock signal - if threads are waiting on the lock, wake up one of those threads and put it on the ready list; otherwise do nothing broadcast — if threads are waiting on the lock, wake up all of those threads and put them on the ready list; otherwise do nothing  problem: when a thread P wakes up another Q they are technically both inside the protected area. Which thread is allowed to proceed? P (Hansen/Mesa style) – seems “logical” but the awakened thread may miss the condition Q (Hoare style) – what to do with signaling thread? P proceeds but immediately releases the lock – can wake up only one thread  all these techniques are implemented and equivalent in power

7 Producer/Consumer with Locks and CVs  Unbounded producer/consumer with locks and CVs  Associated with a data structure is both a lock and a condition variable Before the program performs an operation on the data structure, it acquires the lock If it needs to wait until another operation puts the data structure into an appropriate state, it uses the condition variable to wait conditionvar *cv; lock *lk; int avail=0; /* producer */ while(1){ acquire(lk); /* produce next */ avail++; signal(cv,lk); release(lk); } /* consumer */ while(1){ acquire(lk); if(avail==0) wait(cv,lk); /* consume next */ avail--; release(lk); }

8 Readers/Writers with Locks and CVs is this a readers or writers preference solution? conditionvar wrt, rdr; int nr=0, nw=0; lock lk; writer() { acquire(lk); while(nr>0 || nw>0) wait(wrt,lk); nw++; release(lk); /* perform write */ acquire(lk); nw--; signal(wrt); broadcast(rdr); release(lk); } reader() { acquire(lk); while(nw>0) wait(rdr,lk); nr++; release(lk); /* perform read */ acquire(lk); nr--; if(nr==0) signal(wrt); release(lk); } notice the use of broadcast to wake up all readers

9  the issues related to implementation of semaphores and locks/CVs are similar  spinlock - a locked process does not release CPU but rather “spins” constantly checking the lock until it opens  sleeplock - a locked process blocks and is put back on the ready queue only when the lock is open Locks/CVs Implementation

10  simplest implementation of locks - set up a boolean variable ( *s ) is by busy waiting and constantly checking on it’s value with atomic RMW instruction like test&set ( testnset )  problem - test&set monopolizes memory access and degrades system performance  solution - have two while loops check by test&set once - if locked - check with regular read until unlocked  what’s the problem with both of these solutions?  unfair! void spin_lock (bool *s) { while (testnset(*s)) ; } void spin_unlock (bool *s) { *s=FALSE; } void spin_lock (bool *s) { while (testnset(*s)) while (*s) ; } void spin_unlock (bool *s) { *s=FALSE; } variant 1 variant 2 Spinlock Implementation

11 Implementing CV Using Spinlocks  the CV contains a list that holds the waiting threads, the operations on this list are protected by a spinlock  note the difference between this spinlock - the internal CV lock and s - the external lock that is used in association with the CV /* condition consists of: list - waiting threads listlock - lock protecting operation on list*/ void wait(condition *c, lock *s){ spinlock(c->listlock); /* add self to list */ spinunlock(c->listlock); unlock(s); /* block current thread */ lock(s); return; } void signal(condition *c){ spinlock(c->listlock); /* remove a thread from list if list not empty */ spinunlock(c->listlock); /* make removed thread runnable */ }

12 To Block or to Spin  both techniques have advantages and problems spinning - ties up the processor but efficient in short waits blocking – necessary for long waits but inefficient (due to context switching overhead) for short ones  solutions store a “clue” as to how long the expected wait is to be and let the application decide which type of lock to use adaptive lock (Solaris) implemented as follows: when a thread T1 tries to acquire an adaptive lock held by another thread T2, it checks to see if T2 is currently active on any processor F as long as T2 is active, T1 spins F if T2 is blocked, T1 blocks

13 Semaphore= Lock+Condition Variable  programming with semaphores is deadlock-prone P(fridge);V(fridge);if (noMilk){ buy milk; buy milk; noMilk=false; noMilk=false; } }P(fridge); are these programs correct? what’s wrong with them?  solution — new synchronization constructs (Conditional) Critical region F region v when B do S; variable v is a shared variable that can only be accessed inside the critical region F Boolean expression B governs access F Statement S ( critical region) is executed only if B is true; otherwise it blocks until B becomes true F can prevent some simple programming errors but still problematic monitors (somewhat similar to locks)