Concurrency, Mutual Exclusion and Synchronization.

Slides:



Advertisements
Similar presentations
Operating Systems Part III: Process Management (Process Synchronization)
Advertisements

Concurrency: Mutual Exclusion and Synchronization Chapter 5.
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.
Chapter 6 Process Synchronization Bernard Chen Spring 2007.
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.
Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores.
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
1 Friday, June 16, 2006 "In order to maintain secrecy, this posting will self-destruct in five seconds. Memorize it, then eat your computer." - Anonymous.
The Critical-Section Problem
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem 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.
02/11/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Module 6: Synchronization 6.1 Background 6.2 The Critical-Section.
Synchronization Solutions
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
Instructor: Umar KalimNUST Institute of Information Technology Operating Systems Process Synchronization.
02/14/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Module 6: Process Synchronization.
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail,
Operating Systems CSE 411 CPU Management Oct Lecture 13 Instructor: Bhuvan Urgaonkar.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th Edition, Feb 8, 2005 Background Concurrent.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
1 Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Special Machine Instructions for Synchronization Semaphores.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Chap 6 Synchronization. Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Synchronization Background The Critical-Section.
Concurrency: Mutual Exclusion and Synchronization Chapter 5.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Chapter 7 -1 CHAPTER 7 PROCESS SYNCHRONIZATION CGS Operating System Concepts UCF, Spring 2004.
1 Concurrent Processes. 2 Cooperating Processes  Operating systems allow for the creation and concurrent execution of multiple processes  concurrency.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. Module 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Operating Systems Lecture Notes Synchronization Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Process Synchronization CS 360. Slide 2 CS 360, WSU Vancouver Process Synchronization Background The Critical-Section Problem Synchronization Hardware.
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.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Q: 請以實際例子說明 critical section 之意 ? 何謂 race condition? while (true) { /*
Chapter 6 Synchronization Dr. Yingwu Zhu. The Problem with Concurrent Execution Concurrent processes (& threads) often access shared data and resources.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Synchronization Background The Critical-Section Problem Peterson’s.
Process Synchronization 1. while (true) { /* produce an item and put in nextProduced */ while (count == BUFFER_SIZE) ; // do nothing buffer [in] = nextProduced;
Chapter 6: Process Synchronization
Process Synchronization
Chapter 5: Process Synchronization
Process Synchronization: Semaphores
Background on the need for Synchronization
Chapter 5: Process Synchronization
G.Anuradha Reference: William Stallings
Chapter 5: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Synchronization Tools
Topic 6 (Textbook - Chapter 5) Process Synchronization
The Critical-Section Problem
Lecture 19 Syed Mansoor Sarwar
Introduction to Cooperating Processes
Module 7a: Classic Synchronization
Lecture 2 Part 2 Process Synchronization
Grades.
Concurrency: Mutual Exclusion and Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Synchronization Tools
Presentation transcript:

Concurrency, Mutual Exclusion and Synchronization

Introduction Currency arises in three different contexts: Multiple applications – Multiple programs are allowed to dynamically share processing time. Structured applications – Some applications can be effectively programmed as a set of concurrent processes. Operating system structure – The OS themselves are implemented as set of processes. Concurrent processes (or threads) often need access to shared data and shared resources. Processes use and update shared data such as shared variables, files, and data bases. Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes.

Process Interactions

Cooperating Processes Shared data #define BUFFER_SIZE 10 typedef struct {... } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0; int counter = 0; Producer process item nextProduced; while (1) { while (counter == BUFFER_SIZE; /*do nothing */ buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; counter++; } Consumer process item nextConsumed; while (1) { while (counter == 0); nextConsumed = buffer[out]; out = (out + 1)%BUFFER_SIZE; counter--; }

Race Condition a situation where several processes access (read/write) shared data concurrently and the final value of the shared data depends upon which process finishes last The actions performed by concurrent processes will then depend on the order in which their execution is interleaved.

Example: Race Condition Updating a Variable

Process Synchronization To prevent race conditions, concurrent processes must be coordinated or synchronized. It means that neither process will proceed beyond a certain point in the computation until both have reached their respective synchronization point. Process synchronization is a generic term for the techniques used to delay and resume processes to implement process interactions.

Critical Section/Region Portion of code in a process, in which the process accesses a shared resource.

Critical Section/Region

Critical Section and Race Condition Multiprogramming allows logical parallelism, uses devices efficiently - but we lose correctness when there is a race condition. So we forbid logical parallelism inside critical section We lose some parallelism but we regain correctness.

The Critical-Section Problem The critical-section problem is to design a protocol that the processes can cooperate. The protocol must ensure that when one process is executing in its critical section, no other process is allowed to execute in its critical section.

Solution to Critical Section Problem - Requirements Mutual Exclusion. If process Pi is executing in its critical section, then no other processes can be executing in their critical sections. Implications: Critical sections better be focused and short. Better not get into an infinite loop in there. If a process somehow halts/waits in its critical section, it must not interfere with other processes.

Solution to Critical Section Problem - Requirements Progress. If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely. If only one process wants to enter, it should be able to. If two or more want to enter, one of them should succeed.

Solution to Critical Section Problem - Requirements Bounded Waiting. A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted. Assume that each process executes at a nonzero speed No assumption concerning relative speed of the n processes.

Types of Solutions Software solutions Algorithms whose correctness does not rely on any other assumptions. Hardware solutions Rely on some special machine instructions. Operating System solutions Provide some functions and data structures to the programmer through system/library calls. Programming Language solutions Linguistic constructs provided as part of a language.

Initial Attempts to Solve the Problem The execution of critical sections must be mutually exclusive. Each process must first request permission to enter its critical section. The section of code implementing this request is called the Entry Section (ES). The critical section (CS) might be followed by a Leave/Exit Section (LS).

Software Solutions First, we consider the case of 2 processes: Algorithm 1, 2 and 3 Initial notation Only 2 processes, P0 and P1 (Pi and Pj (i != j). General structure of process Pi (other process Pj) do { entry section critical section exit section reminder section } while (1); Processes may share some common variables to synchronize/coordinate their actions.

Algorithm 1 Satisfies mutual exclusion, but not progress

Algorithm 2 Satisfies mutual exclusion, but not progress requirement.

Algorithm 3 Meets all three requirements; solves the critical-section problem for two processes

What about Process Failure If all 3 criteria (ME, progress, bounded waiting) are satisfied, then a valid solution will provide robustness against failure of a process in its remainder section (RS). Since failure in RS is just like having an infinitely long RS. However, no valid solution can provide robustness against a process failing in its critical section (CS). A process Pi that fails in its CS does not signal that fact to other processes: for them Pi is still in its CS.

Drawback of Software Solutions Processes that are requesting to enter their critical section are busy waiting (consuming processor time needlessly). If critical sections are long, it would be more efficient to block processes that are waiting.

Mutual Exclusion – Hardware Support A process runs until it invokes an operating-system service or until it is interrupted Disabling interrupts guarantees mutual exclusion Processor is limited in its ability to interleave programs Multiprocessing disabling interrupts on one processor will not guarantee mutual exclusion

Special machine instructions Normally, access to a memory location excludes other access to the same location. Extension: designers have proposed machines instructions that perform 2 actions atomically (indivisible) on the same memory location (e.g., reading and writing). The execution of such an instruction is also mutually exclusive (even on Multiprocessors). Examples: test-and-set instruction (TS) and swap instruction

TestAndSet Synchronization Hardware Test and set modifies the content of a word atomically. Indivisible machine instruction Executed in single machine cycle boolean TestAndSet(boolean &target) { boolean rv = target; target = true; return rv; }

Mutual Exclusion with Test-and-Set

OS Solution: Semaphores Logically, a semaphore S is a shared integer variable that, apart from initialization, can only be accessed through 2 atomic and mutually exclusive operations: wait(S) signal(S)

Semaphores Modification to the value of semaphore (S) in the wait and signal is executed individually. In wait, the testing and possible modification of S must also be executed without interruption.

Usage of Semaphore Usage 1: Critical Section of n Processes Problem

Usage of Semaphore Usage 2: Synchronization of 2 or More Processes

Semaphore Implementation To implemented the semaphore, we define a semaphore as a record as: struct sem { int value; struct process *L; }; Assume two simple operations: block suspends the process that invokes it. wakeup(P) resumes the execution of a blocked process P.

Types of Semaphores Counting semaphore – integer value can range over an unrestricted domain. Binary semaphore – integer value can range only between 0 and 1; can be simpler to implement. Can implement a counting semaphore S as a binary semaphore.