CS 153 Design of Operating Systems Winter 2016

Similar documents
CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo

Lecture 6 (cont.): Semaphores and Monitors

CS 153 Design of Operating Systems Winter 2016

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

Operating Systems ECE344

CS 318 Principles of Operating Systems

Opera&ng Systems ECE344

Operating Systems. Operating Systems Summer 2017 Sina Meraji U of T

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

CSE 120 Principles of Operating Systems Spring 2016

Dealing with Issues for Interprocess Communication

Lecture 5: Synchronization w/locks

CS 318 Principles of Operating Systems

Synchronization. CSE 2431: Introduction to Operating Systems Reading: Chapter 5, [OSC] (except Section 5.10)

CSE 120 Principles of Operating Systems Spring 2016

Concurrency and Synchronisation

CENG334 Introduction to Operating Systems

Concurrency and Synchronisation

Synchronization. CS 416: Operating Systems Design, Spring 2011 Department of Computer Science Rutgers University

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

CS 333 Introduction to Operating Systems. Class 4 Concurrent Programming and Synchronization Primitives

Operating Systems. Synchronization

PROCESS SYNCHRONIZATION

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

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

CS 153 Design of Operating Systems Spring 18

Synchronization. Dr. Yingwu Zhu

Synchronization I. Jo, Heeseung

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

Semaphores (by Dijkstra)

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

Process Synchronization Mechanisms

CS 333 Introduction to Operating Systems Class 4 Concurrent Programming and Synchronization Primitives

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

CSE 451: Operating Systems Winter Synchronization. Gary Kimura

Last Class: Synchronization

EECS 482 Introduction to Operating Systems

Synchronization Basic Problem:

Lesson 6: Process Synchronization

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

Process Synchronization(2)

Synchronization Classic Problems

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

IV. Process Synchronisation

Concept of a process

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

CS370 Operating Systems

Synchronization I. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

High-level Synchronization

CS420: Operating Systems. Process Synchronization

Threads. Threads The Thread Model (1) CSCE 351: Operating System Kernels Witawas Srisa-an Chapter 4-5

EECS 482 Introduction to Operating Systems

CS3733: Operating Systems

Process Synchronization(2)

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

Lecture Outline. CS 5523 Operating Systems: Concurrency and Synchronization. a = 0; b = 0; // Initial state Thread 1. Objectives.

CSE 4/521 Introduction to Operating Systems

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

7: Interprocess Communication

Introduction to Operating Systems

Chapter 5: Process Synchronization

SYNCHRONIZATION M O D E R N O P E R A T I N G S Y S T E M S R E A D 2. 3 E X C E P T A N D S P R I N G 2018

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

CS370 Operating Systems

Process Synchronization(2)

Lecture. DM510 - Operating Systems, Weekly Notes, Week 11/12, 2018

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

Process Synchronization

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Operating Systems. User OS. Kernel & Device Drivers. Interface Programs. Interprocess Communication (IPC)

Synchronization. Peter J. Denning CS471/CS571. Copyright 2001, by Peter Denning

Interprocess Communication and Synchronization

CS370 Operating Systems

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

Introduction to OS Synchronization MOS 2.3

Synchronization Spinlocks - Semaphores

Advanced Operating Systems (CS 202) Memory Consistency, Cache Coherence and Synchronization

C09: Process Synchronization

Semaphores and Monitors: High-level Synchronization Constructs

Reminder from last time

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

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

Synchronization. Heechul Yun. Disclaimer: some slides are adopted from the book authors and Dr. Kulkani

Systèmes d Exploitation Avancés

CSE 153 Design of Operating Systems

10/17/ Gribble, Lazowska, Levy, Zahorjan 2. 10/17/ Gribble, Lazowska, Levy, Zahorjan 4

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

Synchronization: semaphores and some more stuff. Operating Systems, Spring 2018, I. Dinur, D. Hendler and R. Iakobashvili

Chapter 5: Process Synchronization

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

Chapter 6: Process Synchronization

What's wrong with Semaphores?

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

CSC501 Operating Systems Principles. Process Synchronization

Synchronization for Concurrent Tasks

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

Chapter 6: Process Synchronization

Concurrency. Chapter 5

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

Transcription:

CS 153 Design of Operating Systems Winter 2016 Lecture 9: Semaphores and Monitors Some slides from Matt Welsh

Summarize Where We Are Goal: Use mutual exclusion to protect critical sections of code that access shared resources Method: Use locks (spinlocks or disable interrupts) Problem: Critical sections can be long Spinlocks: Threads waiting to acquire lock spin in test-and-set loop Wastes CPU cycles Longer the CS, the longer the spin Greater the chance for lock holder to be interrupted acquire(lock) Critical section release(lock) Disabling Interrupts: Should not disable interrupts for long periods of time Can miss or delay important events (e.g., timer, I/O) 2

Implementing Locks (4) -- (no spin) struct lock { int held = 0; void acquire (lock) { while (test-and-set(&lock->held)) thread_yield(); void release (lock) { lock->held = 0; 3

Implementing Locks (4) -- (no spin) mutex_lock: TSL REGISTER, MUTEX copy mutex to register, set mutex to 1 CMP REGISTER, #0 was mutex zero? JNE ok if zero, mutex was unlocked, so return CALL thread_yield mutex busy, schedule another thread JMP mutex_lock try again later ok: RET return to caller; CR entered mutex_unlock: MOVE MUTEX, #0 store a 0 in mutex RET return to caller 4

Implementing Locks (5) -- Mutex (true blocking) System-wide struct lock { int held = 0; void acquire (lock) { if(test-and-set(&lock->held)) // block the thread; // send it to a waiting queue void release (lock) { lock->held = 0; // move on thread from the waiting // queue to ready queue 5

Higher-level synchronization primitives We have looked at one synchronization primitive: locks Locks are useful, but may not satisfy all program needs Examples? Reader/Writer problem Say we had a shared variable where we wanted any number of threads to read the variable, but only one thread to write it. How would you do this with locks? What's wrong with this code? Reader() { lock.acquire(); local_copy = shared_var; lock.release(); return local_copy; Writer() { lock.acquire(); shared_var = NEW_VALUE; lock.release(); 6

Semaphores Semaphores are an abstract data type that provide mutual exclusion to critical sections Block waiters, interrupts enabled within critical section Described by Dijkstra in THE system in 1968 Semaphores are integers that support two operations: wait(semaphore): decrement, block until semaphore is open» Also P(), after the Dutch word for test, or down() signal(semaphore): increment, allow another thread to enter» Also V() after the Dutch word for increment, or up() That's it! No other operations not even just reading its value exist Semaphore safety property: the semaphore value is always greater than or equal to 0 7

Blocking in Semaphores Associated with each semaphore is a queue of waiting threads/processes When wait() is called by a thread: If semaphore is open, thread continues If semaphore is closed, thread blocks on queue Then signal() opens the semaphore: If a thread is waiting on the queue, the thread is unblocked If no threads are waiting on the queue, the signal is remembered for the next thread 8

Semaphore Types Semaphores come in two types Mutex semaphore (or binary semaphore) Represents single access to a resource Guarantees mutual exclusion to a critical section Counting semaphore (or general semaphore) Multiple threads pass the semaphore determined by count»mutex has count = 1, counting has count = N Represents a resource with many units available or a resource allowing some unsynchronized concurrent access (e.g., reading) 9

Using Semaphores Use is similar to our locks, but semantics are different struct Semaphore { int value; Queue q; S; withdraw (account, amount) { wait(s); balance = get_balance(account); balance = balance amount; put_balance(account, balance); signal(s); return balance; Threads block critical section wait(s); balance = get_balance(account); balance = balance amount; wait(s); wait(s); put_balance(account, balance); signal(s); signal(s); It is undefined which thread runs after a signal signal(s); 10

Using Semaphores We ve looked at a simple example for using synchronization Mutual exclusion while accessing a bank account Now we re going to use semaphores to look at more interesting examples Producer consumer with bounded buffers Readers/Writers 11

Producer-Consumer Problem / Bounded Buffer Problem: Producer puts things into a shared buffer Consumer takes them out Need synchronization for coordinating producer and consumer Example Coke machine 12

Bounded Buffer Producer Consumer Problem: There is a set of resource buffers shared by producer and consumer threads Producer inserts resources into the buffer set» Output, disk blocks, memory pages, processes, etc Buffer between producer and consumer allows them to operate somewhat independently (execute at different rates) Otherwise must operate in lockstep producer puts 1 thing in buffer, then consumer takes it out then producer adds another, then consumer takes it out, etc What is desired safety property? Sequence of consumed values is prefix of sequence of produced values If nc is number consumed, np number produced, and N the size of the buffer, then 0 np nc N 13

First Try: Sleep and Wakeup #define N 100 int count=0; //# of slots in the buffer //# of items in the buffer producer { while(true) { // produce new item if (count==n) sleep(inf); // wait for buffer // insert item count=count+1; if(count==1) // just filled an empty buffer wakeup(consumer); consumer { while(true) { if (count==0) sleep(inf); // no more item // remove item count=count-1; if(count==n-1) // have spaces now wakeup(producer); // consume resource; 15

What are the problems? Producer-consumer problem with fatal race condition Access to count is a race condition Access to buffer is a race condition Wakeup call could get lost count = count + 1; count = count 1; mov eax, count inc eax mov count, eax mov eax, count dec eax mov count, eax Obviously, we need synchronization! 16

Second Try: Mutual Exclusion #define N 100 //# of slots in the buffer int count=0; //# of items in the buffer Semaphore mutex = 1; // mutual exclusion producer { while(true) { // produce new item wait(mutex); // lock for shared data access if (count==n) sleep(inf); // wait for buffer // insert item count=count+1; if(count==1) // just filled an empty buffer wakeup(consumer); signal(mutex); // unlock consumer { while(true) { wait(mutex); // lock for shared data access if (count==0) sleep(inf); // no more item // remove item count=count-1; if(count==n-1) // have spaces now wakeup(producer); signal(mutex); // unlock // consume resource; 17

Bounded Buffer (2) 0 np nc N Use three semaphores: filled count of filled buffers» Counting semaphore» filled =? (np nc) empty count of empty buffers» Counting semaphore» empty =? N - (np nc) mutex mutual exclusion to shared set of buffers»binary semaphore 19

Last Try: Semaphores Semaphore mutex = 1; // mutual exclusion to shared buffer Semaphore empty = N; // count of empty buffer slots (all empty to start) Semaphore filled = 0; // count of filled buffer slots (none to start) producer { while (1) { Produce new resource; wait(empty); // wait for empty slot wait(mutex); // lock buffer list Add resource to an empty slot; signal(mutex); // unlock buffer list signal(filled); // note a filled slot consumer { while (1) { wait(filled); // wait for a filled slot wait(mutex); // lock buffer list Remove resource from a filled slot; signal(mutex); // unlock buffer list signal(empty); // note an empty slot Consume resource; 20

Bounded Buffer (4) Why need the mutex at all? The pattern of signal/wait on full/empty is a common construct often called an interlock Producer-Consumer and Bounded Buffer are classic examples of synchronization problems 21

Next time Scheduling Read Chapter 6 22