CSE 425: Concurrency II Semaphores and Mutexes Can avoid bad inter-leavings by acquiring locks –Guard access to a shared resource to take turns using it.

Slides:



Advertisements
Similar presentations
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
Advertisements

Operating Systems Part III: Process Management (Process Synchronization)
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.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
E81 CSE 532S: Advanced Multi-Paradigm Software Development Chris Gill Department of Computer Science and Engineering Washington University in St. Louis.
CSE 425: Semantic Analysis Semantic Analysis Allows rigorous specification of a program’s meaning –Lets (parts of) programming languages be proven correct.
CSE 425: Semantics II Implementing Scopes A symbol table is in essence a dictionary –I.e., every name appears in it, with the info known about it –Usually.
CSE 425: Logic Programming I Logic and Programs Most programs use Boolean expressions over data Logic statements can express program semantics –I.e., axiomatic.
PARALLEL PROGRAMMING with TRANSACTIONAL MEMORY Pratibha Kona.
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.
1 Sharing Objects – Ch. 3 Visibility What is the source of the issue? Volatile Dekker’s algorithm Publication and Escape Thread Confinement Immutability.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
9/8/2015cse synchronization-p1 © Perkins DW Johnson and University of Washington1 Synchronization Part 1 CSE 410, Spring 2008 Computer Systems.
Parallel Programming Philippas Tsigas Chalmers University of Technology Computer Science and Engineering Department © Philippas Tsigas.
Simple Wait-Free Snapshots for Real-Time Systems with Sporadic Tasks Håkan Sundell Philippas Tsigas.
Chapter 6 Process Synchronization Copyright © 2008.
CSE 425: Data Types II Survey of Common Types I Records –E.g., structs in C++ –If elements are named, a record is projected into its fields (e.g., via.
CSE 425: Concurrency III Monitors A monitor is a higher level construct for synchronizing multiple threads’ access to a common code segment –Can implement.
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.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
Maged M.Michael Michael L.Scott Department of Computer Science Univeristy of Rochester Presented by: Jun Miao.
CSE 425: Target Machine Architecture Target Machine Details Many architectures can be similar in overall structure –E.g., Von Neumann with CISC instruction.
CSE 425: Syntax II Context Free Grammars and BNF In context free grammars (CFGs), structures are independent of the other structures surrounding them Backus-Naur.
CSE 425: Control Flow I Categories of Control Flow Constructs Sequencing –order of expressions and statements Selection –if, else, switch Iteration –loops.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Lecture 8 Page 1 CS 111 Online Other Important Synchronization Primitives Semaphores Mutexes Monitors.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
CSE 425: Control Abstraction I Functions vs. Procedures It is useful to differentiate functions vs. procedures –Procedures have side effects but usually.
Wait-Free Multi-Word Compare- And-Swap using Greedy Helping and Grabbing Håkan Sundell PDPTA 2009.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
CSE 425: Control Abstraction II Exception Handling Previous discussion focuses on normal control flow –Sometimes program reaches a point where it cannot.
CSE 153 Design of Operating Systems Winter 2015 Midterm Review.
Synchronization CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han.
CS 153 Design of Operating Systems Winter 2016 Lecture 7: Synchronization.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
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 ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
Kernel Synchronization David Ferry, Chris Gill CSE 522S - Advanced Operating Systems Washington University in St. Louis St. Louis, MO
C++11 Atomic Types and Memory Model
Names and Attributes Names are a key programming language feature
Background on the need for Synchronization
Process Synchronization
Chapter 5: Process Synchronization
Transactional Memory Semaphores, monitors, and conditional critical regions all suffer from limitations based on lock semantics Naïve synchronization may.
Kernel Synchronization II
Topic 6 (Textbook - Chapter 5) Process Synchronization
Threading And Parallel Programming Constructs
Module 7a: Classic Synchronization
Iteration Implemented through loop constructs (e.g., in C++)
Lecture 2 Part 2 Process Synchronization
Parallelism and Concurrency
Delayed Evaluation Special forms in Scheme (e.g., if and cond) do not use applicative order evaluation Only one of two or more expressions is actually.
Kernel Synchronization II
Userspace Synchronization
CSE 153 Design of Operating Systems Winter 19
Chapter 6: Synchronization Tools
CSE 153 Design of Operating Systems Winter 2019
“The Little Book on Semaphores” Allen B. Downey
Process/Thread Synchronization (Part 2)
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

CSE 425: Concurrency II Semaphores and Mutexes Can avoid bad inter-leavings by acquiring locks –Guard access to a shared resource to take turns using it Dijkstra’s semaphore mechanism is one example Sem s(n); Delay(s); [critical region of code] Signal(s); –Where semaphore S gives up to n threads access at a time –Implement via a test-and-set instruction, spin-locks, etc. A binary semaphore (a.k.a. a mutex) if n == 1 –Encodes basic common semantics for mutual exclusion –Can allow optimized implementation (e.g., Linux futexes avoid system calls unless there is contention for the lock) Can implement either one using the other –Update a counter within mutex-guarded method –Initialize a semaphore with a count of 1

CSE 425: Concurrency II Deadlocks and other Issues Synchronization may cause deadlocks –Cyclic dependence, mutual exclusion lead to deadlock –Even if a deadlock has not occurred yet, code may reach a path on which deadlock becomes unavoidable Protocols/mechanisms to avoid/detect/break deadlock –E.g., via Dijkstra’s Banker’s algorithm, timed locking, etc. Fairness/liveness of lock access scheduling matters –Order in which threads are given access to a lock may vary Accidental complexity also matters –E.g., user’s ability to mis-configure locking and concurrency –Motivates alternate uses of mutexes and/or semaphores –Encapsulating locks within type-safe object model may help

CSE 425: Concurrency II Nonblocking (Lock-Free or Wait-Free) Lock-free behavior never blocks (but may live-lock) –Suspension of one thread doesn’t impede others’ progress –Tries to do something, and if it cannot just tries again –E.g., while(head.compare_exchange_weak(n->next,n)); Wait-free behavior never starves a thread –Progress of each is guaranteed (bounded number of retries) Lock-free data structures try for maximum concurrency –E.g., ensuring some thread makes progress at every step –May not be strictly wait-free but that’s something to aim for Watch out for performance costs in practice –E.g., atomic operations are slower, may not be worth it –Some platforms may not relax memory consistency well

CSE 425: Concurrency II C++11 Atomic Types Many atomic types in C++11, some are lock-free –Always lock-free: std::atomic_flag –If it matters, must test others with is_lock_free() Also can specialize std::atomic<> class template –This is already done for many standard non-atomic type –Can also do this for your own types that implement a trivial copy-assignment operator, are bitwise equality comparable Watch out for semantic details –E.g., bitwise evaluation of float, double, etc. representations –Equivalence may differ under atomic operations

CSE 425: Concurrency II Today’s Studio Exercises We’ll code up ideas from Scott Chapter 12.3 –Using threads, mutexes, and other C++11 types –Looking at synchronization, deadlock, and concurrency Today’s exercises are again in C++ –Please take advantage of the on-line tutorial and reference manual pages that are linked on the course web site –The Makefile provided last time also may be helpful –As always, please ask us for help as needed When done, send an with your answers to the course account, with subject line “Concurrency Studio II”