W4118 Operating Systems. Instructor: Junfeng Yang

Similar documents
Synchroniza+on II COMS W4118

Synchronization II. Today. ! Condition Variables! Semaphores! Monitors! and some classical problems Next time. ! Deadlocks

Synchronization II. q Condition Variables q Semaphores and monitors q Some classical problems q Next time: Deadlocks

Synchroniza+on II. COMS W4118 Prof. Kaustubh R. Joshi hfp://

Semaphores Semaphores: A Definition

Process Synchronization. studykorner.org

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Synchronization. Dr. Yingwu Zhu

Operating systems and concurrency (B08)

CS 470 Spring Mike Lam, Professor. Semaphores and Conditions

Week 3. Locks & Semaphores

Synchronization Primitives

CS 471 Operating Systems. Yue Cheng. George Mason University Fall 2017

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

Locks and semaphores. Johan Montelius KTH

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

Condition Variables CS 241. Prof. Brighten Godfrey. March 16, University of Illinois

[537] Concurrency Bugs. Tyler Harter

Lecture 8: September 30

recap, what s the problem Locks and semaphores Total Store Order Peterson s algorithm Johan Montelius 0 0 a = 1 b = 1 read b read a

CS 153 Lab6. Kishore Kumar Pusukuri

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Announcements. Class feedback for mid-course evaluations Receive about survey to fill out until this Friday

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

Semaphores. attention to the previous chapters. And even remembering a few things. You have, right?!

Final review. From threads to file systems

Note: The following (with modifications) is adapted from Silberschatz (our course textbook), Project: Producer-Consumer Problem.

Data Races and Deadlocks! (or The Dangers of Threading) CS449 Fall 2017

Process Synchronization

Locks and semaphores. Johan Montelius KTH

Synchronization Mechanisms

CSC Systems Programming Fall Lecture - XIV Concurrent Programming. Tevfik Ko!ar. Louisiana State University. November 2nd, 2010

Synchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1

[537] Semaphores. Tyler Harter

What's wrong with Semaphores?

Condition Variables. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

CS 537 Lecture 8 Monitors. Thread Join with Semaphores. Dining Philosophers. Parent thread. Child thread. Michael Swift

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

EECS 482 Introduction to Operating Systems

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Systems Programming/ C and UNIX

Plan. Demos. Next Project. CSCI [4 6]730 Operating Systems. Hardware Primitives. Process Synchronization Part II. Synchronization Part 2

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

CMSC421: Principles of Operating Systems

Solving the Producer Consumer Problem with PThreads

More Types of Synchronization 11/29/16

POSIX Threads. Paolo Burgio

Multithreading Programming II

Concurrent Programming Lecture 10

Synchronising Threads

C09: Process Synchronization

Lecture 6 (cont.): Semaphores and Monitors

Synchronization. CSCI 5103 Operating Systems. Semaphore Usage. Bounded-Buffer Problem With Semaphores. Monitors Other Approaches

real time operating systems course

KING FAHD UNIVERSITY OF PETROLEUM & MINERALS. Information and Computer Science Department. ICS 431 Operating Systems. Lab # 9.

Condition Variables. Dongkun Shin, SKKU

Concurrency: a crash course

Monitors (Deprecated)

Semaphores Semaphores: A Definition

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

Synchronization and Semaphores. Copyright : University of Illinois CS 241 Staff 1

Chapter 6: Process Synchronization

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

Operating Systems ECE344

Concurrent Processes Rab Nawaz Jadoon

Using Semaphores CS 241. March 14, University of Illinois

ANKARA UNIVERSITY COMPUTER ENGINEERING DEPARTMENT BLM334-COM334 PROJECT

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

Synchronization: Basics

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

Operating Systems. Thread Synchronization Primitives. Thomas Ropars.

Opera&ng Systems ECE344

Condition Variables. Dongkun Shin, SKKU

Concurrency Control. Synchronization. Brief Preview of Scheduling. Motivating Example. Motivating Example (Cont d) Interleaved Schedules

Process Synchronization (Part I)

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

High-level Synchronization

Warm-up question (CS 261 review) What is the primary difference between processes and threads from a developer s perspective?

CS4961 Parallel Programming. Lecture 12: Advanced Synchronization (Pthreads) 10/4/11. Administrative. Mary Hall October 4, 2011

Lecture 6. Process Synchronization

Real Time Operating Systems and Middleware

Pre-lab #2 tutorial. ECE 254 Operating Systems and Systems Programming. May 24, 2012

Operating Systems (2INC0) 2017/18

Concurrency. Chapter 5

Motivation and definitions Processes Threads Synchronization constructs Speedup issues

Reminder from last time

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

CS-345 Operating Systems. Tutorial 2: Grocer-Client Threads, Shared Memory, Synchronization

ANSI/IEEE POSIX Standard Thread management

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

Operating Systems, Assignment 2 Threads and Synchronization

More Synchronization; Concurrency in Java. CS 475, Spring 2018 Concurrent & Distributed Systems

CSE 153 Design of Operating Systems

Synchronization: Basics

Synchronization 1. Synchronization

Lecture 9: Thread Synchronizations. Spring 2016 Jason Tang

Condition Variables. parent: begin child parent: end

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

Transcription:

W4118 Operating Systems Instructor: Junfeng Yang

Outline Semaphores Producer-consumer problem Monitors and condition variables 2

Semaphore motivation Problem with lock: mutual exclusion, but no ordering Producer-consumer problem: need order $ cat 1.txt sort uniq wc Producer: creates a resource Consumer: uses a resource bounded buffer between them Scheduling order: producer waits if buffer full, consumer waits if buffer empty 3

Semaphore definition A synchronization variable that: Contains an integer value Can t access directly Must initialize to some value sem_init (sem_t *s, int pshared, unsigned int value) Has two operations to manipulate this integer sem_wait (or down(), P()) sem_post (or up(), V()) int sem_wait(sem_t *s) { wait until value of semaphore s is greater than 0 decrement the value of semaphore s by 1 int sem_post(sem_t *s) { increment the value of semaphore s by 1 if there are 1 or more threads waiting, wake 1 4

Semaphore uses Mutual exclusion Semaphore as mutex Binary semaphore: X=1 // initialize to X sem_init(s, 0, X) sem_wait(s); // critical section sem_post(s); Mutual exclusion with more than one resources Counting semaphore: X>1 5

Semaphore uses (cont.) Scheduling order One thread waits for another What should initial value be? //thread 0 // 1 st half of computation sem_post(s); // thread 1 sem_wait(s); //2 nd half of computation 6

How to implement semaphores? Homework! 7

Outline Semaphores Producer-consumer problem Monitors and condition variables 8

Producer-Consumer (Bounded-Buffer) Problem Bounded buffer: size N, Access entry 0 N-1, then wrap around to 0 again Producer process writes data to buffer Consumer process reads data from buffer Order constraints Producer shouldn t try to produce if buffer is full Consumer shouldn t try to consume if buffer is empty 0 1 N-1 Producer Consumer 9

Solving Producer-Consumer problem Two semaphores sem_t full; // # of filled slots sem_t empty; // # of empty slots What should initial values be? Problem: mutual exclusion? sem_init(&full, 0, X); sem_init(&empty, 0, Y); producer() { sem_wait(empty); // fill a slot sem_post(full); consumer() { sem_wait(full); // empty a slot sem_post(empty); 10

Solving Producer-Consumer problem: final Three semaphores sem_t full; // # of filled slots sem_t empty; // # of empty slots sem_t mutex; // mutual exclusion sem_init(&full, 0, 0); sem_init(&empty, 0, N); sem_init(&mutex, 0, 1); producer() { sem_wait(empty); sem_wait(&mutex); // fill a slot sem_post(&mutex); sem_post(full); consumer() { sem_wait(full); sem_wait(&mutex); // empty a slot sem_post(&mutex); sem_post(empty); 11

Outline Semaphores Producer-consumer problem Monitors and condition variables 12

Monitors Background: concurrent programming meets objectoriented programming When concurrent programming became a big deal, objectoriented programming too People started to think about ways to make concurrent programming more structured Monitor: object with a set of monitor procedures and only one thread may be active (i.e. running one of the monitor procedures) at a time 13

Schematic view of a monitor Can think of a monitor as one big lock for a set of operations/ methods In other words, a language implementation of mutexes 14

How to implement monitor? Compiler automatically inserts lock and unlock operations upon entry and exit of monitor procedures class account { int balance; public synchronized void deposit() { ++balance; public synchronized void withdraw() { --balance; ; lock(this.m); ++balance; unlock(this.m); lock(this.m); --balance; unlock(this.m); 15

Condition Variables Need wait and wakeup as in semaphores Monitor uses Condition Variables Conceptually associated with some conditions Operations on condition variables: wait(): suspends the calling thread and releases the monitor lock. When it resumes, reacquire the lock. Called when condition is not true signal(): resumes one thread waiting in wait() if any. Called when condition becomes true and wants to wake up one waiting thread broadcast(): resumes all threads waiting in wait(). Called when condition becomes true and wants to wake up all waiting threads 16

Monitor with condition variables 17

Subtle difference between condition variables and semaphores Semaphores are sticky: they have memory, sem_post() will increment the semaphore, even if no one has called sem_wait() Condition variables are not: if no one is waiting for a signal(), this signal() is not saved Despite the difference, they are as powerful 18

Producer-consumer with monitors monitor ProducerConsumer { int nfull = 0; cond has_empty, has_full; ; producer() { if (nfull == N) wait (has_empty); // fill a slot ++ nfull; signal (has_full); consumer() { if (nfull == 0) wait (has_full); // empty a slot -- nfull; signal (has_empty); Two condition variables has_empty: at least one slot is empty has_full: at least one slot is full nfull: number of filled slots Need to do our own counting for condition variables 19

Condition variable semantics Design question: when signal() wakes up a waiting thread, which thread to run inside the monitor, the signaling thread, or the waiting thread? Hoare semantics: suspends the signaling thread, and immediately transfers control to the woken thread Difficult to implement in practice Mesa semantics: signal() moves a single waiting thread from the blocked state to a runnable state, then the signaling thread continues until it exits the monitor Easy to implement Problem: race! Before a woken consumer continues, another consumer comes in and grabs the buffer 20

Fixing the race in mesa monitors monitor ProducerConsumer { int nfull = 0; cond has_empty, has_full; ; producer() { while (nfull == N) wait (has_empty); // fill slot ++ nfull; signal (has_full); consumer() { while (nfull == 0) wait (has_full); // empty slot -- nfull signal (has_empty); The fix: when woken, a thread must recheck the condition it was waiting on Most systems use mesa semantics 21

Monitor with pthread class ProducerConsumer { int nfull = 0; pthread_mutex_t m; pthread_cond_t has_empty, has_full; C/C++ don t provide monitors; but we can implement monitors using pthread mutex and condition variable public: producer() { pthread_mutex_lock(&m); while (nfull == N) ; ptherad_cond_wait (&has_empty, &m); // fill slot ++ nfull; pthread_cond_signal (has_full); pthread_mutex_unlock(&m); For producer-consumer problem, need 1 pthread mutex and 2 pthread condition variables (pthread_cond_t) Manually lock and unlock mutex for monitor procedures pthread_cond_wait (cv, m): atomically waits on cv and releases m 22