Concurrency.

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

Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Chapter 5 Concurrency: Mutual Exclusion and Synchronization Principals of Concurrency Mutual Exclusion: Hardware Support Semaphores Readers/Writers Problem.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
CSC321 Concurrent Programming: §3 The Mutual Exclusion Problem 1 Section 3 The Mutual Exclusion Problem.
Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Process Synchronization Continued 7.2 The Critical-Section Problem.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Chapter 6 (a): Synchronization.
Concurrency: Mutual Exclusion and Synchronization - Chapter 5 (Part 2)
Chapter 6: Process Synchronization
Background Concurrent access to shared data can lead to inconsistencies Maintaining data consistency among cooperating processes is critical What is wrong.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: 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.
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.
China’s Software Industry August 2006 Instructor: Hengming Zou, Ph.D.
Parallel Processing (CS526) Spring 2012(Week 6).  A parallel algorithm is a group of partitioned tasks that work with each other to solve a large problem.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization
Chapter 6: Process Synchronization. Outline Background Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Objectives Understand.
University of Pennsylvania 9/19/00CSE 3801 Concurrent Processes CSE 380 Lecture Note 4 Insup Lee.
1 Chapter 6: Concurrency: Mutual Exclusion and Synchronization Operating System Spring 2007 Chapter 6 of textbook.
Synchronization (other solutions …). Announcements Assignment 2 is graded Project 1 is due today.
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.
Hardware solutions So far we have looked at software solutions for the critical section problem. –algorithms whose correctness does not rely on any other.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
Process Synchronization Ch. 4.4 – Cooperating Processes Ch. 7 – Concurrency.
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
1 Lecture 9: Synchronization  concurrency examples and the need for synchronization  definition of mutual exclusion (MX)  programming solutions for.
Concurrency, Mutual Exclusion and Synchronization.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Introduction to Concurrency.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion.
TANNENBAUM SECTION 2.3 INTERPROCESS COMMUNICATION OPERATING SYSTEMS.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrency: Mutual Exclusion and Synchronization Chapter 5.
1 Concurrent Processes. 2 Cooperating Processes  Operating systems allow for the creation and concurrent execution of multiple processes  concurrency.
Operating Systems CMPSC 473 Mutual Exclusion Lecture 11: October 5, 2010 Instructor: Bhuvan Urgaonkar.
1 Lecture 8: Concurrency: Mutual Exclusion and Synchronization Advanced Operating System Fall 2012.
Computer Architecture and Operating Systems CS 3230: Operating System Section Lecture OS-5 Process Synchronization Department of Computer Science and Software.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Synchronization Questions answered in this lecture: Why is synchronization necessary? What are race conditions, critical sections, and atomic operations?
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
Chapter 5 Concurrency: Mutual Exclusion and Synchronization Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee.
G.Anuradha Reference: William Stallings
Concurrency.
Chapter 5: Process Synchronization
143a discussion session week 3
Lecture 11: Mutual Exclusion
Concurrency: Mutual Exclusion and Synchronization
Designing Parallel Algorithms (Synchronization)
Critical Sections User Software Solutions Dekker’s Algorithm
Grades.
Concurrency: Mutual Exclusion and Process Synchronization
Chapter 6: Process Synchronization
Lecture 11: Mutual Exclusion
Chapter 6: Synchronization Tools
Foundations and Definitions
Process/Thread Synchronization (Part 2)
CSE 542: Operating Systems
Chapter 5 Mutual Exclusion(互斥) and Synchronization(同步)
Presentation transcript:

Concurrency

Concurrency Interleaved or overlapped execution Examples from general purpose digital computing: Threads Multiprogramming Multiprocessing Examples from computing in nature: Team projects Transportation Food chain Allows more efficient execution, design flexibility

Challenges of concurrency Cannot predict relative execution speed Cannot know which process* will run next Effective allocation of shared resources Memory (e.g. shared variables) I/O devices (or associated data structures) Testing and debugging Program behaviour cannot be reliably replicated * In this discussion “process” is used to mean an entity of dispatch

Race conditions Final result depends on order of execution Can happen when two or more processes modify a shared resource For example …

… a threaded mail server Suppose Mail server counts the emails received each day Emails received by any of several worker threads The assembly language description of the worker threads might include the following: LOAD MailCount ADD 1 STORE MailCount

A Possible Execution Trace Thread 1 Load MailCount Add 1 Store MailCount Thread 2 Load MailCount Add 1 Store MailCount

Preventing Race Conditions Restrict access to shared resources Requires restricting access to code (at some level) that accesses shared resources (“critical section”) Only when one process has completed execution of the critical section can another process be allowed to begin executing it (“mutual exclusion”)

Requirements for a mutual exclusion solution Only one process at a time is allowed into its critical section (CS) To prevent race conditions A process that halts in its non-CS must do so without interfering with other processes. Definition of critical section Process waiting for CS must not be delayed indefinitely To prevent deadlock and starvation Whenever there is no process in the CS and at least one process waiting to enter the CS, one must be permitted access To prevent starvation No assumptions are made about the relative speeds or number of processes The time that any one process remains inside its critical section is finite Assumption

Four different approaches Hardware Turn off interrupts Special instruction(s) Software Application layer System layer (OS/compiler)

1. Disable interrupts void func() { <non CS> /* enable interrupts */ } Satisfies requirements No interrupts means no mode switches No mode switches means no process switches No process switches means no race condition Simple and efficient Appropriate for some embedded systems Insecure Trusts application to announce exit from critical section Only applicable on single processor systems

2. Special Instruction(s) TESTSET X1, X2: Test X1, and set X2 accordingly Executes atomically – nothing else executes between test operation and set operation! For example (assuming X1 and X2 are stored in registers R1 and R2): If register R1 contains the value 0, store the value 1 in both register R1 and R2 Otherwise, leave R1 unchanged and store the value 0 in register R2. Pros? Cons? Example application (busy wait): int bolt = false; void main() { parbegin( P( 0 ),P( 1 )…P( n ) ) } void P( int j ) { while( ! TestSet( bolt ) ) { /* do nothing */ <CS> bolt = false; <non-CS>

3. Application layer Relies on hardware support Typical assumption: machine language instructions cannot concurrently modify a memory location

Dekker’s Algorithm (1) Discussed in Appendix A (Figure A.2, p. 375) for the case of two processes Global variable indicates next process to receive access to critical section Processes busy wait (a.k.a. spin wait) while checking the global variable When a process detects that no other process is in the critical section, it asserts its entry (i.e. updates the global variable) Issues: Processes are strictly ordered (coroutines) Process failure (in or out of critical section)

Dekker’s Algorithm (2) Global variable for each process indicating its presence in critical section Processes busy wait while checking other processes’ presence When a process detects that no other process is in the critical section, it asserts its entry (i.e. updates its global variable) Issues: Process failure in critical section (this does not violate requirements) No mutual exclusion (two processes check global variables, then enter critical section)

Dekker’s Algorithm (3) Global variable for each process indicating its presence in critical section Processes first assert their entry, then busy wait while checking other processes’ presence Issues: Process failure in critical section Deadlock (two processes assert their entry, then busy wait)

Dekker’s Algorithm (4) Global variable for each process indicating its need to enter critical section Processes first assert their need to enter, then busy wait checking other processes’ need to enter Unassert their own need Wait Assert it again Issues: Process failure in critical section “Livelock” starvation (two processes assert their needs, then unassert them, then sequence repeats indefinitely)

Dekker’s Algorithm (Final) Global variables indicating Each process’ need to enter critical section Which process is first in line for the critical section Processes first assert their need to enter, then busy wait Check other processes’ need to enter If they are not first in line Unassert their own need Wait Assert it again Upon entering critical section, processes update the global variable indicating which process is next Issue: Fairness (how to decide which process is next)

Peterson’s Algorithm Discussed in Appendix A (Figure A.3, p. 736) for the case of two processes More elegant than Dekker’s algorithm Generalization to n processes left as an exercise for the student