V0.3 CSS430 Process Synchronization Textbook Chapter 6 Instructor: Stephen G. Dame These slides were adapted from the OSC textbook.

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
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.
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.
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.
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/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 6: Process Synchronization Dr. Mohamed Hefeeda.
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.
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.
Process Synchronization Continued 7.2 Critical-Section Problem 7.3 Synchronization Hardware 7.4 Semaphores.
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.
Operating Systems CSE 411 CPU Management Oct Lecture 14 Instructor: Bhuvan Urgaonkar.
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 Synchronisation (Pgs 225 – 267)
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
1 Interprocess Communication (IPC) - Outline Problem: Race condition Solution: Mutual exclusion –Disabling interrupts; –Lock variables; –Strict alternation.
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.
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.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 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.
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
Chapter 5: Process Synchronization
Chapter 5: Process Synchronization
Chapter 6: Process Synchronization
Chapter 6: Process Synchronization
Lecture 9: Process Synchronization
Topic 6 (Textbook - Chapter 5) Process Synchronization
Module 7a: Classic Synchronization
Lecture 2 Part 2 Process Synchronization
Critical section problem
Chapter 6: Process Synchronization
Chapter 6: Synchronization Tools
Process/Thread Synchronization (Part 2)
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

V0.3 CSS430 Process Synchronization Textbook Chapter 6 Instructor: Stephen G. Dame These slides were adapted from the OSC textbook slides (Silberschatz, Galvin, and Gagne), Professor Munehiro Fukuda and the instructor’s class materials. 1 CSS430 Operating Systems : Process Synchronization

V0.3 WKP 17 2 “Programming is the process of converting caffeine into error messages” - Unknown

V0.3 Learning Objectives 3 CSS430 Operating Systems : Process Synchronization  Introduction to the Critical-Section Problem  Discuss HW & SW solutions to C-S Problem  Atomic Transactions and mechanisms  Java Synchronization techniques: Mutex Mutex Semaphores Semaphores Monitors Monitors

V0.3 Revisiting Bounded Buffer 4 CSS430 Operating Systems : Process Synchronization Producer Process III Buffer[0] [1] [2] [3] [4] Consumer Process in=4out=1 Let’s Dissect the problem!

V0.3 Race Condition 5 CSS430 Operating Systems : Process Synchronization The outcome of concurrent thread execution depends on the particular order in which the access takes place  race condition! ++count: reg1 = mem[count]; reg1 = reg1 + 1; mem[count] = reg1; -- count: reg2 = mem[count]; reg2 = reg2 – 1; mem[count] = reg2; Producer: reg1 = mem[count]; {reg1=5} Producer: reg1 = reg1 + 1; {reg1=6} Consumer: reg2 = mem[count]; {reg2=5} Consumer: reg2 = reg2 – 1; {reg2=4} Producer: mem[count] = reg1; {count=6} Consumer: mem[count] = reg2; {count=4}

V0.3 The critical section is a block of code in which no two processes can be executing their instructions at the same time. The Critical Section (“CS”) 6 CSS430 Operating Systems : Process Synchronization while (true) { entry section critical section exit section remainder section }

V0.3 Critical Section (3) Requirements 7 CSS430 Operating Systems : Process Synchronization ① Mutual Exclusion. If process P i is executing in its critical section(CS), then no other processes can be executing in their corresponding critical sections. ② Progress. If no process is executing in its CS, and there exist some processes that wish to enter their CS, then the selection of the processes that will enter the CS next cannot be postponed indefinitely. ③ Bounded Waiting. A bound must exist on the number of times that other processes are allowed to enter their CS after a process has made a request to enter its CS and before that request is granted. Critical Section ① only one process When exiting from CS ② Pick up a process to enter ③ Bounded times When entering CS

V0.3 Peterson’s Solution (Algorithm) 8 CSS430 Operating Systems : Process Synchronization int turn;// shared var boolean flag[2];// shared var while (true) { flag[i] = true; turn = j; while(flag[j] && turn == j); critical section flag[i] = false; remainder section } P i, i=0 P j, j=1 Must demonstrate: ① Mutual Exclusion is preserved. ② Progress requirement is satisfied. ③ Bounded Waiting requirement is met Peterson's algorithm (solution) G.L. Peterson White Paper

V0.3 Mutual Exclusion Class 9 CSS430 Operating Systems : Process Synchronization

V0.3 Worker Thread 10 CSS430 Operating Systems : Process Synchronization

V0.3 Test Algorithm (Algorithm Factory) 11 CSS430 Operating Systems : Process Synchronization

V0.3 Algorithm 1 (yielding by turn) 12 CSS430 Operating Systems : Process Synchronization  Violates CS rule #1 – mutual exclusion Both threads 0 and 1 cannot be in CS at same time. Both threads 0 and 1 cannot be in CS at same time.

V0.3 Algorithm 2 (flag I’m using…) 13 CSS430 Operating Systems : Process Synchronization  Violates CS rule #2, #3 – progress + bounded waiting Thread 0 sets flag[0] true. Thread 0 sets flag[0] true. A context switch occurs. A context switch occurs. Thread 1 sets flag[1]. Thread 1 sets flag[1]. Thread 1 finds out flag[0] is true, and waits for Thread 0. Thread 1 finds out flag[0] is true, and waits for Thread 0. A context switch occurs. A context switch occurs. Thread 0 finds out flag[1] is true, and waits for Thread 1. Thread 0 finds out flag[1] is true, and waits for Thread 1.

V0.3 Algorithm 3 (Mixed of 1 and 2) (Peterson’s Solution) 14 CSS430 Operating Systems : Process Synchronization  Complies with all three CS rules– Even in case both threads declared, will enter CS in an orderly manner Even in case both threads declared, will enter CS in an orderly manner Turn eventually points to either thread A or B! Turn eventually points to either thread A or B!

V0.3 There no guarantees that Peterson’s solution will work correctly on modern computer architectures due to complex load and store instructions No Guarantees! 15 CSS430 Operating Systems : Process Synchronization

V0.3 Discussion 1 16 CSS430 Operating Systems : Process Synchronization ① What is the definition of atomicity, and where does it apply? ① What is the meaning of busy waiting? What is an alternative to busy waiting? ② Fill out the following table with your analysis of the different CS methods: AdvantagesDisadvantagesImplementation (HW, OS, or Language) TestandSet, Swap Semaphore Monitor

V CSS430 Operating Systems : Process Synchronization Synchronization  Software solutions: Algorithm 3 (Peterson’s Solution) works only for a pair of threads. How about a mutual execution (n > 2) threads? Algorithm 3 (Peterson’s Solution) works only for a pair of threads. How about a mutual execution (n > 2) threads? Lamport’s Algorithm (See Appendix). Interrupt Masking: Interrupt Masking:  Disables even time interrupts, thus not allowing preemption.  Malicious user program may hog CPU forever.  Operating systems using this technique are not broadly scalable  Hardware solutions: Modern machines provide special atomic hardware instructions Modern machines provide special atomic hardware instructions Many systems provide hardware support for critical section code Many systems provide hardware support for critical section code Atomic = non-interruptible sequence of instructions Atomic = non-interruptible sequence of instructions  Test-and-set (or read-modify-write)  Swap contents of two memory words

V0.3 Critical Section (“CS”) Locks 18 CSS430 Operating Systems : Process Synchronization while (true) { acquire lock critical section release lock remainder section }

V0.3 Hardware Data Simulator 19 CSS430 Operating Systems : Process Synchronization

V0.3 Sleep Utilities 20 CSS430 Operating Systems : Process Synchronization

V0.3 Mutual Exclusion 21 CSS430 Operating Systems : Process Synchronization

V0.3 Worker Thread – Test and Set 22 CSS430 Operating Systems : Process Synchronization

V0.3 H/W Algorithm  Test and Set 23 CSS430 Operating Systems : Process Synchronization

V0.3 Test and Set Factory 24 CSS430 Operating Systems : Process Synchronization

V CSS430 Operating Systems : Process SynchronizationRun! Test and Set

V0.3 H/W Algorithm  Swap 26 CSS430 Operating Systems : Process Synchronization

V0.3 Worker Thread - Swap 27 CSS430 Operating Systems : Process Synchronization

V0.3 Swap Factory 28 CSS430 Operating Systems : Process Synchronization

V CSS430 Operating Systems : Process SynchronizationRun!Swap

V0.3Semaphore 30 CSS430 Operating Systems : Process Synchronization Synchronization tool that does not require busy waiting at a user level Semaphore S – integer variable Two standard operations modify S: acquire() and release() Originally called P() and V() [Dutch P proberen, meaning “to test ”) and V (from verhogen, meaning “to increment”] Less complicated (AND implemented by import java.util.concurrent.*) Can only be accessed via two indivisible (atomic) operations P V PV PV acquire( ) { while value <= 0 ; // no-op value--; } release( ) { value++; wakeup( ); }

V0.3 Semaphore Eliminating Busy-Waiting 31 CSS430 Operating Systems : Process Synchronization Bee1 Bee3 Bee0 Bee2 Bee4 P V Bee1 Bee3 Bee0 Bee2 Bee4 P V Waiting List Wake one up

V0.3 Semaphore Worker Thread 32 CSS430 Operating Systems : Process Synchronization Private Data + Constructor Main Body of Worker BEE Critical and Remainder Sections Sim

V0.3 Semaphore Factory 33 CSS430 Operating Systems : Process Synchronization SemaphoreFactory Initialize and Start N “worker bees”

V CSS430 Operating Systems : Process SynchronizationRun!Semaphore

V0.3 Deadlock and Starvation 35 CSS430 Operating Systems : Process Synchronization  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 P(S);P(Q); P(Q);P(S);     V(Q);V(S); V(S);V(Q);  Starvation – indefinite blocking. A process may never be removed from the semaphore queue in which it is suspended. What if processes are waiting at P(S) in LIFO order What if processes are waiting at P(S) in LIFO order

V CSS430 Operating Systems : Process Synchronization Classical problem 1: Bounded-Buffer Problem mutex.P( ) mutex.V( ) empty.P( ) (empty--) empty.V( ) (empty++) full.P( ) (full--) full.V( ) (full++) signal producer consumer

V0.3 Insert and Remove Methods 37 CSS430 Operating Systems : Process Synchronization Lock Unlock 1 Lock Unlock 1

V0.3 Producer and Consumer Threads 38 CSS430 Operating Systems : Process Synchronization buffer

V0.3 Bounded Buffer Problem: Factory 39 CSS430 Operating Systems : Process Synchronization

V0.3 Concurrent Programming 40 CSS430 Operating Systems : Process Synchronization “Programming concurrent applications is a difficult and error-prone undertaking” – Dietel ** When thread synchronization is required use the following guidelines (in order of complexity): ① Use existing classes from the Java API (e.g. import java.util.concurrent.* ) ① Use synchronized keyword and Object methods wait, notify and notifyAll ① Use Lock and Condition interfaces ** (p.678) Java for Programmers, Deitel & Deitel, 2nd Edition, Prentice Hall, 2011

V0.3 Java Thread Model 41 CSS430 Operating Systems : Process Synchronization task completes New runnable timed waitingterminatedwaitingblocked acquire lock, interrupt, I/O completes notify notifyAll wait issue I/O request enter synchronized statement Interval expires Notify notifyAll wait sleep

V0.3Monitors 42 CSS430 Operating Systems : Process Synchronization  High-level language construct  Only one process allowed in a monitor, thus executing its method  A process in the monitor can wait on a condition variable, say x, thus relinquishing the monitor and allowing another process to enter  A process can signal another process waiting on a condition variable (on x).  A process signaling another process should exit from the monitor, because the signal process may have begun to work in the monitor. MethodA MethodB MethodC x.wait( ); x.signal( ) X: Y: p5 p1 p4 p1 p3 p2 p8 p7 p6 Entry queue “…the happens-before relationship. This relationship is simply a guarantee that memory writes by one specific statement are visible to another specific statement. “

V0.3 Java Synchronization 43 CSS430 Operating Systems : Process Synchronization JavaSE Reference on Synchronization (PLEASE READ IN DETAIL!) intrinsic lock = monitor lock

V0.3 Java Monitor 44 CSS430 Operating Systems : Process Synchronization

V0.3 Java Monitor - synchronized method 45 CSS430 Operating Systems : Process Synchronization Every object has an intrinsic lock associated with it. Calling a synchronized method requires ”owning” the lock.

V0.3 Statement vs. Method Sync 46 CSS430 Operating Systems : Process Synchronization Another way to create synchronized code is with synchronized statements. Synchronized statements must specify the object (i.e. "this") that provides the intrinsic lock: To make a method synchronized, simply add the synchronized keyword to its declaration:

V0.3 Insert and Remove with Java Synchronization (i.e. Monitors) 47 CSS430 Operating Systems : Process Synchronization Producer Consumer CS

V0.3 Classical Problem 2: The Readers-Writers Problem 48 CSS430 Operating Systems : Process Synchronization  Multiple readers or a single writer can use DB. writer reader writer reader X X X

V0.3Database 49 CSS430 Operating Systems : Process Synchronization

V0.3Readers 50 CSS430 Operating Systems : Process Synchronization

V0.3Writers 51 CSS430 Operating Systems : Process Synchronization  Why do we have to use notifyAll rather than notify?  Is this algorithm perfect?

V0.3 Classical Problem 3: Dining Philosophers Problem 52 CSS430 Operating Systems : Process Synchronization  Shared data Semaphore chopStick[] = new Semaphore[5]; THINKING HUNGRY EATING

V0.3 The Structure of Philosopher i 53 CSS430 Operating Systems : Process Synchronization A deadlock occurs!

V0.3 Dining-Philosophers Problem Using a Monitor (intrinsic lock) 54 CSS430 Operating Systems : Process Synchronization URL: java-concurrency-part-5-monitors-locks-and-conditions JavaSE : Monitor Monitors (Deprecated)

V CSS430 Operating Systems : Process Synchronization Dining-Philosophers Problem Using a Monitor (intrinsic lock) URL: monitors-locks-and- conditions JavaSE : Monitor Monitors (Deprecated) Java monitor has only one condition. Thus, this abstract code must be modified.

V0.3 Transactional Memory and Concurrency Control 56 CSS430 Operating Systems : Process Synchronization Transactional Memory update ( ) { atomic { /* read and write shared data */ } update ( ) { acquire( ); /* read and write shared data */ release( ); } Compiler-generated code R1 R2 W3 R4 W5 R1 R2 W6 R4 W7 R1 R2 W9 R4 W8 R1 R2 R6 R8 W8 Trans_start Trans_end Trans_abort Trans_restart validation Commitment CPU ACPU B CPU CCPU D Compare reads with former writes Concurrency Control TBD

V0.3 Discussions 2 57 CSS430 Operating Systems : Process Synchronization 1. Is the solution on slides 48–52 perfect for the readers-writers problem? If not, how can you improve it? 2. Rather than a monitor, there is the simplest way that addresses the dining-philosophers problem but introduces another problem. What is that? 3. If we want to handle multiple monitor conditions in Java, what classes should you design?

V0.3Exercises 58 CSS430 Operating Systems : Process Synchronization  Programming Assignment 3: Check the syllabus for its due date. Check the syllabus for its due date.  No-turn-in problems:  Solve Exercises 6.8, 6.12, 6.13, 6.14, 6.19, and 6.24

V0.3 Appendix - Lamport’s Algorithm 59 CSS430 Operating Systems : Process Synchronization Leslie Lamport’s 1974 Original Paper Lamport’s additional remarks Lamport's bakery algorithm (wikipedia)