Atomicity, Mutex, and Locks

Slides:



Advertisements
Similar presentations
Symmetric Multiprocessors: Synchronization and Sequential Consistency.
Advertisements

Threads Cannot be Implemented As a Library Andrew Hobbs.
Synchronization. How to synchronize processes? – Need to protect access to shared data to avoid problems like race conditions – Typical example: Updating.
Global Environment Model. MUTUAL EXCLUSION PROBLEM The operations used by processes to access to common resources (critical sections) must be mutually.
Chapter 2 Processes and Threads
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.
Concurrent Programming James Adkison 02/28/2008. What is concurrency? “happens-before relation – A happens before B if A and B belong to the same process.
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
Mutual Exclusion.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
Cpr E 308 Spring 2004 Recap for Midterm Introductory Material What belongs in the OS, what doesn’t? Basic Understanding of Hardware, Memory Hierarchy.
Concurrency.
EEE 435 Principles of Operating Systems Interprocess Communication Pt I (Modern Operating Systems 2.3)
Avishai Wool lecture Introduction to Systems Programming Lecture 4 Inter-Process / Inter-Thread Communication.
Synchronization Principles. Race Conditions Race Conditions: An Example spooler directory out in 4 7 somefile.txt list.c scores.txt Process.
Chapter 2.3 : Interprocess Communication
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
1 Sharing Objects – Ch. 3 Visibility What is the source of the issue? Volatile Dekker’s algorithm Publication and Escape Thread Confinement Immutability.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
CS510 Concurrent Systems Class 5 Threads Cannot Be Implemented As a Library.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
1 Interprocess Communication Race Conditions Two processes want to access shared memory at same time.
Hardware solutions So far we have looked at software solutions for the critical section problem. –algorithms whose correctness does not rely on any other.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
CS510 Concurrent Systems Introduction to Concurrency.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
11/18/20151 Operating Systems Design (CS 423) Elsa L Gunter 2112 SC, UIUC Based on slides by Roy Campbell, Sam.
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
Operating Systems CMPSC 473 Signals, Introduction to mutual exclusion September 28, Lecture 9 Instructor: Bhuvan Urgaonkar.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
Process Synchronization. Concurrency Definition: Two or more processes execute concurrently when they execute different activities on different devices.
December 1, 2006©2006 Craig Zilles1 Threads & Atomic Operations in Hardware  Previously, we introduced multi-core parallelism & cache coherence —Today.
Kernel Synchronization David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO
Atomic Operations in Hardware
Multithreading Tutorial
Atomic Operations in Hardware
Chapter 5: Process Synchronization
143a discussion session week 3
Lecture 11: Mutual Exclusion
Symmetric Multiprocessors: Synchronization and Sequential Consistency
Symmetric Multiprocessors: Synchronization and Sequential Consistency
Implementing synchronization
Multithreading Tutorial
Mutual Exclusion.
Lecture 2 Part 2 Process Synchronization
Background and Motivation
Global Environment Model
Dr. Mustafa Cem Kasapbaşı
Multithreading Tutorial
Kernel Synchronization I
Multithreading Tutorial
Kernel Synchronization II
Race Conditions David Ferry CSCI 3500 – Operating Systems
Lecture 11: Mutual Exclusion
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
Chapter 6: Synchronization Tools
Process Synchronization
“The Little Book on Semaphores” Allen B. Downey
Process Synchronization
CSE 332: Concurrency and Locks
Presentation transcript:

Atomicity, Mutex, and Locks David Ferry CSCI 3500 – Operating Systems Saint Louis University St. Louis, MO 63103

CSCI 3500 - Operating Systems Critical Sections Recall the problem with race conditions: Suppose x=0 initially: Thread 1 Thread 2 u = x v = x u = u + 1 v = v * 2 x = u x = v A critical section is any piece of code that should not execute simultaneously with another piece of code. CSCI 3500 - Operating Systems

CSCI 3500 - Operating Systems Critical Sections Recall the problem with race conditions: Suppose x=0 initially: Thread 1 Thread 2 u = x v = x u = u + 1 v = v * 2 x = u x = v A critical section is any piece of code that should not execute simultaneously with another piece of code (potentially itself- see push() from last time). CSCI 3500 - Operating Systems

Locks & Mutex (Mutual Exclusion) We need to prevent simultaneous access to critical sections. Something like: mutex m; lock(m); //Do critical section unlock(m); Where lock(m): Proceeds if m is unlocked If not, blocks until m is unlocked CSCI 3500 - Operating Systems

Implementing lock() and unlock()? lock() should block until the mutex variable is unlocked, a naïve implementation: int mutex = 0; lock( mutex ): unlock( mutex ): while(1){ mutex = 0; if( mutex == 0 ){ mutex = 1; break; } CSCI 3500 - Operating Systems

CSCI 3500 - Operating Systems But wait… Another race condition! lock( mutex ) translates to: while(1){ start: if( mutex == 0 ){ load m to register mutex = 1; compare m break; jump if not zero->start } store 1 to memory } What if we could simultaneously test and update the value of the mutex variable? CSCI 3500 - Operating Systems

CSCI 3500 - Operating Systems Atomic Operations An atomic operation is one that is indivisible. From the previous slide, what we want is this: if ( m == 0 ) //Test the value of m m = 1; //Set the value of m If this were atomic, then it looks as though both statements execute simultaneously. The outside world sees the “before” or “after” but never any intermediate states. In particular, a second thread can’t come along and read the value of m after it has already been read but before the assignment m=1 takes place. CSCI 3500 - Operating Systems

CSCI 3500 - Operating Systems Hardware to the Rescue Hardware designers can give us a special atomic test and set instruction. Then: lock(mutex): while(1){ int success = hardware_test_and_set(mutex); if( success ) break; } The lock() functions we use probably look something like this, modulo performance optimizations. CSCI 3500 - Operating Systems

CSCI 3500 - Operating Systems Unsatisfying? Do we have to rely on hardware giving us a special “solve everything” instruction? In fact, mutual exclusion can be accomplished only with regular memory loads and stores (i.e. in software). The first such solution, Dekker’s Algorithm, was published by Edsger Dijkstra in 1968. Leslie Lamport published the first general solution called the Bakery Algorithm in 1974. Other solutions exist since then. Hardware implementations tend to be faster and easier to implement. However, software-only solutions can be useful, such as when portability is important. CSCI 3500 - Operating Systems

Other Atomic Operations We will see other atomic operations in the studios. E.g.: __sync_fetch_and_add(); This function synchronously (atomically) loads a value from memory and adds to it. Thread 1: Thread 2: x++; x++; CSCI 3500 - Operating Systems

Solving Race Conditions Critical sections occur when variables are shared between threads, in particular when shared variables are written to. lock() and unlock(): Can encapsulate any critical section Code/data can be any size Atomic operations: Usually faster, if applicable CSCI 3500 - Operating Systems