Lecture 13 Condition Variables

Similar documents
Condition Variables. Dongkun Shin, SKKU

Condition Variables. Dongkun Shin, SKKU

Condition Variables. parent: begin child parent: end

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

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

Condition Variables & Semaphores

30. Condition Variables

Condition Variables. Figure 29.1: A Parent Waiting For Its Child

More Types of Synchronization 11/29/16

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

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

Today: Synchronization. Recap: Synchronization

Condition Variables. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Monitors (Deprecated)

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

CS 105, Spring 2007 Ring Buffer

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

Solving the Producer Consumer Problem with PThreads

CS 326: Operating Systems. Process Execution. Lecture 5

Concurrency, Thread. Dongkun Shin, SKKU

Chapter 6: Process Synchronization

CS 167 Midterm Exam. Two Hours; Closed Book; Please Use a Black Pen March 23, 2017

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

Vulkan: Scaling to Multiple Threads. Kevin sun Lead Developer Support Engineer, APAC PowerVR Graphics

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

Synchronization in Concurrent Programming. Amit Gupta

Motivation and definitions Processes Threads Synchronization constructs Speedup issues

CS-537: Midterm Exam (Fall 2013) Professor McFlub

Lecture #7: Implementing Mutual Exclusion

CS 105, Spring 2015 Ring Buffer

Synchroniza+on II COMS W4118

Shared Memory Programming. Parallel Programming Overview

W4118 Operating Systems. Instructor: Junfeng Yang

[537] Semaphores. Tyler Harter

Threads. Concurrency. What it is. Lecture Notes Week 2. Figure 1: Multi-Threading. Figure 2: Multi-Threading

Reminder from last time

Copyright 2013 Thomas W. Doeppner. IX 1

Synchronising Threads

CS-537: Midterm Exam (Fall 2013) Professor McFlub: The Solutions Edition

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

What's wrong with Semaphores?

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

COMP 3430 Robert Guderian

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

Synchronization 1. Synchronization

5. Synchronization. Operating System Concepts with Java 8th Edition Silberschatz, Galvin and Gagn

Locks and semaphores. Johan Montelius KTH

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

Interprocess Communication and Synchronization

Last Class: Deadlocks. Today

Learning from Bad Examples. CSCI 5828: Foundations of Software Engineering Lecture 25 11/18/2014

Capriccio : Scalable Threads for Internet Services

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

CS533 Concepts of Operating Systems. Jonathan Walpole

TCSS 422: OPERATING SYSTEMS

CS 318 Principles of Operating Systems

COMP 3430 Robert Guderian

Synchronization I. Jo, Heeseung

., --t - QJ. ., n. - r-+ r- (v. ::r. !:t. ... c.. c.. V> c.. c.. ::J ::J ::J. ~ ::::s. ::::s \I\ ([) QJ (D -. l/')

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

This exam paper contains 8 questions (12 pages) Total 100 points. Please put your official name and NOT your assumed name. First Name: Last Name:

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

Operating Systems (2INC0) 2017/18

High Performance Computing Lecture 21. Matthew Jacob Indian Institute of Science

Threads. CS3026 Operating Systems Lecture 06

Project 3-2. Mutex & CV

High Performance Computing Prof. Matthew Jacob Department of Computer Science and Automation Indian Institute of Science, Bangalore

Lecture 17: Threads and Scheduling. Thursday, 05 Nov 2009

Figure 30.1: A Parent Waiting For Its Child What we would like to see here is the following output:

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

CSE 153 Design of Operating Systems

Synchronization for Concurrent Tasks

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

EECS 482 Introduction to Operating Systems

Locks and semaphores. Johan Montelius KTH

CS A331 Programming Language Concepts

Reminder from last time

COMP 3361: Operating Systems 1 Final Exam Winter 2009

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

Lecture 3: Processes. CMPUT 379, Section A1, Winter 2014 January 13, 15 and 17

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

Unit 8: Threads. Prepared by: Dr. Abdallah Mohamed, AOU-KW Updated by Mrs. Malak EL-Amir AOU SAB Fall 14-15

Condition Synchronization

Synchronization 1. Synchronization

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Midterm Exam #2 Solutions October 25, 2016 CS162 Operating Systems

CS 3305 Intro to Threads. Lecture 6

CS533 Concepts of Operating Systems. Jonathan Walpole

Computer Systems Assignment 4: Scheduling and I/O

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

Dealing with Issues for Interprocess Communication

CS333 Intro to Operating Systems. Jonathan Walpole

CS/SE3SH3 Operating Systems

Interprocess Communication By: Kaushik Vaghani

FINE-GRAINED SYNCHRONIZATION. Operating Systems Engineering. Sleep & Wakeup [chapter #5] 15-Jun-14. in the previous lecture.

Main Points. Defini&on. Design pa9ern Example: bounded buffer. Condi&on wait/signal/broadcast

Lecture 8: September 30

EECS 482 Introduction to Operating Systems

CS Operating Systems

Transcription:

Lecture 13 Condition Variables

Contents In this lecture, you will learn Condition Variables And how to use CVs to solve The Producer/Consumer (Bounded Buffer) Problem

Review Thus far we have developed the notion of a lock. Unfortunately, locks are not the only primitives that are needed to build concurrent programs. E.g., one thread waiting for another, remember pthread_join()?

Review Two problems:

Review Using a shared variable? Is it correct? Yes What is the limitation? Waste of CPU time Do we need a lock here? Not a critical section

Condition Variables A condition variable is an explicit queue that: threads can put themselves on when some state of execution (i.e., some condition) is not as desired (by waiting on the condition); some other thread, when it changes said state, can then wake one (or more) of those waiting threads and thus allow them to continue (by signaling on the condition).

Condition Variables The POSIX calls: The wait() call is executed when a thread wishes to put itself to sleep; The signal() call is executed when a thread has changed something in the program and thus wants to wake a sleeping thread waiting on this condition

Condition Variables

Condition Variables Discussion: Why do we need a lock here? Why we need the variable &m in calling wait()?

Discussion Why is the code broken?

Discussion Why is the code broken?

Contents In this lecture, you will learn Condition Variables And how to use CVs to solve The Producer/Consumer (Bounded Buffer) Problem

The Producer/Consumer (Bounded Buffer) Problem Imagine one or more producer threads and one or more consumer threads. Producers generate data items and place them in a buffer; Consumers grab said items from the buffer and consume them in some way. (A bounded buffer is used when you pipe the output of one program into another.)

The Producer/Consumer (Bounded Buffer) Problem Let s begin with only 1 producer, 1 consumer, buffer-size = 1:

The Producer/Consumer (Bounded Buffer) Problem

The Producer/Consumer (Bounded Buffer) Problem The put() and get() routines have critical sections within them.

The Producer/Consumer (Bounded Buffer) Problem

The Producer/Consumer (Bounded Buffer) Problem With just a single producer and a single consumer, the code works. However, if we have more than one of these threads (e.g., two consumers), the solution has two critical problems.

The Producer/Consumer (Bounded Buffer) Problem

The Producer/Consumer (Bounded Buffer) Problem A simple rule to remember with condition variables is to always use while loops.

The Producer/Consumer (Bounded Buffer) Problem

The Producer/Consumer (Bounded Buffer) Problem Discussion: Dealing with bounded buffer instead of single buffer. How many CVs do we need?

The Producer/Consumer (Bounded Buffer) Problem

The Producer/Consumer (Bounded Buffer) Problem

Discussion Let us make the Producer/Consumer Problem more complicated. Different producers generate different types of data items and place them in a buffer; Picky consumers each grab some certain type(s) of items from the buffer and consume them in some way. How many CVs do we need? It depends An example on memory allocation.

Discussion Let us make the Producer/Consumer Problem more complicated. Different producers generate different types of data items and place them in a buffer; Picky consumers each grab some certain type(s) of items from the buffer and consume them in some way. How many CVs do we need? It depends An example on memory allocation.

Discussion

Problem Assume there are zero bytes free; thread Ta calls allocate(100), followed by thread Tb which asks for less memory by calling allocate(10). At that point, assume a third thread, Tc, calls free(50). If Ta is woken up, both threads will keep waiting. The code does not work, as the thread waking other threads does not know which thread (or threads) to wake up.

Covering Condition Many CVs? Based on the types of items. One CV is enough. Replace the pthread_cond_signal() call in the code above with a call to pthread_cond_broadcast(), which wakes up all waiting threads.

Summary Condition Variables The Producer/Consumer (Bounded Buffer) Problem