Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Module 6: Synchronization 6.1 Background 6.2 The Critical-Section.

Slides:



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

Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
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.
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.
Synchronization Principles Gordon College Stephen Brinton.
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.
Silberschatz, Galvin and Gagne ©2007 Operating System Concepts with Java – 7 th Edition, Nov 15, 2006 Process Synchronization (Or The “Joys” of Concurrent.
What we will cover… Process Synchronization Basic Concepts
02/11/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Concurrency, Mutual Exclusion and Synchronization.
Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization.
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.
Chapter 6: Process Synchronization
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts Essentials – 9 th Edition Chapter 5: Process Synchronization.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
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.
Operating Systems CSE 411 CPU Management Dec Lecture Instructor: Bhuvan Urgaonkar.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
CSE Operating System Principles 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 ©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) { /*
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Chapter 6: Process Synchronization.
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
Chapter 5: Process Synchronization
Background on the need for Synchronization
Chapter 5: Process Synchronization
Chapter 5: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Chapter 5: Process Synchronization
Chapter 6: Synchronization Tools
Topic 6 (Textbook - Chapter 5) Process Synchronization
Lecture 19 Syed Mansoor Sarwar
Module 7a: Classic Synchronization
Critical section problem
Grades.
Chapter 6: Process Synchronization
Chapter 6: Synchronization Tools
Process/Thread Synchronization (Part 2)
Presentation transcript:

Chapter 6: Synchronization

6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Module 6: Synchronization 6.1 Background 6.2 The Critical-Section Problem 6.3 Peterson’s Solution 6.4 Synchronization Hardware 6.5 Semaphores 6.6 Classic Problems of Synchronization 6.7 Monitors 6.8 Synchronization Examples 6.9 Atomic Transactions

6.3 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Independent process cannot affect or be affected by the execution of another process Cooperating process can affect or be affected by the execution of another process Advantages of process cooperation Information sharing Computation speed-up Modularity Convenience Review Interproces Communication

6.4 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Communications Models Message passing Shared memory

6.5 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Producer-Consumer Problem Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process unbounded-buffer places no practical limit on the size of the buffer bounded-buffer assumes that there is a fixed buffer size Shared-Memory Solution : Shared data #define BUFFER_SIZE 10 Typedef struct {... } item; item buffer[BUFFER_SIZE]; int in = 0; int out = 0;

6.6 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Bounded-Buffer – Producer() Method while (true) { /* produce an item in nextProduced*/ while (( (in + 1) % BUFFER_SIZE) == out) ; /* do nothing -- no free buffers */ buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; } Solution is correct, but can only use BUFFER_SIZE-1 elements Bounded-Buffer – Consumer() Method while (true) { while (in == out) ; // do nothing -- nothing to consume nextConsumed = buffer[out]; out = (out + 1) % BUFFER SIZE; /* consume the item in nextConsumed */; }

6.7 Silberschatz, Galvin and Gagne ©2005 Operating System Principles BUFFER_SIZE solution One more shared memory variable: counter

6.8 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Still Have Problems

6.9 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.1 Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes Suppose that we wanted to provide a solution to the consumer-producer problem (Section 3.4.1) that fills all the buffers. We can do so by having an integer count that keeps track of the number of full buffers. Initially, count is set to 0. It is incremented by the producer after it produces a new buffer and is decremented by the consumer after it consumes a buffer.

6.10 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Producer while (true) { /* produce an item and put in nextProduced */ while (counter == BUFFER_SIZE) ; // do nothing buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; counter++; } while (true) { while (counter == 0) ; // do nothing nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; /* consume the item in nextConsumed } Consumer

6.11 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Race Condition counter++ could be implemented as register1 = counter register1 = register1 + 1 counter = register1 counter-- could be implemented as register2 = counter register2 = register2 - 1 counter = register2

6.12 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Race Condition Consider this execution interleaving with “counter = 5” initially: T0: producer execute register1 = counter {register1 = 5} T1: producer execute register1 = register1 + 1 {register1 = 6} T2: consumer execute register2 = counter {register2 = 5} T3: consumer execute register2 = register2 - 1 {register2 = 4} T4: producer execute counter = register1 {count = 6 } T5: consumer execute counter = register2 {count = 4} If the order T4 and T5 is reversed, then the final state is count = 6

6.13 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.2 Solution to Critical-Section Problem 1. Mutual Exclusion - If process P i is executing in its critical section, then no other processes can be executing in their critical sections 2. Progress - If no process is executing in its critical section and some processes wish to enter their critical sections, then only those processes that are not executing in their remainder sections can participate in the decision on which will enter its critical section next, and this selection cannot be postponed indefinitely 3.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

6.14 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Solution to Critical-Section Problem 6.2 Solution to Critical-Section Problem Example kernel data structure that is subject to race conditions: List of open files in the OS Data structure for free/allocated memory Process lists Data structure for interrupts handling Approaches in handling critical sections in OS: Preemptive kernels Nonpreemptive kernels A preemptive kernel is more suitable for real-time programming, more responsive 訂正: p190 倒數第 3 列第 2 個字 應為 preemptive

6.15 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.3 Peterson’s Solution Two process solution Assume that the LOAD and STORE instructions are atomic; that is, cannot be interrupted. The two processes share two variables: int turn; boolean flag[2]; The variable turn indicates whose turn it is to enter the critical section. The flag array is used to indicate if a process is ready to enter the critical section. flag[i] = true implies that process P i is ready!

6.16 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Algorithm for Process P i while (true) { flag[i] = TRUE; turn = j; // j is i -1 while ( flag[j] && turn == j); CRITICAL SECTION flag[i] = FALSE; REMAINDER SECTION } entry section (acquire lock) exit section (release lock)

6.17 Silberschatz, Galvin and Gagne ©2005 Operating System Principles To prove Peterson’s solution is correct: Mutual exclusion is preserved The progress requirement is satisfied The bounded-waiting requirement is met

6.18 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.4 Synchronization Hardware Many systems provide hardware support for critical section code Uniprocessors – could disable interrupts Currently running code would execute without preemption Generally too inefficient on multiprocessor systems  Operating systems using this not broadly scalable Modern machines provide special atomic hardware instructions  Atomic = non-interruptable Either test memory word and set value Or swap contents of two memory words

6.19 Silberschatz, Galvin and Gagne ©2005 Operating System Principles TestAndndSet Instruction Definition: boolean TestAndSet (boolean *target) { boolean rv = *target; *target = TRUE; return rv: } Shared boolean variable lock initialized to false. Solution using TestandSet: while (true) { while ( TestAndSet (&lock )) ; // do nothing // critical section lock = FALSE; // remainder section }

6.20 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Swap Instruction Definition: void Swap (boolean *a, boolean *b) { boolean temp = *a; *a = *b; *b = temp: } Shared boolean variable lock initialized to FALSE; Each process has a local boolean variable key. Solution using Swap: while (true) { key = TRUE; while ( key == TRUE) Swap (&lock, &key ); // critical section lock = FALSE; // remainder section } Does not satisfy bounded-waiting

6.21 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Common data structure: boolean waiting[n] and boolean lock; Solution using TestandSet: while (true) { waiting[i] = TRUE; key = TRUE; while ( waiting[i] && key) key = TestAndSet(&lock); waiting[i] = FALSE; // critical section j = (i+1) %n; while ( ( j != i) && !waiting[j] ) j = (j + 1) %n; if (j == i) lock = FALSE; else waiting[j] = FALSE; // remainder section } Bounded-waiting mutual exclusion with TestAndSet()