U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts Amherst Operating Systems CMPSCI 377 Lecture.

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
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
1 Mutual Exclusion: Primitives and Implementation Considerations.
CS 162 Discussion Section Week 3. Who am I? Mosharaf Chowdhury Office 651 Soda 4-5PM.
CS444/CS544 Operating Systems Synchronization 2/21/2006 Prof. Searleman
Informationsteknologi Wednesday, September 26, 2007 Computer Systems/Operating Systems - Class 91 Today’s class Mutual exclusion and synchronization 
CS444/CS544 Operating Systems Synchronization 2/16/2007 Prof. Searleman
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
Chapter 2.3 : Interprocess Communication
Semaphores CSCI 444/544 Operating Systems Fall 2008.
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
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,
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Semaphores Questions answered in this lecture: Why are semaphores necessary? How are semaphores used for mutual exclusion? How are semaphores used for.
Semaphores. Readings r Silbershatz: Chapter 6 Mutual Exclusion in Critical Sections.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Implementing Synchronization. Synchronization 101 Synchronization constrains the set of possible interleavings: Threads “agree” to stay out of each other’s.
COMP 111 Threads and concurrency Sept 28, Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.
COSC 3407: Operating Systems Lecture 7: Implementing Mutual Exclusion.
Kernel Locking Techniques by Robert Love presented by Scott Price.
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
CS 3204 Operating Systems Godmar Back Lecture 7. 12/12/2015CS 3204 Fall Announcements Project 1 due on Sep 29, 11:59pm Reading: –Read carefully.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
CS252: Systems Programming Ninghui Li Based on Slides by Prof. Gustavo Rodriguez-Rivera Topic 11: Thread-safe Data Structures, Semaphores.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Software Systems Advanced Synchronization Emery Berger and Mark Corner University.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
1 Synchronization Threads communicate to ensure consistency If not: race condition (non-deterministic result) Accomplished by synchronization operations.
Lecture 6: Monitors & Semaphores. Monitor Contains data and procedures needed to allocate shared resources Accessible only within the monitor No way for.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
C H A P T E R E L E V E N Concurrent Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
CSC 660: Advanced Operating SystemsSlide #1 CSC 660: Advanced OS Synchronization.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
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.
CS 311/350/550 Semaphores. Semaphores – General Idea Allows two or more concurrent threads to coordinate through signaling/waiting Has four main operations.
CS703 – Advanced Operating Systems
Process Synchronization
Background on the need for Synchronization
Chapter 5: Process Synchronization
Threading And Parallel Programming Constructs
Module 7a: Classic Synchronization
Synchronization and Semaphores
Lecture 2 Part 2 Process Synchronization
Implementing Mutual Exclusion
CSCI1600: Embedded and Real Time Software
Implementing Mutual Exclusion
Chapter 6 Synchronization Principles
Thread Synchronization including Mutual Exclusion
September 12, 2012 Ion Stoica CS162 Operating Systems and Systems Programming Lecture 5 Semaphores, Conditional Variables.
CSE 153 Design of Operating Systems Winter 19
Chapter 6: Synchronization Tools
CSCI1600: Embedded and Real Time Software
Presentation transcript:

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts Amherst Operating Systems CMPSCI 377 Lecture 8: Synchronization II

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 2 Last Time: Synchronization Threads communicate to ensure consistency If not: race condition (non-deterministic result) Accomplished by synchronization operations How to write concurrent code How to implement synchronization operations

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 3 This Time: Locks & Semaphores Implementing locks Semaphores

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 4 Atomic Read-Write-Modify Instructions Atomically read old value, write new value SMP invalidates copies in other caches Cache coherence support Examples: Test & Set (most arch) Exchange (x86) Compare & Swap (68K, Sparc)

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 5 Implementing Locks: Test & Set int testset (int value) { int old = value; value = 1; return old; } pseudo-code: red = atomic What’s the effect of testset(value) when: value = 0? value = 1?

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 6 Implementing Locks: Test & Set int testset (int value) { int old = value; value = 1; return old; } pseudo-code: red = atomic class Lock { private int value; Lock() { value = 0; } void acquire() {…} void release() {…} } void acquire() { while (testset(value)) {} } void release() { value = 0; } acquire – wait until lock released, then take it release – release lock

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 7 Busy Waiting (“Spinning”) What’s wrong with this implementation? CPU utilization? Different priorities? void acquire() { while (testset(value)) {} } void release() { value = 0; } spin-lock

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 8 Minimizing Busy Waiting Can’t implement locks with test & set without any waiting (w/o disabling interrupts) Add queue to lock and sleep: blocking lock void acquire() { while (1) { if (testset(value)) { put thread on queue, sleep } else { break; } } } void release() { value = 0; wake up threads; }

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 9 Locks as Synch Operations +Locks provide mutual exclusion +Only one thread enters section at a time +Simplifies writing concurrent programs - Low-level - Can complicate coding - e.g., “allow at most n threads to access resource”  What are some alternatives?

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 10 This Time: Locks & Semaphores Implementing locks Semaphores

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 11 Brief Segue: Edsger W. Dijkstra ( ) Shortest Path First modern compiler (Algol-60) Stack for implementing recursion, vector First modular OS (THE) Predicate transformers On-the-fly garbage collection Self-stabilizing systems Mutual exclusion problem: “Deadly embrace” (now called deadlock) Semaphores

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 12 Semaphores What’s a “semaphore” anyway? A visual system for sending information by means of two flags that are held one in each hand, using an alphabetic code based on the position of the signaler's arms.

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 13 Semaphores What’s a “semaphore” anyway? A visual signaling apparatus with flags, lights, or mechanically moving arms, as one used on a railroad.

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 14 Semaphores What’s a “semaphore” anyway? A visual signaling apparatus with flags, lights, or mechanically moving arms, as one used on a railroad. Intuitively: regulates traffic at critical section

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 15 Semaphores in CS Computer science: Dijkstra (1965) A non-negative integer counter with atomic increment & decrement. Blocks rather than going negative. Higher-level than locks but not too high level

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 16 Semaphores: Key Concepts P(sem), a.k.a. wait = decrement counter If sem = 0, block until greater than zero P = “prolagen” (proberen te verlagen, “try to decrease”) In Holland the good Dr. Dijkstra Took a break from a walk on his bijkstra And said: "Which shall it be? Take a P or a V? For the two seem to me quite alijkstra!" V(sem), a.k.a. signal = increment counter Wake 1 waiting process V = “verhogen” (“increase”)

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 17 Implementing Semaphores class Semaphore { private int value; private Queue q; Semaphore(int v) { value = v; } } void wait() { if (value > 0) { value = value – 1; } if (value == 0) { add this process to Q; sleep(); } } void signal() { value = value + 1; if (anyone on Q) { remove P from Q; wakeup(P); } }

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 18 Variants of Semaphores Binary semaphore just two values (0 or 1), initially 1 (“free”) Counting semaphore useful when units of resource are available initialized to number of resources  thread can access as long as one unit available

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 19 Binary Semaphores: Example “too much milk” with locks thread A Lock.acquire() if (no milk) buy milk Lock.release() thread B Lock.acquire() if (no milk) buy milk Lock.release() thread A sem.wait() if (no milk) buy milk sem.signal() thread B sem.wait() if (no milk) buy milk sem.signal() “too much milk” with binary semaphores (initially 1)

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 20 Binary Semaphore: Example More flexible than locks! By initializing semaphore to 0, threads can wait for an event to occur thread A // wait for thread B sem.wait(); // do stuff … thread B // do stuff, then // wake up A sem.signal();

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 21 Counting Semaphores: Example Controlling resources: Allow threads to use at most 5 files simultaneously Initialize to 5 thread A sem.wait(); // use a file sem.signal(); thread B sem.wait(); // use a file sem.signal();

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 22 Summary Implementing locks Test & Set Spin locks, blocking locks Semaphores Generalization of locks Binary, “Dijkstra-style” Useful for: Controlling resources Waiting on events

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 23 Next Time Synchronization III! Monitors