Reminder from last <me

Similar documents
Reminder from last time

Concurre Concurr nt n Systems 8L for Part IB Handout 1 Dr. Steven Hand 1

Reminder from last time

Concurrent systems Lecture 1: Introduction to concurrency, threads, and mutual exclusion

Concurrent systems Lecture 1: Introduction to concurrency, threads, and mutual exclusion

CSE Opera,ng System Principles

Concurrent systems Lecture 7: Crash recovery, lock- free programming, and transac<onal memory. Dr Robert N. M. Watson

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Synchroniza+on II COMS W4118

Last Class: Synchronization

Today: Synchronization. Recap: Synchronization

Synchronization Principles I

PRINCIPLES OF OPERATING SYSTEMS

Synchroniza+on. Today: Implementa+on issues

2 Threads vs. Processes

Introduction to Threads

Locks. Dongkun Shin, SKKU

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

Opera&ng Systems ECE344

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

CIS Operating Systems Synchronization based on Busy Waiting. Professor Qiang Zeng Spring 2018

Carnegie Mellon. Synchroniza+on : Introduc+on to Computer Systems Recita+on 14: November 25, Pra+k Shah (pcshah) Sec+on C

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

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

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

CS370 OperaBng Systems

Classical concurrency control: topic overview 1 In these lectures we consider shared writeable data in main memory

PROCESS SYNCHRONIZATION

Systèmes d Exploitation Avancés

Page 1. Challenges" Concurrency" CS162 Operating Systems and Systems Programming Lecture 4. Synchronization, Atomic operations, Locks"

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

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

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

CS370 Operating Systems

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 6: Process Synchronization

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

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

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

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

Semaphores. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

EECS 482 Introduction to Operating Systems

Lecture #7: Implementing Mutual Exclusion

C09: Process Synchronization

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

Process Synchronization Mechanisms

The Kernel Abstrac/on

Chapter 5: Synchronization 1

Synchronization for Concurrent Tasks

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

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

Operating Systems. Thread Synchronization Primitives. Thomas Ropars.

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

Concurrency: Signaling and Condition Variables

COP 4225 Advanced Unix Programming. Synchronization. Chi Zhang

CSE 451: Operating Systems Winter Lecture 7 Synchronization. Hank Levy 412 Sieg Hall

The Art and Science of Memory Alloca4on

CHAPTER 6: PROCESS SYNCHRONIZATION

Concept of a process

Chapter 7: Process Synchronization!

CS 153 Design of Operating Systems Winter 2016

Consistency & Coherence. 4/14/2016 Sec5on 12 Colin Schmidt

Chapter 6: Process Synchronization

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

Interprocess Communication and Synchronization

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

Page 1. Another Concurrent Program Example" Goals for Today" CS162 Operating Systems and Systems Programming Lecture 4

Symmetric Multiprocessors: Synchronization and Sequential Consistency

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

Synchronization I. Jo, Heeseung

CS370 Opera;ng Systems Midterm Review. Yashwant K Malaiya Spring 2018

Synchronization COMPSCI 386

CSE 153 Design of Operating Systems

UNIX Input/Output Buffering

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

G52CON: Concepts of Concurrency

EECS 482 Introduction to Operating Systems

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

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

Synchronization. CS61, Lecture 18. Prof. Stephen Chong November 3, 2011

Synchronising Threads

Synchronization Principles

Process Synchronization

IV. Process Synchronisation

COMP 3430 Robert Guderian

Dealing with Issues for Interprocess Communication

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

Chapter 6: Synchronization. Chapter 6: Synchronization. 6.1 Background. Part Three - Process Coordination. Consumer. Producer. 6.

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

Chapter 5: Process Synchronization. Operating System Concepts Essentials 2 nd Edition

MULTITHREADING AND SYNCHRONIZATION. CS124 Operating Systems Fall , Lecture 10

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

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

Interprocess Communication By: Kaushik Vaghani

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

Chapter 5: Process Synchronization. Operating System Concepts 9 th Edition

Synchronization for Concurrent Tasks

CS5460: Operating Systems

CS370 Operating Systems

CS370 Operating Systems

Transcription:

Concurrent systems Lecture 2: Mutual exclusion and process synchronisa<on Dr Robert N. M. Watson 1 Reminder from last <me Defini<on of a concurrent system Origins of concurrency within a computer Processes and threads Challenge: concurrent access to shared resources Mutual exclusion, race condi<ons, and atomicity 2 1

From last <me: beer- buying example Thread 1 (person 1) 1. Look in fridge 2. If no beer, go buy beer 3. Put beer in fridge Thread 2 (person 2) 1. Look in fridge 2. If no beer, go buy beer 3. Put beer in fridge In most cases, this works just fine But if both people look (step 1) before either refills the fridge (step 3) we ll end up with too much beer! Obviously more worrying if look in fridge is check We spozed race condi<ons in obvious concurrent implementa<ons reactor, Ad and hoc buy solu<ons beer (e.g., is toggle leaving a safety note) failed system ;- ) Even naïve applica<on of atomic opera<ons failed What we want is a general soluon for mutual exclusion 3 This <me General mutual exclusion: how to [not] do it Hardware support for mutual exclusion Semaphores for mutual exclusion, process synchronisaon, and resource allocaon Two- party and general producer- consumer rela<onships Mul<- reader single- writer locks 4 2

General mutual exclusion We would like the ability to define a region of code as a cri<cal sec<on e.g. // thread 1 ENTER_CS(); beer = checkfridge(); if(!beer) buybeer(); LEAVE_CS(); // thread 2 ENTER_CS(); beer = checkfridge(); if(!beer) buybeer(); LEAVE_CS(); This should work providing that our implementa<on of ENTER_CS() / LEAVE_CS() is correct 5 Implemen<ng mutual exclusion One op<on is to prevent context switches e.g. disable interrupts (for kernel threads), or set an in- memory flag (for user threads) ENTER_CS() = disable context switches ; LEAVE_CS() = re- enable context switches Can work but: Rather brute force (stops all other threads, not just those who want to enter the cri<cal sec<on) Poten<ally unsafe (if disable interrupts and then sleep wai<ng for a <mer interrupt ;- ) And doesn t work across mul<ple CPUs 6 3

Implemen<ng mutual exclusion Associate a mutual exclusion lock with each cri<cal sec<on, e.g. a variable L (must ensure use correct lock variable!) ENTER_CS() = LOCK(L) LEAVE_CS() = UNLOCK(L) Can implement LOCK() using read- and- set(): LOCK(L) { while(!read-and-set(l)) ; // do nothing UNLOCK(L) { L = ; 7 Solu<on #3: mutual exclusion locks // thread 1 LOCK(fridgeLock); beer = checkfridge(); if(!beer) buybeer(); UNLOCK(fridgeLock); // thread 2 LOCK(fridgeLock); beer = checkfridge(); if(!beer) buybeer(); UNLOCK(fridgeLock); This is finally! a correct program S<ll not perfect Lock might be held for quite a long <me (e.g. imagine another person wan<ng to get the milk!) Wai<ng threads waste CPU <me (or worse) Contenon occurs when consumers have to wait for locks Mutual exclusion locks open known as mutexes 8 4

Compare and Swap (CAS) Found on CISC systems such as x86 Test and Set (TAS) another varia<on Caller provides previous value as argument If memory contents match, assignment occurs Return value can be tested to trigger loop spin: mov %edx, 1 # New value mov foo_lock, %eax # Load old value test %eax, %eax # If non-zero (owned), jnz spin # loop lock cmpxchg %edx, foo_lock # If foo_lock == %eax, test %eax, %eax # swap in value from jnz spin # %edx; else loop 9 Load linked- store condi<onal (LL/SC) Found on RISC systems (MIPS, Alpha, ARM, ) Load value from memory with LL Manipulate value in register SC fails if memory modified since load linked Return value can be tested to trigger loop spin: lld $t, ($a) # Load old value bnez $t, spin # If non-zero (owned), loop dli $t, 1 # New value (branch-delay slot) scd $t, ($a) # Conditional store to $a beqz $t, spin # If failed ($t zero), loop nop # Branch-delay slot 1 5

What if no hardware support? Solu<on #3 requires an atomic read- and- set opera<on but what if we don t have one? Op<on 1: Fake atomic opera<on by disabling interrupts (or context switches) between read and set But doesn t work across mul<ple CPUs Op<on 2: Build a mutual exclusion scheme which only relies on atomic reads and writes! Hot topic in the 197s/8s; mostly irrelevant now Probably doesn t even work on modern hardware! In prac<ce, we almost always build mutual exclusion on top of atomic instruc<ons like CAS, TAS, LL/SC,... 11 Semaphores Even with atomic opera<ons, busy wai<ng for a lock is inefficient BeZer to sleep un<l resource available Dijkstra (THE, 1968) proposed semaphores New type of variable Ini<alized once to an integer value (default ) Supports two opera<ons: wait() and signal() Some<mes called down() and up() (and originally called P() and V()... blurk!) 12 6

Semaphore implementa<on Implemented as an integer and a queue wait(sem) { if(sem > ) { sem = sem-1; else suspend caller & add to queue for sem signal(sem) { if no threads are waiting { sem = sem + 1; else wake up some thread on queue Method bodies are implemented atomically suspend and wake invoke threading APIs 13 Mutual exclusion with a semaphore asem 1 A B C wait (asem) B B, C CS wait (asem) B blocked wait (asem) C signal (asem) C blocked CS signal (asem) 1 CS signal (asem) Ini<alize semaphore to 1; wait() is lock(), signal() is unlock() 14 7

Two process synchroniza<on wait before signal signal before wait asem A B asem A B wait (asem) A A blocked 1 wake- up wai3ng signal (asem) signal (asem) A con3nues wait (asem) A con3nues Ini<alize semaphore to ; A proceeds only aper B signals 15 N- resource alloca<on Suppose there are N instances of a resource e.g. N printers azached to a DTP system Can manage alloca<on with a semaphore sem, ini<alized to N Anyone wan<ng printer does wait(sem) Aper N people get a printer, next will sleep To release resource, signal(sem) Will wake someone if anyone is wai<ng Will typically also require mutual exclusion e.g. to decide which printers are free 16 8

Semaphore programming examples Semaphores are quite powerful Can solve mutual exclusion Can also provide condion synchronizaon Thread waits un<l some condi<on set by another thread becomes true Let s look at some examples: 1. One producer thread, one consumer thread, with a N- slot shared memory buffer 2. Any number of producer and consumer threads, again using an N- slot shared memory buffer 3. Mul<ple reader, single writer synchroniza<on 17 Producer- consumer problem Shared buffer B[] with N slots, ini<ally empty Producer thread wants to: Produce an item If there s room, insert into next slot; Otherwise, wait un<l there is room Consumer thread wants to: If there s anything in buffer, remove an item (and consume it) Otherwise, wait un<l there is something General concurrent programming paradigm e.g. pipelines in Unix; staged servers; work stealing 18 9

Producer- consumer solu<on int buffer[n]; int in =, out = ; spaces = new Semaphore(N); items = new Semaphore(); // producer thread while(true) { item = produce(); if there is space { buffer[in] = item; in = (in + 1) % N; // consumer thread while(true) { if there is an item { item = buffer[out]; out = (out + 1) % N; consume(item); buffer g h i j k l out in N- 1 19 Producer- consumer solu<on int buffer[n]; int in =, out = ; spaces = new Semaphore(N); items = new Semaphore(); // producer thread while(true) { item = produce(); wait(spaces); buffer[in] = item; in = (in + 1) % N; signal(items); // consumer thread while(true) { wait(items); item = buffer[out]; out = (out + 1) % N; signal(spaces); consume(item); buffer g h i j k l out in N- 1 2 1

Producer- consumer solu<on Use of semaphores for N- resource alloca<on In this case, resource is a slot in the buffer spaces allocates empty slots (for producer) items allocates full slots (for consumer) No explicit mutual exclusion threads will never try to access the same slot at the same <me; if in == out then either buffer is empty (and consumer will sleep on items ), or buffer is full (and producer will sleep on spaces ) 21 Generalized producer- consumer Previously had exactly one producer thread, and exactly one consumer thread More generally might have many threads adding items, and many removing them If so, we do need explicit mutual exclusion e.g. to prevent two consumers from trying to remove (and consume) the same item Can implement with one more semaphore 22 11

Generalized P- C solu<on int buffer[n]; int in =, out = ; spaces = new Semaphore(N); items = new Semaphore(); guard = new Semaphore(1); // for mutual exclusion // producer threads while(true) { item = produce(); wait(spaces); wait(guard); buffer[in] = item; in = (in + 1) % N; signal(guard); signal(items); // consumer threads while(true) { wait(items); wait(guard); item = buffer[out]; out = (out + 1) % N; signal(guard); signal(spaces); consume(item); Exercise: allow 1 producer and 1 consumer concurrent access 23 Mul<ple- Readers Single- Writer Another common paradigm is MRSW Shared resource accessed by a set of threads e.g. cached set of DNS results Safe for many threads to read simultaneously, but a writer (upda<ng) must have exclusive access Data stability vs. mutual exclusion Simplest solu<on uses a single semaphore as a mutual exclusion lock for write access Any writer must wait to acquire this First reader also acquires this; last reader releases it Manage reader counts using another semaphore 24 12

Simplest MRSW solu<on int nr = ; // number of readers rsem = new Semaphore(1); // protects access to nr wsem = new Semaphore(1); // protects access to data // a writer thread wait(wsem);.. perform update to data signal(wsem); Code for writer is simple.. but reader case more complex: must track number of readers, and acquire or release overall lock as appropriate // a reader thread wait(rsem); nr = nr + 1; if (nr == 1) // first in wait(wsem); signal(rsem);.. read data wait(rsem); nr = nr - 1; if (nr == ) // last out signal(wsem); signal(rsem); 25 Simplest MRSW solu<on Solu<on on previous slide is correct Only one writer will be able to access data structure, but providing there is no writer any number of readers can access it However writers can starve If readers con<nue to arrive, a writer might wait forever (since readers will not release wsem) Would be fairer if a writer only had to wait for all current readers to exit Can implement this with an addi<onal semaphore 26 13

A fairer MRSW solu<on int nr = ; // number of readers rsem = new Semaphore(1); // protects access to nr wsem = new Semaphore(1); // protects access to data turn = new Semaphore(1); // for more fairness! Once a writer tries to enter he will acquire turn which prevents any further readers from entering // a writer thread wait(turn); wait(wsem);.. perform update to data signal(turn); signal(wsem); // a reader thread wait(turn); signal(turn); wait(rsem); nr = nr + 1; if (nr == 1) // first in wait(wsem); signal(rsem);.. read data wait(rsem); nr = nr - 1; if (nr == ) // last out signal(wsem); signal(rsem); 27 Semaphores: summary Powerful abstrac<on for implemen<ng concurrency control: mutual exclusion & condi<on synchroniza<on BeZer than read- and- set() but correct use requires considerable care e.g. forget to wait(), can corrupt data e.g. forget to signal(), can lead to infinite delay generally get more complex as add more semaphores Used internally in some OSes and libraries, but generally deprecated for other mechanisms 28 14

Summary + next <me General mutual exclusion: how to [not] do it Hardware support for mutual exclusion Semaphores for mutual exclusion, process synchronisaon, and resource allocaon Two- party and generalised producer- consumer rela<onships Mul<- reader single- writer locks Next <me: Condi<onal cri<cal regions (CCRs); Monitors Condi<on variables; signal- and- wait vs. signal- and- con<nue Concurrency in prac<ce; concurrency primi<ves wrap- up 29 15