EECS 482 Introduction to Operating Systems

Similar documents
EECS 482 Introduction to Operating Systems

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

Threads and concurrency

Threads and concurrency

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

CS 153 Design of Operating Systems Winter 2016

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 "

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

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

PROCESS SYNCHRONIZATION

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

Lecture 8: September 30

Semaphores and Monitors: High-level Synchronization Constructs

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

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

Last Class: Synchronization

Models of concurrency & synchronization algorithms

Synchronization for Concurrent Tasks

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

More Types of Synchronization 11/29/16

The University of Texas at Arlington

CS 31: Intro to Systems Misc. Threading. Kevin Webb Swarthmore College December 6, 2018

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

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

Chapter 2 Processes and Threads. Interprocess Communication Race Conditions

Concurrency: a crash course

EECS 482 Introduction to Operating Systems

Introduction to Operating Systems Prof. Chester Rebeiro Department of Computer Science and Engineering Indian Institute of Technology, Madras

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

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

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

Review: Processes. A process is an instance of a running program. POSIX Thread APIs:

2.c Concurrency Mutual exclusion & synchronization mutexes. Unbounded buffer, 1 producer, N consumers

Interprocess Communication and Synchronization

Semaphores. Blocking in semaphores. Two types of semaphores. Example: Bounded buffer problem. Binary semaphore usage

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

EECS 482 Introduction to Operating Systems

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

C09: Process Synchronization

The University of Texas at Arlington

Process Management And Synchronization

CSE 451: Operating Systems Spring Module 10 Semaphores, Condition Variables, and Monitors

Condition Variables & Semaphores

W4118 Operating Systems. Instructor: Junfeng Yang

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

Today: Synchronization. Recap: Synchronization

Synchroniza+on II COMS W4118

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

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

EECS 482 Introduction to Operating Systems

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

Reminder from last time

Monitors & Condition Synchronization

Chapter 6: Process Synchronization

CS3502 OPERATING SYSTEMS

Locks and Condition Variables Recap. Introducing Monitors. Hoare Monitors: Semantics. Coke Machine Example. Locks. Condition variables

Midterm Exam. October 20th, Thursday NSC

EECS 482 Introduction to Operating Systems

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

What's wrong with Semaphores?

Dealing with Issues for Interprocess Communication

Student: Yu Cheng (Jade) ICS 412 Homework #3 October 07, Explain why a spin-lock is not a good idea on a single processor, single machine.

Chapter 5 Concurrency: Mutual Exclusion and Synchronization

Synchronising Threads

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

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo

Systèmes d Exploitation Avancés

Operating Systems ECE344

CIS Operating Systems Application of Semaphores. Professor Qiang Zeng Spring 2018

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

High-level Synchronization

CS162 Operating Systems and Systems Programming Midterm Review"

CSE 120 Principles of Operating Systems Spring 2016

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

What is the Race Condition? And what is its solution? What is a critical section? And what is the critical section problem?

Last Class: Deadlocks. Today

Chapter 5 Concurrency: Mutual Exclusion. and. Synchronization. Operating Systems: Internals. and. Design Principles

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

CSCE Operating Systems Synchronization: Producer-Consumer Problem. Qiang Zeng, Ph.D. Fall 2018

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

Introduction to Operating Systems

CSCI 447 Operating Systems Filip Jagodzinski

9/29/2014. CS341: Operating System Mid Semester Model Solution Uploaded Semaphore ADT: wait(), signal()

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

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

Concurrency - II. Recitation 3/24 Nisarg Raval Slides by Prof. Landon Cox and Vamsi Thummala

IV. Process Synchronisation

Concept of a process

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

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

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

Concurrency. Chapter 5

Synchronization Classic Problems

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Last Class: Monitors. Real-world Examples

Remaining Contemplation Questions

CS 550 Operating Systems Spring Concurrency Semaphores, Condition Variables, Producer Consumer Problem

Deadlock. Concurrency: Deadlock and Starvation. Reusable Resources

Semaphores (by Dijkstra)

Transcription:

EECS 482 Introduction to Operating Systems Fall 2017 Manos Kapritsos Slides by: Harsha V. Madhyastha

Implementing reader-writer locks with monitors Shared data needed to implement readerstart, readerfinish, writerstart, writerfinish? numreaders numwriters Use one lock (rwlock) Condition variables? waitingreaders: readers must wait if there are writers waitingwriters: writers must wait if there are readers or writers September 21, 2017 EECS 482 Lecture 6 2

Implementing reader-writer locks with monitors readerstart () { rwlock.lock() while (numwriters > 0) { waitingreaders.wait() numreaders++ rwlock.unlock() readerfinish() { rwlock.lock() numreaders-- if (numreaders == 0) { waitingwriters.signal() rwlock.unlock() writerstart() { rwlock.lock() while (numreaders > 0 numwriters > 0) { waitingwriters.wait() numwriters++ rwlock.unlock() writerfinish() { rwlock.lock() numwriters-- waitingreaders.broadcast() waitingwriters.signal() rwlock.unlock() September 21, 2017 EECS 482 Lecture 6 3

Implementing reader-writer locks with monitors readerstart () { rwlock.lock() while (numwriters > 0) { waitingreaders.wait() numreaders++ rwlock.unlock() readerfinish() { rwlock.lock() if (numreaders == 1) { waitingwriters.signal() numreaders-- rwlock.unlock() writerstart() { rwlock.lock() while (numreaders > 0 numwriters > 0) { waitingwriters.wait() numwriters++ rwlock.unlock() writerfinish() { rwlock.lock() numwriters-- waitingreaders.broadcast() waitingwriters.signal() rwlock.unlock() September 21, 2017 EECS 482 Lecture 6 4

Implementing reader-writer locks with monitors readerstart () { rwlock.lock() while (numwriters > 0) { waitingreaders.wait() numreaders++ rwlock.unlock() readerfinish() { rwlock.lock() numreaders-- if (numreaders == 0) { waitingwriters.signal() rwlock.unlock() writerstart() { rwlock.lock() while (numreaders > 0 numwriters > 0) { waitingwriters.wait() numwriters++ rwlock.unlock() writerfinish() { rwlock.lock() numwriters-- waitingreaders.broadcast() waitingwriters.signal() rwlock.unlock() September 21, 2017 EECS 482 Lecture 6 5

Implementing reader-writer locks with monitors What will happen if a writer finishes and there are several waiting readers and writers? How long will a writer wait? September 21, 2017 EECS 482 Lecture 6 6

Recap Multi-threaded code must synchronize access to shared data High-level synchronization primitives: Locks: Mutual exclusion Condition variables: Ordering constraints Monitors: Lock + condition variables Today: Semaphores September 21, 2017 EECS 482 Lecture 6 7

Semaphores Generalized lock/unlock Definition: A non-negative integer (initialized to user-specified value) down(): wait for semaphore value to become positive, then atomically decrement semaphore value by 1 do { if (value > 0) { value-- break while (1) Atomic up(): increment semaphore value by 1 September 21, 2017 EECS 482 Lecture 6 8

Two types of semaphores Mutex semaphore (or binary semaphore) Represents single resource (critical section) Guarantees mutual exclusion Counting semaphore (or general semaphore) Represents a resource with many units, or a resource that allows concurrent access (e.g., reading) Multiple threads can hold the semaphore» Number determined by the semaphore count September 21, 2017 EECS 482 Lecture 6 9

Benefit of Semaphores Mutual exclusion Initial value is 1 down() critical section up() Ordering constraints Usually, initial value is 0 Example: thread A wants to wait for thread B to finish Thread A down() continue execution Thread B do task up() September 21, 2017 EECS 482 Lecture 6 10

Implementing producerconsumer with semaphores Semaphore assignments mutex: ensures mutual exclusion around code that manipulates coke machine fullslots: counts no. of full slots in the coke machine emptyslots: counts no. of empty slots in machine Initialization values? September 21, 2017 EECS 482 Lecture 6 11

Implementing producerconsumer with semaphores Semaphore mutex = 1; // mutual exclusion to shared set of slots Semaphore emptyslots = N; // count of empty slots (all empty to start) Semaphore fullslots = 0; // count of full slots (none full to start) producer { // wait for empty slot emptyslots.down(); mutex.down(); Add coke to the machine mutex.up(); consumer { // wait for full slot fullslots.down(); mutex.down(); Take coke out of machine mutex.up(); // note a full slot fullslots.up(); // note an empty slot emptyslots.up(); September 21, 2017 EECS 482 Lecture 6 12

Implementing producerconsumer with semaphores Why do we need different semaphores for fullslots and emptyslots? Does the order of down() matter? Does the order of up() matter? What if there s 1 full slot, and multiple consumers call down() at the same time? What if a context switch happens between emptyslots.down() and mutex.down()? What if fullslots.up() before mutex.down()? September 21, 2017 EECS 482 Lecture 6 13

Reminders Group declaration due today Project 1 due on Tuesday Work through discussion questions about monitors before tomorrow s discussion section September 21, 2017 EECS 482 Lecture 6 14

Comparing monitors and semaphores Semaphores provide 1 mechanism that can accomplish both mutual exclusion and ordering (monitors use different mechanisms for each) Elegant Can be difficult to use Monitor lock = binary semaphore (initialized to 1) lock() = down() unlock() = up() September 21, 2017 EECS 482 Lecture 6 15

Condition variable versus semaphore Condition variable while(!cond) {wait(); Can safely handle spurious wakeups Conditional code in user program User writes customized condition; more flexible User provides shared variable; protects with lock No memory of past signals Semaphore down() No spurious wakeups Conditional code in semaphore definition Condition specified by semaphore definition (wait if value == 0) Semaphore provides shared variable (integer) and thread-safe operations on that variable (down, up) Remembers past up calls September 21, 2017 EECS 482 Lecture 6 16

Condition variable versus semaphore Condition variable while(!cond) {wait(); Can safely handle spurious wakeups Conditional code in user program T1: wait() T2: signal() User writes customized T3: signal() condition; more flexiblet4: wait() User provides shared variable; protects with lock No memory of past signals Semaphore down() No spurious wakeups Conditional code in semaphore T1: down() definition T2: up() Condition T3: specified up() by semaphore definition (wait T4: down() if value == 0) Semaphore provides shared variable (integer) and thread-safe operations on that variable (down, up) Remembers past up calls September 21, 2017 EECS 482 Lecture 6 17

Implementing custom waiting condition with semaphores Semaphores work best if the shared integer and waiting condition (value==0) map naturally to problem domain How to implement custom waiting condition with semaphores? September 21, 2017 EECS 482 Lecture 6 18

Producer-consumer with monitors Consumer cokelock.lock() Producer cokelock.lock() while (numcokes == 0) { waitingconsumers.wait() while (numcokes == MAX) { waitingproducers.wait() take coke out of machine numcokes-- add coke to machine numcokes++ waitingproducers.signal() waitingconsumers.signal() cokelock.unlock() cokelock.unlock() September 21, 2017 EECS 482 Lecture 6 19

Producer-consumer with semaphores (monitor style) Consumer mutex.down() while (numcokes == 0) { Producer mutex.down() while (numcokes == MAX) { go to sleep go to sleep take coke out of machine numcokes add coke to machine numcokes++ wake up waiting producer, if any wake up waiting consumer, if any mutex.up() mutex.up() September 21, 2017 EECS 482 Lecture 6 20

Producer-consumer with semaphores (monitor style) Consumer mutex.down() while (numcokes == 0) { semaphore s = 0 waitingconsumers.push(&s) Producer mutex.down() while (numcokes == MAX) { semaphore s = 0 waitingproducers.push(&s) s.down() s.down() take coke out of machine numcokes if (!waitingproducers.empty()) { waitingproducers.front()->up() waitingproducers.pop() mutex.up() add coke to machine numcokes++ if (!waitingconsumers.empty()) { waitingconsumers.front()->up() waitingconsumers.pop() mutex.up() September 21, 2017 EECS 482 Lecture 6 21

Producer-consumer with semaphores (monitor style) Consumer mutex.down() while (numcokes == 0) { semaphore s = 0 waitingconsumers.push(&s) mutex.up() s.down() mutex.down() take coke out of machine numcokes if (!waitingproducers.empty()) { waitingproducers.front()->up() waitingproducers.pop() mutex.up() Producer mutex.down() while (numcokes == MAX) { semaphore s = 0 waitingproducers.push(&s) mutex.up() s.down() mutex.down() add coke to machine numcokes++ if (!waitingconsumers.empty()) { waitingconsumers.front()->up() waitingconsumers.pop() mutex.up() September 21, 2017 EECS 482 Lecture 6 22

The first commandment Would you name your kid Kid? Or KidA? Or MyKid? Or k? September 21, 2017 EECS 482 Lecture 6 23

The third commandment We gave you monitors so you don t have to worship the ancient gods! September 21, 2017 EECS 482 Lecture 6 24

The fifth commandment This is NOT OK: while(true) { mutex.lock() if(condition) { mutex.unlock() break; else { mutex.unlock() sleep(200); September 21, 2017 EECS 482 Lecture 6 25

The sixth commandment September 21, 2017 EECS 482 Lecture 6 26

The seventh commandment myatomicfunction() { mutex.lock() mutex.unlock() September 21, 2017 EECS 482 Lecture 6 27