Last Class: Synchronization

Similar documents
Today: Synchronization. Recap: Synchronization

Last Class: Synchronization. Review. Semaphores. Today: Semaphores. MLFQ CPU scheduler. What is test & set?

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

Lecture #7: Implementing Mutual Exclusion

Page 1. Goals for Today" Atomic Read-Modify-Write instructions" Examples of Read-Modify-Write "

Page 1. Goals for Today. Atomic Read-Modify-Write instructions. Examples of Read-Modify-Write

Page 1. Goals for Today" Atomic Read-Modify-Write instructions" Examples of Read-Modify-Write "

CS162 Operating Systems and Systems Programming Lecture 7. Mutual Exclusion, Semaphores, Monitors, and Condition Variables

Lecture 8: September 30

Page 1. Another Concurrent Program Example" Goals for Today" CS162 Operating Systems and Systems Programming Lecture 4

Page 1. Challenges" Concurrency" CS162 Operating Systems and Systems Programming Lecture 4. Synchronization, Atomic operations, Locks"

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

Operating Systems (1DT020 & 1TT802) Lecture 6 Process synchronisation : Hardware support, Semaphores, Monitors, and Condition Variables

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring Lecture 8: Semaphores, Monitors, & Condition Variables

Concept of a process

CS5460: Operating Systems

September 23 rd, 2015 Prof. John Kubiatowicz

What's wrong with Semaphores?

Last Class: Deadlocks. Today

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

PROCESS SYNCHRONIZATION READINGS: CHAPTER 5

Process Synchronisation (contd.) Operating Systems. Autumn CS4023

CSE Traditional Operating Systems deal with typical system software designed to be:

Synchronization Spinlocks - Semaphores

2 Threads vs. Processes

Symmetric Multiprocessors: Synchronization and Sequential Consistency

Systèmes d Exploitation Avancés

CS 153 Design of Operating Systems Winter 2016

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Goals. Processes and Threads. Concurrency Issues. Concurrency. Interlacing Processes. Abstracting a Process

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

Signaling and Hardware Support

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Steve Gribble. Synchronization. Threads cooperate in multithreaded programs

Lecture #7: Shared objects and locks

Dr. D. M. Akbar Hussain DE5 Department of Electronic Systems

Introduction to Operating Systems

CS 318 Principles of Operating Systems

Synchronization. Disclaimer: some slides are adopted from the book authors slides 1

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Concurrency: a crash course

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

Process Management And Synchronization

Synchronization. Race Condition. The Critical-Section Problem Solution. The Synchronization Problem. Typical Process P i. Peterson s Solution

Midterm Exam. October 20th, Thursday NSC

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

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

Synchronization. Disclaimer: some slides are adopted from the book authors slides 1

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

Synchronization for Concurrent Tasks

IT 540 Operating Systems ECE519 Advanced Operating Systems

Introduction to OS Synchronization MOS 2.3

CS370 Operating Systems

CS420: Operating Systems. Process Synchronization

Resource management. Real-Time Systems. Resource management. Resource management

IV. Process Synchronisation

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

CS Advanced Operating Systems Structures and Implementation Lecture 8. Synchronization Continued. Goals for Today. Synchronization Scheduling

Background. The Critical-Section Problem Synchronisation Hardware Inefficient Spinning Semaphores Semaphore Examples Scheduling.

Synchronization. Disclaimer: some slides are adopted from the book authors slides with permission 1

Lecture 6. Process Synchronization

Concurrency. On multiprocessors, several threads can execute simultaneously, one on each processor.

Real-Time Systems. Lecture #4. Professor Jan Jonsson. Department of Computer Science and Engineering Chalmers University of Technology

Lecture 6 (cont.): Semaphores and Monitors

EECS 482 Introduction to Operating Systems

Reminder from last time

M4 Parallelism. Implementation of Locks Cache Coherence

Concurrency and Synchronisation

Process & Thread Management II. Queues. Sleep() and Sleep Queues CIS 657

Process & Thread Management II CIS 657

CS162 Operating Systems and Systems Programming Midterm Review"

Recap. Mutual exclusion. Peterson s algorithm. Synchronization instructions. s/w solution Assume program order execution. Test&set

Concurrency & Synchronization. COMPSCI210 Recitation 25th Feb 2013 Vamsi Thummala Slides adapted from Landon Cox

CS3733: Operating Systems

Process Synchronization: Semaphores. CSSE 332 Operating Systems Rose-Hulman Institute of Technology

Semaphores and Monitors: High-level Synchronization Constructs

Locks. Dongkun Shin, SKKU

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2002

Page 1. CS162 Operating Systems and Systems Programming Lecture 8. Readers-Writers Language Support for Synchronization

Critical Section Problem: Example

Synchronization I. Jo, Heeseung

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

Synchronization Principles

Mutex Implementation

Page 1. CS194-3/CS16x Introduction to Systems. Lecture 6. Synchronization primitives, Semaphores, Overview of ACID.

Dealing with Issues for Interprocess Communication

CS Advanced Operating Systems Structures and Implementation Lecture 8. Synchronization Continued. Critical Section.

Process/Thread Synchronization

CMSC421: Principles of Operating Systems

Synchronization II: EventBarrier, Monitor, and a Semaphore. COMPSCI210 Recitation 4th Mar 2013 Vamsi Thummala

Recap: Thread. What is it? What does it need (thread private)? What for? How to implement? Independent flow of control. Stack

Background. Old Producer Process Code. Improving the Bounded Buffer. Old Consumer Process Code

Synchronization. CISC3595/5595 Fall 2015 Fordham Univ.

Chapter 6: Process Synchronization

Learning Outcomes. Concurrency and Synchronisation. Textbook. Concurrency Example. Inter- Thread and Process Communication. Sections & 2.

Synchronization. Before We Begin. Synchronization. Example of a Race Condition. CSE 120: Principles of Operating Systems. Lecture 4.

Process Synchronization

CS162 Operating Systems and Systems Programming Lecture 7. Synchronization (Continued) Recall: How does Thread get started?

CS 333 Introduction to Operating Systems. Class 3 Threads & Concurrency. Jonathan Walpole Computer Science Portland State University

ENGR 3950U / CSCI 3020U UOIT, Fall 2012 Quiz on Process Synchronization SOLUTIONS

Transcription:

Last Class: Synchronization Synchronization primitives are required to ensure that only one thread executes in a critical section at a time. Concurrent programs Low-level atomic operations (hardware) load/store interrupt disable test&set High-level atomic operations (software) lock monitors semaphore send & receive Lecture 9, page 1 Today: Synchronization: Locks and Semaphores More on hardware support for synchronization Implementing locks using test&set and busy waiting What are semaphores? Semaphores are basically generalized locks. Like locks, semaphores are a special type of variable that supports two atomic operations and offers elegant solutions to synchronization problems. They were invented by Dijkstra in 1965. Lecture 9, page 2 1

Atomic read-modify-write Instructions Atomic read-modify-write instructions atomically read a value from memory into a register and write a new value. Straightforward to implement simply by adding a new instruction on a uniprocessor. On a multiprocessor, the processor issuing the instruction must also be able to invalidate any copies of the value the other processes may have in their cache, i.e., the multiprocessor must support some type of cache coherence. Examples: Test&Set: (most architectures) read a value, write 1 back to memory. Exchange: (x86) swaps value between register and memory. Compare&Swap: (68000) read value, if value matches register value r1, exchange register r2 and value. Lecture 9, page 3 Implementing Locks with Test&Set Test&Set: reads a value, writes 1 to memory, and returns the old value. class Lock { Lock::Acquire() { public: // if busy do nothing void Acquire(); while (test&set(value) == 1); void Release(); private: Lock::Release() { int value; value = 0; Lock::Lock { value = 0; If lock is free (value = 0), test&set reads 0, sets value to 1, and returns 0. The Lock is now busy: the test in the while fails, and Acquire is complete. If lock is busy (value = 1), test&set reads 1, sets value to 1, and returns 1. The while continues to loop until a Release executes. Lecture 9, page 4 2

Busy Waiting Lock::Acquire(){ //if Busy, do nothing while (test&set(value) == 1); What's wrong with the above implementation? What is the CPU doing? What could happen to threads with different priorities? How can we get the waiting thread to give up the processor, so the releasing thread can execute? Lecture 9, page 5 Locks using Test&Set with minimal busy-waiting Can we implement locks with test&set without any busy-waiting or disabling interrupts? No, but we can minimize busy-waiting time by atomically checking the lock value and giving up the CPU if the lock is busy class Lock { // same declarations as earlier private int guard; Lock::Acquire(T:Thread) { while (test&set(guard) == 1) ; if (value!= FREE) { put T on Q; T->Sleep() & set guard = 0; else { value = BUSY; guard = 0; Lock::Release() { // busy wait while (test&set(guard) == 1) ; if Q is not empty { take T off Q; put T on ready queue; else { value = FREE; guard = 0; Lecture 9, page 6 3

Semaphores Semaphore: an integer variable that can be updated only using two special atomic instructions. Binary (or Mutex) Semaphore: (same as a lock) Guarantees mutually exclusive access to a resource (only one process is in the critical section at a time). Can vary from 0 to 1 It is initialized to free (value = 1) Counting Semaphore: Useful when multiple units of a resource are available The initial count to which the semaphore is initialized is usually the number of resources. A process can acquire access so long as at least one unit of the resource is available Lecture 9, page 7 Semaphores: Key Concepts Like locks, a semaphore supports two atomic operations, Semaphore->Wait() and Semaphore->Signal(). S->Wait() // wait until semaphore S // is available <critical section> S->Signal() // signal to other processes // that semaphore S is free Each semaphore supports a queue of processes that are waiting to access the critical section (e.g., to buy milk). If a process executes S->Wait() and semaphore S is free (non-zero), it continues executing. If semaphore S is not free, the OS puts the process on the wait queue for semaphore S. A S->Signal() unblocks one process on semaphore S's wait queue. Lecture 9, page 8 4

Binary Semaphores: Example Too Much Milk using locks: Thread A Lock->Acquire(); if (nomilk){ buy milk; Thread B Lock->Acquire(); if (nomilk){ buy milk; Lock->Release(); Lock->Release(); Too Much Milk using semaphores: Thread A Thread B Semaphore->Wait(); Semaphore->Wait(); if (nomilk){ if (nomilk){ buy milk; buy milk; Semaphore->Signal(); Semaphore->Signal(); Lecture 9, page 9 Implementing Signal and Wait class Semaphore { public: void Wait(Process P); void Signal(); private: int value; Queue Q; // queue of processes; Semaphore::Semaphore(int val) { value = val; Q = empty; Semaphore::Wait(Process P) { value = value - 1; if (value < 0) { add P to Q; P->block(); Semaphore::Signal() { value = value + 1; if (value <= 0){ remove P from Q; wakeup(p); => Signal and Wait of course must be atomic! Lecture 9, page 10 5

Signal and Wait: Example P1: S->Wait(); S->Wait(); P2: S->Wait(); S->Signal(); S->Signal(); S->Signal(); Lecture 9, page 11 Signal and Wait: Example Lecture 9, page 12 6

Using Semaphores Mutual Exclusion: used to guard critical sections the semaphore has an initial value of 1 S->Wait() is called before the critical section, and S->Signal() is called after the critical section. Scheduling Constraints: used to express general scheduling constraints where threads must wait for some circumstance. The initial value of the semaphore is usually 0 in this case. Example: You can implement thread join (or the Unix system call waitpid(pid)) with semaphores: Semaphore S; S->value = 0; // semaphore initialization Thread::Join S->Wait(); Thread::Finish S->Signal(); Lecture 9, page 13 Multiple Consumers and Producers class BoundedBuffer { public: void Producer(); void Consumer(); private: Items *buffer; // control access to buffers Semaphore mutex; // count of free slots Semaphore empty; // count of used slots Semaphore full; BoundedBuffer::BoundedBuffer( int N){ mutex->value = 1; empty->value = N; full->value = 0; new buffer[n]; BoundedBuffer::Producer(){ <produce item> empty->wait(); // one fewer slot, or wait mutex->wait(); // get access to buffers <add item to buffer> mutex->signal(); // release buffers full->signal(); // one more used slot BoundedBuffer::Consumer(){ full->wait(); //wait until there's an item mutex->wait(); // get access to buffers <remove item from buffer> mutex->signal(); // release buffers empty->signal(); // one more free slot <use item> Lecture 9, page 14 7

Multiple Consumers and Producers Problem Lecture 9, page 15 Summary Locks can be implemented by disabling interrupts or busy waiting Semaphores are a generalization of locks Semaphores can be used for three purposes: To ensure mutually exclusive execution of a critical section (as locks do). To control access to a shared pool of resources (using a counting semaphore). To cause one thread to wait for a specific action to be signaled from another thread. Lecture 9, page 16 8