5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling.

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.
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.
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.
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.
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.
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.
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
02/11/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
02/17/2010CSCI 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.
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.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
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.
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.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process 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.
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. 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 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.
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.
Chapter 6: Process Synchronization
Process Synchronization
Chapter 5: Process Synchronization
Process Synchronization: Semaphores
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
Lecture 9: Process Synchronization
Topic 6 (Textbook - Chapter 5) Process Synchronization
Process Synchronization
Module 7a: Classic Synchronization
Lecture 2 Part 2 Process Synchronization
Critical section problem
Grades.
Concurrency: Mutual Exclusion and Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Synchronization Tools
CSE 542: Operating Systems
Presentation transcript:

5.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 5: CPU Scheduling

5.2 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Algorithm Evaluation How to compare scheduling algorithms? How to determine which is a good algorithm for a given system Deterministic modeling – takes a particular predetermined workload and defines the performance of each algorithm for that workload Queueing models Simulation

5.3 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Evaluation of CPU schedulers by Simulation

5.4 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition End of Chapter 5

5.5 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 6: Process Synchronization

5.6 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization Monitors Synchronization Examples Atomic Transactions

5.7 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Objectives To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data To present both software and hardware solutions of the critical-section problem To introduce the concept of an atomic transaction and describe mechanisms to ensure atomicity

5.8 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Background Processes and threads provide concurrency Data sharing among cooperating processes/threads Simultaneous access to shared data (especially simultaneous writes) lead to data inconsistency Producer/Consumer problem example

5.9 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Producer while (count == BUFFER.SIZE) ; // do nothing // add an item to the buffer buffer[in] = item; in = (in + 1) % BUFFER.SIZE; ++count;

5.10 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Consumer while (count == 0) ; // do nothing // remove an item from the buffer item = buffer[out]; out = (out + 1) % BUFFER.SIZE; --count;

5.11 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Race Condition count++ can be implemented as register1 = count register1 = register1 + 1 count = register1 count-- can be implemented as register2 = count register2 = register2 - 1 count = register2

5.12 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Race Condition (Contd.) Consider a scenario with one producer and one consumer count++ and count-- executed simultaneously Count value should remain unaltered Execution interleaving with “count = 5” initially: T0: producer execute register1 = count {register1 = 5} T1: producer execute register1 = register1 + 1 {register1 = 6} T2: consumer execute register2 = count {register2 = 5} T3: consumer execute register2 = register2 - 1 {register2 = 4} T4: producer execute count = register1 {count = 6 } T5: consumer execute count = register2 {count = 4}

5.13 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Critical Section A conceptual tool to help programmers avoid race conditions A section of code where shared memory/resources (variables, files, tables, etc.) are modified At most one cooperating process can be in the critical region at any given point in time Processes needs to follow a protocol when they modify shared resources – Critical Section Problem Process need to request permission before they enter critical region Program structured as “Entry Section”, Critical Section”, “Exit Section” and “Remainder Section”

5.14 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Structure of a Typical Process

5.15 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Critical Section Solution Requirements  Mutual Exclusion - If process P i is executing in its critical section, then no other processes can be executing in their critical sections.  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.  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.

5.16 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Critical Section Solution Assumptions Assume that each process executes at a nonzero speed No assumption concerning relative speed of the N processes

5.17 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Peterson’s Solution Two process solution Theoretical solution – May not work correctly on modern architectures 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!

5.18 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition while (true) { flag[i] = true; turn = j; while (flag[j] && turn == j); critical section flag[i] = false; remainder section } Algorithm for Process P i

5.19 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Peterson’s Solution – Points to Note Each process plays the “nice guy” Asserts it is the other processes turn to enter into the critical section The infinite wait loop is broken if at least one of the following holds The other process is not interested in entering the critical region The other process’s write on the “turn” variable survived (i.e., turn has been set to this process)

5.20 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Peterson’s Solution – Correctness proof Mutual exclusion Impossible for both processes to break the while loop simultaneously – Why? Progress and bounded wait Process Pi can be stuck in the wait loop only if turn == j and flag[j] == true If both conditions hold -- Pj wants to be in critical region and it has necessary permission to do so When Pj exits the critical section it sets flag[j] to false Pi enters critical region after at most one entry by Pj When Pj is in the remainder region it has no effect on Pi’s entry into critical region

5.21 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Critical regions protected by locks Processes need to acquire lock before entering CR Acquiring and releasing locks are atomic operations while (true) { acquire lock critical section release lock remainder section } Locks – A Generic Hardware Paradigm

5.22 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Synchronization Hardware Modern machines provide special atomic hardware instructions Atomic mean non-interruptable (i.e., the instruction executes as one unit) getAndSet() -- Test memory word and set its value swap() – exchange the contents of two memory words

5.23 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Illustration of getAndSet and swap

5.24 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Solution using GetAndSet Instruction

5.25 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Solution using Swap Instruction

5.26 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Semaphore Synchronization tool for programmers Semaphore S – integer variable Two standard operations modify S: acquire() and release() Originally called P() (proberen) and V() (verhogen) Can only be accessed only via the above atomic operations

5.27 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Semaphore for Mutual Exclusion Binary semaphore – integer value can range only between 0 and 1 Also known as mutex locks

5.28 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Java Example Using Semaphores

5.29 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Java Example Using Semaphores

5.30 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Semaphore for Imposing Order P1 and P2 are concurrently running processes S1– Statement in process P1; S2 – Statement in Process P2 Ensure that S2 gets executed only after S1 Initialize semaphore synch to 0 Process P1:Process P2: S1;synch.acquire(); synch.release(); S2;

5.31 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Counting Semaphores Counting semaphore – integer value can range over an unrestricted domain Controlling access to a resource with finite number of instances Initialize semaphore to # of available instances A process wanting to access resource will do an acquire on the semaphore Processes will do a release on the semaphore after using the resource Processes will wait if all available resources are currently being used

5.32 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Semaphore Implementation Must guarantee that no two processes can execute acquire () and release () on the same semaphore at the same time Disadvantage: Required busy waiting Processes continually wait in the entry code Spinlock – another name for this type of semaphore Wastes CPU cycles