Race condition The scourge of parallel and distributed computing...

Slides:



Advertisements
Similar presentations
1 Interprocess Communication 1. Ways of passing information 2. Guarded critical activities (e.g. updating shared data) 3. Proper sequencing in case of.
Advertisements

Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Synchronization and Deadlocks
Chapter 6: Process Synchronization
5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
© 2009 Matthew J. Sottile, Timothy G. Mattson, and Craig E Rasmussen 1 Concurrency in Programming Languages Matthew J. Sottile Timothy G. Mattson Craig.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
Threading Part 4 CS221 – 4/27/09. The Final Date: 5/7 Time: 6pm Duration: 1hr 50mins Location: EPS 103 Bring: 1 sheet of paper, filled both sides with.
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Concurrent Processes Lecture 5. Introduction Modern operating systems can handle more than one process at a time System scheduler manages processes and.
OS Spring 2004 Concurrency: Principles of Deadlock Operating Systems Spring 2004.
5.6.2 Thread Synchronization with Semaphores Semaphores can be used to notify other threads that events have occurred –Producer-consumer relationship Producer.
Concurrency: Mutual Exclusion, Synchronization, Deadlock, and Starvation in Representative Operating Systems.
OS Fall’02 Concurrency: Principles of Deadlock Operating Systems Fall 2002.
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
1 Concurrency: Deadlock and Starvation Chapter 6.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
1 Concurrency: Deadlock and Starvation Chapter 6.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic,
Atomic Operations David Monismith cs550 Operating Systems.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
6.3 Peterson’s Solution The two processes share two variables: Int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical.
1 Announcements The fixing the bug part of Lab 4’s assignment 2 is now considered extra credit. Comments for the code should be on the parts you wrote.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic,
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
4061 Session 21 (4/3). Today Thread Synchronization –Condition Variables –Monitors –Read-Write Locks.
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
Copyright © Curt Hill Concurrent Execution An Overview for Database.
COSC 3407: Operating Systems Lecture 9: Readers-Writers and Language Support for Synchronization.
CSE 153 Design of Operating Systems Winter 2015 Midterm Review.
Concurrency in Shared Memory Systems Synchronization and Mutual Exclusion.
C H A P T E R E L E V E N Concurrent Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
CS 153 Design of Operating Systems Winter 2016 Lecture 7: Synchronization.
Process Synchronization. Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
CSE 451 Section 4. 2 Synchronization High-level Monitors Java synchronized method OS-level support Special variables – mutex, semaphor, condition var.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
NETW 3005 Monitors and Deadlocks. Reading For this lecture, you should have read Chapter 7. NETW3005 (Operating Systems) Lecture 06 - Deadlocks2.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community.
Background on the need for Synchronization
Process Synchronization
Chapter 5: Process Synchronization
Midterm review: closed book multiple choice chapters 1 to 9
Process Synchronization
Lecture 2 Part 2 Process Synchronization
Another Means Of Thread Synchronization
Concurrency: Mutual Exclusion and Process Synchronization
Conditions for Deadlock
Chapter 6 Synchronization Principles
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
CSE 153 Design of Operating Systems Winter 2019
CSE 542: Operating Systems
CSE 542: Operating Systems
Synchronization and liveness
Presentation transcript:

Race condition The scourge of parallel and distributed computing...

Race condition When multiple processes compete for a non-sharable resource With no synchronization there is a race to who claims/modifies the resource Read-Modify-Write (safe) Read-Modify-Read-Write-Modify-Write

Race condition (cont) reg := mem reg := reg + 1 mem := reg reg := mem reg := reg + 8 mem := reg mem Thread AThread B

Race condition (cont) reg := mem reg := reg + n mem := reg Timereg of Areg of Bmem (reg:=mem) 31 (reg:= reg+1) 8 (reg:= reg+8) 4? The race to write to mem creates a wrong result.

Race condition (cont) reg := mem reg := reg + n mem := reg Timereg of Areg of Bmem (reg:=mem) 0 31 (reg:= reg+1) 0 (reg:=mem) 48 (reg:= reg+8) 1 (mem:=A.reg) 58 (mem:=B.reg) 6 7 The update from process A is lost because it was not seen by B

Race condition (cont) reg := mem reg := reg + n mem := reg Timereg of Areg of Bmem (reg:=mem) 31 (reg:= reg+1) 41 (mem:=A.reg) 51 (reg:=mem) 69 (reg:= reg+8) 79 (mem:=B.reg) The two updates do not interact, result is as expected. By sheer luck, operations were serialized.

Non-serialized operations Change request #1 Change request #2 Change request #3 Resource

Serialized operations Change request #1 Change request #2 Change request #3 Resource The resource is protected by a mechanism which enforces serial access to it: Operating system Database manager Programming language synchronization

Synchronization Only one process may execute the critical section of code Acquire exclusive rights Execute critical section Release exclusive rights

Synchronization... acquire_permission(s); reg = mem; reg = reg + n; mem = reg; release_permission(s);... The entity s controls access to mem.

Synchronization Semaphore – guards n resources –Any thread can return a resource Mutex – guards 1 resource –Only the currently owning thread can return the resource Threads block until the resource is granted

Semaphore Counts the nof available resources Acquire: decrement, Release: increment No allocation or identification of resources An atomically modifiable counter Race conditions over the semaphore are prevented by –virtual machine / pgm language interpreter –operating system –hardware

Semaphore caveats Processes (i.e. programmers) must follow the protocol and not: –Forget to return a resource after use –Return a resource that was not requested –Hold a resource for too long –Use a resource anyway

Mutex A binary semaphore (one resource) The process acquiring the resource is the only one that can return it (ownership) The synchronized keyword in Java use any object as a monitor (a kind of mutex).

Monitor A mutex with a thread queue Threads queue for the mutex Threads can yield the resource (wait) Threads can alert other threads (notify) In Java, every object has a monitor The synchronized keyword

Unsharable resources Process memory (part of) Files Databases Printers, scanners, cameras, modems

R2 Deadlock Processes wait forever for all required resources to be released P2 R1 Waiting for R2 to be released Waiting for R1 to be released P1

R2 Livelock Processes attempt to break the deadlock by time-out and release, but no-one wins P2 R1 Waiting for R2 to be released Waiting for R1 to be released P1

Further fun reading Dijkstra, The Dining Philosophers problem Chandy/Misra solution to DPh problem Producer-consumer problem Sleeping barber problem Readers-writers problem