More Shared Memory Programming

Similar documents
ANSI/IEEE POSIX Standard Thread management

Threads need to synchronize their activities to effectively interact. This includes:

Pthreads (2) Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Synchronization Primitives

Pthreads. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Synchronising Threads

Solving the Producer Consumer Problem with PThreads

Synchronization. Dr. Yingwu Zhu

Condition Variables. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CSci 4061 Introduction to Operating Systems. Synchronization Basics: Locks

Multithreading Programming II

Operating systems and concurrency (B08)

Paralleland Distributed Programming. Concurrency

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

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

CMSC421: Principles of Operating Systems

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

Introduction to parallel computing

Operating Systems (2INC0) 2017/18

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

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

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

CS 470 Spring Mike Lam, Professor. Semaphores and Conditions

POSIX Threads. HUJI Spring 2011

Operating Systems CMPSC 473. Synchronization February 26, Lecture 12 Instructor: Trent Jaeger

Operating Systems ECE344

Introduction to OS Synchronization MOS 2.3

Introduc)on to pthreads. Shared memory Parallel Programming

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

CS 25200: Systems Programming. Lecture 26: Classic Synchronization Problems

Synchronization 1. Synchronization

Opera&ng Systems ECE344

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

More Types of Synchronization 11/29/16

CMSC421: Principles of Operating Systems

Lecture 6. Process Synchronization

Synchroniza+on II COMS W4118

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

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

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

CS510 Operating System Foundations. Jonathan Walpole

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

Threads Tuesday, September 28, :37 AM

Synchronization Mechanisms

Advanced Threads & Monitor-Style Programming 1/24

Threads. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

Synchronization. Semaphores implementation

PRINCIPLES OF OPERATING SYSTEMS

W4118 Operating Systems. Instructor: Junfeng Yang

Synchronization 1. Synchronization

CS 241 Honors Concurrent Data Structures

Assertions, pre/postconditions

LSN 13 Linux Concurrency Mechanisms

Posix Threads (Pthreads)

High-level Synchronization

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

CPSC 261 Midterm 2 Thursday March 17 th, 2016

Lecture 6 (cont.): Semaphores and Monitors

Chapter 4 Threads. Images from Silberschatz 03/12/18. CS460 Pacific University 1

Lab 12 - Concurrency 2

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

Thread Posix: Condition Variables Algoritmi, Strutture Dati e Calcolo Parallelo. Daniele Loiacono

Motivation and definitions Processes Threads Synchronization constructs Speedup issues

CSE 120. Fall Lecture 6: Semaphores. Keith Marzullo

Process Synchronization

So far, we've seen situations in which locking can improve reliability of access to critical sections.

ENCM 501 Winter 2019 Assignment 9

Programming in Parallel COMP755

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

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

COMPSCI 230 Threading Week8. Figure 1 Thread status diagram [

Locks. Dongkun Shin, SKKU

Systems Programming/ C and UNIX

real time operating systems course

EEE3052: Introduction to Operating Systems. Fall Project #3

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

Introduction to PThreads and Basic Synchronization

Computer Systems Laboratory Sungkyunkwan University

CSE 153 Design of Operating Systems

CS 345 Operating Systems. Tutorial 2: Treasure Room Simulation Threads, Shared Memory, Synchronization

A Brief Introduction to OS/2 Multithreading

Outline for Today CSE 142. CSE142 Wi03 G-1. withdraw Method for BankAccount. Class Invariants

CS 111. Operating Systems Peter Reiher

The mutual-exclusion problem involves making certain that two things don t happen at once. A non-computer example arose in the fighter aircraft of

PROCESS SYNCHRONIZATION

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

THREADS. Jo, Heeseung

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:

Atomic Instructions! Hakim Weatherspoon CS 3410, Spring 2011 Computer Science Cornell University. P&H Chapter 2.11

Chapter 32 Multithreading and Parallel Programming

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

CSE332: Data Abstractions Lecture 22: Shared-Memory Concurrency and Mutual Exclusion. Tyler Robison Summer 2010

TCSS 422: OPERATING SYSTEMS

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

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

Operating Systems. Synchronization

CSE 120 Principles of Operating Systems Spring 2016

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

Transcription:

More Shared Memory Programming Shared data structures We want to make data structures that can be shared by threads. For example, our program to copy a file from one disk to another used a shared FIFO queue and a shared set. We start with a very simple data structure to keep track of money in a bank account. [Note: To keep things simple I will ignore the fact that long integers could overflow. In real code, we should look out for this.] We have typedef struct Account { pthread_mutex_t mtx; long balance;// In 100ths of dollars. } Account; void initialize( Account *p ) { pthread_mutex_init( & p->mtx, NULL ); p->balance = 0;} void deposit( Account *p, long amount ) { p->balance += amount; And we can write a withdraw function that is similar. Typeset June 16, 2016 1

Enforcing an object invariant Let s suppose we want to prevent the account balance from ever going below zero. Such a restriction is called an object invariant. We should document it. The object invariant must be true whenever the object is no locked. typedef struct Account { /* Object invariant: balance>= 0 */ pthread_mutex_t mtx; long balance; } Account; The following will not work /* Precondition: 0 <= amount && amount <= p->balance */ void withdraw( Account *p, long amount ) { p->balance += amount; The caller can not be sure of what the balance is, since another thread might change it. We could do the following int withdraw( Account *p, long amount ) { int result; Typeset June 16, 2016 2

if( amount<= p->balance ) { p->balance += amount; result = 1;} else result = 0; pthread_mutex_unlock( & p->mtx ); return result;} In this case the function returns a boolean indicating whether the withdrawal was successful. Waiting Another way to enforce the object invariant is to force threads that withdraw to wait until there are sufficient funds in the account. We want something like this void withdraw( Account *p, long amount ) { wait until amount<= p->balance p->balance -= amount; However, if the thread waits while the object is locked, no thread will be able to deposit, so the thread will wait forever. We must unlock the object while waiting. We want something like the following Typeset June 16, 2016 3

void withdraw( Account *p, long amount ) { while(! amount<= p->balance ) { pthread_mutex_unlock( & p->mtx ); wait a little while } p->balance -= amount; There is a nice way to implement the code in the box with a device called a condition variable. Each condition variable is associated with some mutex and allows threads to wait until some condition happens. We add a condition variable to the Account type and initialize it typedef struct Account { pthread_mutex_t mtx; long balance; pthread_cond_t bigenoughbalancecond; } Account; void initialize( Account *p ) { pthread_mutex_init( & p->mtx, NULL ); pthread_cond_init( & p->bigenoughbalancecond, NULL ); p->balance = 0;} Now in withdraw we wait on the condition variable. Typeset June 16, 2016 4

void withdraw( Account *p, long amount ) { while(! (amount<= p->balance) ) { pthread_cond_wait( & p->bigenoughbalancecond, & p->mtx );} p->balance -= amount; The call pthread_cond_wait( & p->bigenoughbalancecond, & p->mtx ); means pthread_mutex_unlock( & p->mtx ); wait until condition p->bigenoughbalancecond is signalled Since threads doing a withdrawal may wait for a signal we should add code to signal whenever a deposit is made. We add one line to the deposit method void deposit( Account *p, long amount ) { p->balance += amount; pthread_cond_broadcast( & p- >bigenoughbalancecond ); Typeset June 16, 2016 5

The call to pthread_cond_broadcast will wake up all threads waiting on the condition. [Exercise: Suppose two threads are each waiting to withdraw $100.00. If another thread deposits $200.00, will both threads return from their calls to withdraw? You may assume that there are no other threads. Draw a message sequence diagram (or other kind of picture) illustrating what may happen.] Typeset June 16, 2016 6

A Queue data structure We will define a data type representing FIFO (first-in, first-out) queue. A thread that tries to take from an empty queue must wait. A thread that tries to put data into a full queue must wait. There are two reasons to wait, so we use 2 condition variables. typedef struct Queue { pthread_mutex_t mtx; pthread_cond_t notfull; // When p->size < p->capacity pthread_cond_t notempty;// When p->size>0 // Object invariant: 0<capacity int capacity; // Object invariant: 0<= size && size<= capacity int size; // Object invariant: 0<= next && next<capacity int next; // Object invariant: a points to an array of capacity integers int *a; } Queue; We have a routine for initializing a Queue. This must be called before the queue is shared. int initialize( Queue *p, int capacity ) { pthread_mutex_init( & p->mtx, NULL ); Typeset June 16, 2016 7

} pthread_cond_init( & p->notfull, NULL ); pthread_cond_init( & p->notempty, NULL ); p->capacity = 0;p->size = 0;p->next = 0; p-> a = calloc( capacity, sizeof( int ) ); return p->a!= NULL; To put new data on the queue we must wait until there is space. We want something like this void put( Queue *p, int value ) { wait until the queue is not full add a new item We can implement it like this. void put( Queue *p, int value ) { /* Wait until the queue is not full */ while( p->size == p->capacity ) { pthread_cond_wait( & p->notfull, & p->mtx );} /* Add a new value */ p->a[ (p->next + p->size) % p->capacity ] = value; ++ p->size; pthread_cond_broadcast( & p->notempty ); The call to broadcast at the end is because after size has been incremented, the queue will not be empty anymore. There may be threads waiting to for this condition. Typeset June 16, 2016 8

The code for take is similar to the code for put. int take( Queue *p ) { /* Wait until the queue is not empty */ while( p->size == 0 ) { pthread_cond_wait( & p->notempty, & p->mtx ); } /* Remove one item. */ int result = p->a[ p->next ]; p->next = (p->next + 1) % p->capacity; -- p->size; pthread_cond_broadcast( & p->notfull ); pthread_mutex_unlock( & p->mtx ); return result;} [Exercise: We also need a shared structure representing a set of integers. Threads can put numbers into the set and they can remove an arbitrary member of the set. However, to take a number, the thread must wait until the set is not empty. Implement this data structure.] Advice: Only use pthread_cond_wait and pthread_cond_broadcast as illustrated in the examples above. In particular always put calls to pthread_cond_wait inside a while loop and put the loop insize of a lock/unlock pair. Deadlock As we have seen the solution to shared memory problems is often waiting. Typeset June 16, 2016 9

Threads must wait to obtain exclusive access Threads must wait for conditions to arise. We wait to ensure safety. Much like walking across a street in China. A saftey property is a property that says that the system will never get into a bad state, e.g. one where object invariants are violated. However, waiting has a potential hazard of its own. We may cause threads to wait forever. Consider the bank example. We will make a new method transfer. We lock both accounts because we want to ensure that there is never a time that that money is missing or extra money is present. void deposit( Account *p, Account *q, long amount ) { pthread_mutex_lock( & q->mtx ); while( amount>p->balance ) { pthread_cond_wait( & p->bigenoughbalancecond, & p->mtx );} p->balance -= amount; q->balance +=amount; pthread_cond_broadcast( & q- >bigenoughbalancecond ); pthread_mutex_unlock( & q->mtx ); Typeset June 16, 2016 10

But what happens if we transfer from account a to account b at the same time? We could end up with both accounts locked and neither thread able to proceed. They will remain locked forever. This is deadlock. It is a violation of a liveness property. Liveness properties state that the system does not get stuck. Typeset June 16, 2016 11