02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.

Slides:



Advertisements
Similar presentations
Chapter 6: Process Synchronization
Advertisements

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.
Synchronization Principles Gordon College Stephen Brinton.
02/19/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
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.
Process Synchronization
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
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.
Chapter 6: Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Principles Module 6: Synchronization 6.1 Background 6.2 The Critical-Section.
02/25/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Synchronization Solutions
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.
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. 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.
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.
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.
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.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 22 Semaphores Classic.
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.
6.1 Silberschatz, Galvin and Gagne ©2005 Operating System Principles 6.5 Semaphore Less complicated than the hardware-based solutions Semaphore S – integer.
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
Topic 6 (Textbook - Chapter 5) Process Synchronization
Semaphore Originally called P() and V() wait (S) { while S <= 0
Module 7a: Classic Synchronization
Lecture 2 Part 2 Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Synchronization Tools
CSE 542: Operating Systems
Presentation transcript:

02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying an earlier edition of the course text Operating Systems Concepts with Java, by Silberschatz, Galvin, and Gagne. Many, if not all, the illustrations contained in this presentation come from this source.

02/17/2010CSCI 315 Operating Systems Design2 Race Condition A race occurs when the correctness of a program depends on one thread reaching point x in its control flow before another thread reaches point y. Races usually occurs because programmers assume that threads will take some particular trajectory through the execution space, forgetting the golden rule that threaded programs must work correctly for any feasible trajectory. Computer Systems A Programmer’s Perspective Randal Bryant and David O’Hallaron

02/17/2010CSCI 315 Operating Systems Design3 The Critical-Section Problem Solution 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 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. 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.)

02/17/2010CSCI 315 Operating Systems Design4 Algorithm 3 public class Algorithm_3 implements MutualExclusion { private volatile boolean flag[2]; private volatile int turn; public Algorithm_3() { flag[0] = false; flag[1] = false; turn = TURN_0; } // Continued on Next Slide

02/17/2010CSCI 315 Operating Systems Design5 Algorithm 3 – cont’d public void enteringCriticalSection(int t) { int other = 1 - t; flag[t] = true; turn = other; while(flag[other] && turn == other) Thread.yield(); } public void leavingCriticalSection(int t) { flag[t] = false; }

02/17/2010CSCI 315 Operating Systems Design6 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: –boolean getAndSet(boolean b) –void swap(boolean b)

02/17/2010CSCI 315 Operating Systems Design7 Semaphore as General Synchronization Tool 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 (also known as mutex locks). Note that one can implement a counting semaphore S as a binary semaphore. Provides mutual exclusion: Semaphore S(1); // initialized to 1 acquire(S); criticalSection(); release(S);

02/17/2010CSCI 315 Operating Systems Design8 Semaphore Implementation acquire(S) { value--; if (value < 0) { add this process to list block; } release(S) { value++; if (value <= 0) { remove some process P from list wakeup(P); }

02/17/2010CSCI 315 Operating Systems Design9 Semaphore Implementation Must guarantee that no two processes can execute acquire() and release() on the same semaphore at the same time. The implementation becomes the critical section problem: –Could now have busy waiting in critical section implementation But implementation code is short Little busy waiting if critical section rarely occupied –Applications may spend lots of time in critical section

02/17/2010CSCI 315 Operating Systems Design10 Monitor Semaphores are low-level synchronization resources. A programmer’s honest mistake can compromise the entire system (well, that is almost always true). We should want a solution that reduces risk. The solution can take the shape of high-level language constructs, as the monitor type: monitor monitor-name { // variable declarations public entry p1(…) { … } public entry p2(…) { … } A procedure within a monitor can access only local variables defined within the monitor. There cannot be concurrent access to procedures within the monitor (only one thread can be active in the monitor at any given time). Condition variables: queues are associated with variables. Primitives for synchronization are wait and signal.

02/17/2010CSCI 315 Operating Systems Design11 Monitor

02/17/2010CSCI 315 Operating Systems Design12 Deadlock and Starvation Deadlock – two or more processes are waiting indefinitely for an event that can be caused by only one of the waiting processes. Let S and Q be two semaphores initialized to 1 P 0 P 1 acquire(S); acquire(Q); acquire(Q); acquire(S);. release(S); release(Q); release(Q); release(S); Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended.

02/17/2010CSCI 315 Operating Systems Design13 The Dining-Philosophers Problem

02/17/2010CSCI 315 Operating Systems Design14 The Dining-Philosophers Problem thinking hungry eating State diagram for a philosopher

02/17/2010CSCI 315 Operating Systems Design15 The Dining-Philosophers Problem

02/17/2010CSCI 315 Operating Systems Design16 The Dining-Philosophers Problem

02/17/2010CSCI 315 Operating Systems Design17 The Dining-Philosophers Problem

02/17/2010CSCI 315 Operating Systems Design18 The Dining-Philosophers Problem

02/17/2010CSCI 315 Operating Systems Design19 The Dining-Philosophers Problem

02/17/2010CSCI 315 Operating Systems Design20 The Dining-Philosophers Problem Question: How many philosophers can eat at once? How can we generalize this answer for n philosophers and n chopsticks? Question: What happens if the programmer initializes the semaphores incorrectly? (Say, two semaphores start out a zero instead of one.) Question: How can we formulate a solution to the problem so that there is no deadlock or starvation?