Safety and liveness for critical sections

Similar documents
Thread Synchronization: Too Much Milk

Thread Synchronization: Foundations. Properties. Safety properties. Edsger s perspective. Nothing bad happens

Concurrency Problem. Thread Synchronization: Too Much Milk. The Fundamental Issue. Critical Sections. Order of thread execution is non-deterministic

Today: Synchronization. Recap: Synchronization

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2002

EECS 482 Introduction to Operating Systems

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

Chapter 5: Synchronization 1

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

Atomicity and Virtualization. Atomicity. Critical section. Edsger s perspective. Virtualizing a resource requires managing concurrent accesses

Chapter 6: Process [& Thread] Synchronization. CSCI [4 6] 730 Operating Systems. Why does cooperation require synchronization?

Page 1. Recap: ATM Bank Server" Recap: Challenge of Threads"

Threads and Too Much Milk! CS439: Principles of Computer Systems January 31, 2018

Threads and Too Much Milk! CS439: Principles of Computer Systems February 6, 2019

Threads and Critical Sections. Otto J. Anshus, Thomas Plagemann, Tore Brox-Larsen, Kai Li

! Why is synchronization needed? ! Synchronization Language/Definitions: ! How are locks implemented? Maria Hybinette, UGA

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

Concurrency & Synchronization. COMPSCI210 Recitation 25th Feb 2013 Vamsi Thummala Slides adapted from Landon Cox

CSCI [4 6] 730 Operating Systems. Example Execution. Process [& Thread] Synchronization. Why does cooperation require synchronization?

PESIT Bangalore South Campus

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

CSE 153 Design of Operating Systems

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

Lecture #7: Implementing Mutual Exclusion

Page 1. CS162 Operating Systems and Systems Programming Lecture 6. Synchronization. Goals for Today

CS370 Operating Systems

Mutual Exclusion and Synchronization

CS370 Operating Systems

PROCESS SYNCHRONIZATION

Pre- and post- CS protocols. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 7. Other requirements for a mutual exclusion algorithm

Page 1. Why allow cooperating threads?" Threaded Web Server"

Chapters 5 and 6 Concurrency

Part II Process Management Chapter 6: Process Synchronization

Threading and Synchronization. Fahd Albinali

Process Synchronization

Implementing Mutual Exclusion. Sarah Diesburg Operating Systems CS 3430

Last class: Today: CPU Scheduling. Start synchronization

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

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

Mutual Exclusion. 1 Formal problem definitions. Time notion CSE /17/2015. Outline of this lecture:

Synchronization COMPSCI 386

CS 153 Design of Operating Systems Winter 2016

Concurrent & Distributed Systems Supervision Exercises

PROVING THINGS ABOUT PROGRAMS

Chapter 6: Process Synchronization

Operating Systems CMPSC 473. Synchronization February 21, Lecture 11 Instructor: Trent Jaeger

Fall 2004 CS414 Prelim 1

Models of concurrency & synchronization algorithms

Non-blocking Array-based Algorithms for Stacks and Queues. Niloufar Shafiei

2 Threads vs. Processes

Remaining Contemplation Questions

Multiprocessor Synchronization

Concurrency: Mutual Exclusion and Synchronization

Introducing Shared-Memory Concurrency

Asynchronous Programming Under the Hood. Week 6

Coordination 1. To do. Mutual exclusion Election algorithms Next time: Global state. q q q

Introduction to OS Synchronization MOS 2.3

10/17/2011. Cooperating Processes. Synchronization 1. Example: Producer Consumer (3) Example

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

Concurrency. Glossary

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

CSE 153 Design of Operating Systems Fall 2018

September 23 rd, 2015 Prof. John Kubiatowicz

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

G52CON: Concepts of Concurrency

Dekker s algorithms for Semaphores Implementation. ALI TARIQ AL-KHAYYAT ( ) Submitted to : Prof. Dr. Huseyin Balik

A house of cards? Advanced Synchronization and Deadlock. Contra Threads: Events. Contra Threads: Events

Dealing with Issues for Interprocess Communication

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

CS 361 Concurrent programming Drexel University Fall 2004 Lecture 8. Proof by contradiction. Proof of correctness. Proof of mutual exclusion property

Synchronization. CISC3595/5595 Fall 2015 Fordham Univ.

G52CON: Concepts of Concurrency

Interprocess Communication By: Kaushik Vaghani

SPIN, PETERSON AND BAKERY LOCKS

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

Concurrency & Parallelism. Threads, Concurrency, and Parallelism. Multicore Processors 11/7/17

Dept. of CSE, York Univ. 1

Concurrency - II. Recitation 3/24 Nisarg Raval Slides by Prof. Landon Cox and Vamsi Thummala

Operating Systems. Synchronization

Operating Systems. Synchronisation Part I

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

CS 450 Exam 2 Mon. 4/11/2016

Synchronization for Concurrent Tasks

Threads, Concurrency, and Parallelism

Synchronization Spinlocks - Semaphores

Concurrent Processes Rab Nawaz Jadoon

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

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

Consistency. CS 475, Spring 2018 Concurrent & Distributed Systems

CS-537: Midterm Exam (Spring 2001)

Introduction to Locks. Intrinsic Locks

Synchronization. Before We Begin. Synchronization. Credit/Debit Problem: Race Condition. CSE 120: Principles of Operating Systems.

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

Deadlock CS 241. March 19, University of Illinois

Last Class: Synchronization

Chapter 5 Concurrency: Mutual Exclusion. and. Synchronization. Operating Systems: Internals. and. Design Principles

Multiprocessors II: CC-NUMA DSM. CC-NUMA for Large Systems

Process Synchronization

Synchronization I. Jo, Heeseung

Transcription:

Safety and liveness for critical sections! At most k threads are concurrently in the critical section A. Safety B. Liveness C. Both! A thread that wants to enter the critical section will eventually succeed A. Safety B. Liveness C. Both! Bounded waiting: If a thread i is in entry section, then there is a bound on the number of times that other threads are allowed to enter the critical section (only 1 thread is alowed in at a time) before thread i s request is granted. A. Safety B. Liveness C. Both 14

Thread Synchronization: Too Much Milk 15

Implementing Critical Sections in Software Hard! The following example will demonstrate the difficulty of providing mutual exclusion with memory reads and writes Hardware support is needed! The code must work all of the time Most concurrency bugs generate correct results for some interleavings! Designing mutual exclusion in software shows you how to think about concurrent updates Always look for what you are checking and what you are updating A meddlesome thread can execute between the check and the update, the dreaded race condition 16

Thread Coordination Too much milk! Jack! Look in the fridge; out of milk! Go to store! Buy milk! Arrive home; put milk away Jill! Look in fridge; out of milk! Go to store! Buy milk! Arrive home; put milk away! Oh, no! Fridge and milk are shared data structures 17

Formalizing Too Much Milk! Shared variables Look in the fridge for milk check a variable Put milk away update a variable! Safety property At most one person buys milk! Liveness Someone buys milk when needed! How can we solve this problem? 18

How to think about synchronization code! Every thread has the same pattern Entry section: code to attempt entry to critical section Critical section: code that requires isolation (e.g., with mutual exclusion) Exit section: cleanup code after execution of critical region Non-critical section: everything else! There can be multiple critical regions in a program Only critical regions that access the same resource (e.g., data structure) need to synchronize with each other while(1) { Entry section Critical section Exit section Non-critical section 19

The correctness conditions! Safety Only one thread in the critical region! Liveness Some thread that enters the entry section eventually enters the critical region Even if some thread takes forever in non-critical region! Bounded waiting A thread that enters the entry section enters the critical section within some bounded number of operations.! Failure atomicity It is OK for a thread to die in the critical region Many techniques do not provide failure atomicity while(1) { Entry section Critical section Exit section Non-critical section 20

Too Much Milk: Solution #0 while(1) { if (nomilk) { // check milk (Entry section) if (nonote) { // check if roommate is getting milk leave Note; //Critical section buy milk; remove Note; // Exit section // Non-critical region! Is this solution 1. Correct 2. Not safe 3. Not live 4. No bounded wait 5. Not safe and not live What if we switch the order of checks?! It works sometime and doesn t some other times 21

Too Much Milk: Solution #1 turn := Jill // Initialization while(1) { while(turn Jack) ; //spin while (Milk) ; //spin buy milk; // Critical section turn := Jill // Exit section // Non-critical section while(1) { while(turn Jill) ; //spin while (Milk) ; //spin buy milk; turn := Jack // Non-critical section! Is this solution 1. Correct 2. Not safe 3. Not live 4. No bounded wait 5. Not safe and not live! At least it is safe 22

Solution #2 (a.k.a. Peterson s algorithm): combine ideas of 0 and 1 Variables: in i : thread T i is executing, or attempting to execute, in CS turn: id of thread allowed to enter CS if multiple want to Claim: We can achieve mutual exclusion if the following invariant holds before entering the critical section: {( in j (in j turn = i)) in i CS in i = false (( in 0 (in 0 turn = 1)) in 1 ) (( in 1 (in 1 turn = 0)) in 0 ) ((turn = 0) (turn = 1)) = false 23

Peterson s Algorithm in 0 = in 1 = false; Jack while (1) { in 0 := true; turn := Jack; while (turn == Jack && in 1 ) ;//wait Critical section in 0 := false; Non-critical section Jill while (1) { in 1 := true; turn := Jill; while (turn == Jill && in 0 );//wait Critical section in 1 := false; Non-critical section Safe, live, and bounded waiting But, only 2 participants 24

Too Much Milk: Lessons! Peterson s works, but it is really unsatisfactory Limited to two threads Solution is complicated; proving correctness is tricky even for the simple example While thread is waiting, it is consuming CPU time! How can we do better? Use hardware to make synchronization faster Define higher-level programming abstractions to simplify concurrent programming 25

Towards a solution The problem boils down to establishing the following right after entry i ( in j (in j turn = i)) in i = ( in j turn = i) in i How can we do that? entry i = in i := true; while (in j turn i); 26

We hit a snag Thread T 0 in 0 := true {in 0 while (in 1 turn 0); {in 0 ( in 1 turn = 0) CS 0 Thread T 1 in 1 := true {in 1 while (in 0 turn 1); {in 1 ( in 0 turn = 1) CS 1 The assignment to in 0 invalidates the invariant! 27

What can we do? Add assignment to turn to establish the second disjunct Thread T 0 in 0 := true; turn := 1; {in 0 while (in 1 turn 0); α 0 α 1 {in 0 ( in 1 turn = 0 at(α 1 ) ) CS 0 in 0 := false; NCS 0 Thread T 1 in 1 := true; turn := 0; {in 1 while (in 0 turn 1); {in 1 ( in 0 turn = 1 at(α 0 ) ) CS 1 in 1 := false; NCS 1 28

Safe? Thread T 0 in 0 := true; α 0 turn := 1; α 1 {in 0 while (in 1 turn 0); {in 0 ( in 1 turn = 0 at(α 1 ) ) Thread T 1 in 1 := true; turn := 0; {in 1 while (in 0 turn 1); {in 1 ( in 0 turn = 1 at(α 0 ) ) CS 0 in 0 := false; NCS 0 CS 1 in 1 := false; NCS 1 If both in CS, then in 0 ( in 1 at(α 1 ) turn = 0) in 1 ( in 0 at(α 0 ) turn = 1) at(α 0 ) at(α 1 ) = (turn = 0) (turn = 1) = false 29

Live? Thread T 0 {S 1 : in 0 (turn = 1 turn = 0) in 0 := true; {S 2 : in 0 (turn = 1 turn = 0) α 0 turn := 1; {S 2 while (in 1 turn 0); α 1 {S 3 : in 0 ( in 1 at(α 1 ) turn = 0) CS 0 {S 3 in 0 := false; {S 1 NCS 0 Thread T 1 {R 1 : in 0 (turn = 1 turn = 0) in 1 := true; {R 2 : in 0 (turn = 1 turn = 0) turn := 0; {R 2 while (in 0 turn 1); {R 3 : in 1 ( in 0 at(α 0 ) turn = 1) CS 1 {R 3 in 1 := false; {R 1 NCS 1 Non-blocking: T 0 before NCS 0, T 1 stuck at while loop S 1 R 2 in 0 (turn = 0) = in 0 in 1 in 0 (turn = 0) = false Deadlock-free: T 1 and T 0 at while, before entering the critical section S 2 R 2 (in 0 (turn = 0)) (in 1 (turn = 1)) (turn = 0) (turn = 1) = false 30

Bounded waiting? Thread T 0 in 0 := true; turn := 1; while (in 1 turn 0); CS 0 in 0 := false; NCS 0 Thread T 1 in 1 := true; turn := 0; while (in 0 turn 1); CS 0 in 1 := false; NCS 0 Yup! 31

Mutual Exclusion: Primitives and Implementation Considerations 32