More Types of Synchronization 11/29/16

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

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

C09: Process Synchronization

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

Chapter 6: Process Synchronization

Lecture 13 Condition Variables

Interprocess Communication and Synchronization

CS 470 Spring Mike Lam, Professor. Semaphores and Conditions

Synchronising Threads

Process Synchronization Mechanisms

Synchroniza+on II COMS W4118

Operating Systems (2INC0) 2017/18

EECS 482 Introduction to Operating Systems

Threads and Synchronization. Kevin Webb Swarthmore College February 15, 2018

Lecture Topics. Announcements. Today: Concurrency (Stallings, chapter , 5.7) Next: Exam #1. Self-Study Exercise #5. Project #3 (due 9/28)

Concurrency. Chapter 5

W4118 Operating Systems. Instructor: Junfeng Yang

Interprocess Communication By: Kaushik Vaghani

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

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

Operating Systems. Thread Synchronization Primitives. Thomas Ropars.

Semaphores INF4140. Lecture 3. 0 Book: Andrews - ch.04 ( ) INF4140 ( ) Semaphores Lecture 3 1 / 34

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

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

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

COSC 6374 Parallel Computation. Shared memory programming with POSIX Threads. Edgar Gabriel. Fall References

Threading and Synchronization. Fahd Albinali

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

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

Solving the Producer Consumer Problem with PThreads

Concurrency: Deadlock and Starvation

Operating Systems: William Stallings. Starvation. Patricia Roy Manatee Community College, Venice, FL 2008, Prentice Hall

Chapter 2 Processes and Threads. Interprocess Communication Race Conditions

Concept of a process

Reminder from last time

Last Class: Deadlocks. Today

PROCESS SYNCHRONIZATION

Readers/Writers Problem. Readers/Writers: Scenario 1. Readers/Writers Problem. Today: Synchronization for Readers/Writers Problem

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

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data

Semaphores (by Dijkstra)

Condition Variables & Semaphores

Synchronization Mechanisms

Multi-core Architecture and Programming

Synchronization 1. Synchronization

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

More Shared Memory Programming

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

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

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

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

Operating Systems ECE344

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

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

Concurrency and Synchronisation

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Concurrency and Synchronisation

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

An Introduction to Parallel Systems

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

Dealing with Issues for Interprocess Communication

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

Process Management And Synchronization

High-level Synchronization

IV. Process Synchronisation

Opera&ng Systems ECE344

Synchronization (Part 2) 1/40

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

Chapter 2 Processes and Threads

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks

Last Class: Synchronization

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

CS 153 Design of Operating Systems Winter 2016

EECS 482 Introduction to Operating Systems

CSE 120 Principles of Operating Systems

Today: Synchronization. Recap: Synchronization

Semaphores and Monitors: High-level Synchronization Constructs

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

Synchronization I. Jo, Heeseung

CS A331 Programming Language Concepts

Operating Systems. Designed and Presented by Dr. Ayman Elshenawy Elsefy

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

Semaphores. To avoid busy waiting: when a process has to wait, it will be put in a blocked queue of processes waiting for the same event

Semaphores. Semaphores. Semaphore s operations. Semaphores: observations

Condition Variables. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Chap. 6 Part 1. CIS*3090 Fall Fall 2016 CIS*3090 Parallel Programming 1

POSIX / System Programming

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

Operating systems and concurrency (B08)

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

Process & Thread Management II CIS 657

Midterm Exam. October 20th, Thursday NSC

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

Synchronization Classic Problems

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

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

Real-Time Programming

Last Class: Monitors. Real-world Examples

What's wrong with Semaphores?

Transcription:

More Types of Synchronization 11/29/16

Today s Agenda Classic thread patterns Other parallel programming patterns More synchronization primitives: RW locks Condition variables Semaphores Message passing Exercise

Common Thread Patterns Thread pool (a.k.a. work queue) Producer / Consumer (a.k.a. Bounded buffer) Thread per client connection

Thread Pool / Work Queue Common way of structuring threaded apps: Thread Pool

Thread Pool / Work Queue Common way of structuring threaded apps: Queue of work to be done: Thread Pool

Thread Pool / Work Queue Common way of structuring threaded apps: Queue of work to be done: Farm out work to threads when they re idle. Thread Pool

Thread Pool / Work Queue Common way of structuring threaded apps: Queue of work to be done: Thread Pool As threads finish work at their own rate, they grab the next item in queue. Common for embarrassingly parallel algorithms. Works across the network too!

The Producer/Consumer Problem in Producer(s) 2 3 5 4 9 Consumer(s) buf out Producer produces data, places it in shared buffer Consumer consumes data, removes from buffer All kinds of real-world examples: print queue: printer is consumer CPU queue of ready processes/threads to run on CPU

Thread Per Client Consider a web server: Client connects Client asks for a page: http://web.cs.swarthmore.edu/~bryce/cs31/f16 Server looks through file system to find path (I/O) Server sends back html for client browser (I/O) Web server does this for MANY clients at once

Thread Per Client Server main thread: Wait for new connections Upon receiving one, spawn new client thread Continue waiting for new connections, repeat Client threads: Read client request, find files in file system Send files back to client Nice property: Each client is independent Nice property: When a thread does I/O, it gets blocked for a while. OS can schedule another one.

Other Noteworthy Parallel Patterns Single instruction, multiple data (SIMD) Apply the same operation independently to many pieces of data. Map-Reduce Apply the same operation independently to many pieces of data, then combine the results.

Single instruction, multiple data Apply the same operation independently to many pieces of data. This is so common in graphics that we have specialized hardware for it (graphics cards). Graphics hardware can be used for non-graphics SIMD tasks. Known as GPGPU: general purpose programming on graphics processing units. Example: matrix multiplication for machine learning.

Map-Reduce Map step: perform some computation on each piece of data. Reduce step: combine the results of the mappers. Assign data to mappers Assign data to reducers output Example: find the most-common words in a book.

Synchronization Mechanisms Mutex locks Guarantee mutually exclusive access. Barriers Wait for other threads to catch up. Read/write locks Condition variables Semaphores

Read/Write locks Readers/Writers Problem: An object is shared among several threads. Some threads only read the object, others may write it. We can safely allow multiple readers. But writers need exclusive access. pthread_rwlock_t: pthread_rwlock_init: pthread_rwlock_rdlock: pthread_rwlock_wrlock: initialize rwlock lock for reading lock for writing

Condition Variables Wait for a condition to be true. In the pthreads library: pthread_cond_init: pthread_cond_wait: pthread_cond_signal: pthread_cond_broadcast: Initialize CV Wait on CV Wakeup one waiter Wakeup all waiters Condition variable is associated with a mutex: 1. Lock mutex, realize conditions aren t ready yet. 2. Temporarily give up mutex until CV signaled. 3. Reacquire mutex and wake up when ready.

Using Condition Variables while (TRUE) { //independent code } lock(m); while (conditions bad) wait(cond, m); //proceed knowing that conditions are now good signal (other_cond); // Let other thread know unlock(m);

Semaphores: generalized mutexes Semaphore: synchronization variable Has integer value List of waiting threads Works like a gate If sem > 0, gate is open Value equals number of threads that can enter Else, gate is closed Possibly with waiting threads sem = 3 sem = 2 sem = 1 critical section sem = 0 A semaphore with initial value 1 is a mutex

Message Passing send (to, buf) receive (from, buf) P 1 P 2 kernel Operating system mechanism for IPC send (destination, message_buffer) receive (source, message_buffer) Data transfer: in to and out of kernel message buffers Synchronization: can t receive until message is sent

Producer-Consumer Problem A shared fix-sized buffer Two types of threads: 1. Producers: create an item, add it to buffer. 2. Consumers: remove an item from buffer, and consume it. P0 P1 Cm... Buffer of size N Threads

Producer/Consumer Synchronization? Circular Queue Buffer: add to one end (in), remove from other (out) int buf[n]; int in, out; int num_items; buf: add/remove 9 11 3 7 out: 1 in: 5 num_items: 4 Assume Producers & Consumers forever produce & consume Q: Where is Synchronization Needed in Producer & Consumer? Producer: Consumer:

Producer/Consumer Synchronization? Producer: Needs to wait if there is no space to put a new item in the buffer (Scheduling) Needs to wait to have mutually exclusive access to shared state associated with the buffer (Atomic): Size of the buffer (num_items) Next spot to insert into (in) Consumer: Needs to wait if there is nothing in the buffer to consume (Scheduling) Needs to wait to have mutually exclusive access to shared state associated with the buffer (Atomic): Size of the buffer (num_items) Next spot to remove from (out)

Exercise Come up with a pseudo-code solution to producer and consumer. Assume circular buffer add/remove functions provided (don t check overwrite or garbage return value) What does Producer need to do to add an item? What does Consumer need to do to remove an item? Questions to Ask: Where do you need to add synchronization? What sort of synchronization? Do you need any other state information?