3/17/2016cse410-24-synchronization-p2 © 2006-07 Perkins, DW Johnson and University of Washington1 Synchronization Part 2 CSE 410, Spring 2008 Computer.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Tutorial 3 Sync or sink! presented by: Antonio Maiorano Paul Di Marco.
– R 7 :: 1 – 0024 Spring 2010 Parallel Programming 0024 Recitation Week 7 Spring Semester 2010.
Ch 7 B.
Chapter 6: 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.
1 Condition Synchronization. 2 Synchronization Now that you have seen locks, is that all there is? No, but what is the “right” way to build a parallel.
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
1 CSE451 – Section 4. 2 Reminders Project 2 parts 1,2,3 due next Thursday Threads, synchronization Today: Project 2 continued (parts 2,3) Synchronization.
1 CS318 Project #3 Preemptive Kernel. 2 Continuing from Project 2 Project 2 involved: Context Switch Stack Manipulation Saving State Moving between threads,
CS444/CS544 Operating Systems Synchronization 2/21/2007 Prof. Searleman
Monitors CSCI 444/544 Operating Systems Fall 2008.
02/23/2004CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
02/17/2010CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
K. Stirewalt CSE 335: Software Design Software Architecture and Larger System Design Issues Lecture 3: Synchronization Topics: –Concurrent access to shared.
Race Conditions CS550 Operating Systems. Review So far, we have discussed Processes and Threads and talked about multithreading and MPI processes by example.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
Monitors: An Operating System Structuring Concept
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 19 th, 2010 The University of Georgia.
Concurrency Recitation – 2/24 Nisarg Raval Slides by Prof. Landon Cox.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
9/8/2015cse synchronization-p1 © Perkins DW Johnson and University of Washington1 Synchronization Part 1 CSE 410, Spring 2008 Computer Systems.
U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts, Amherst Operating Systems CMPSCI 377 Lecture.
Atomic Operations David Monismith cs550 Operating Systems.
Implementing Synchronization. Synchronization 101 Synchronization constrains the set of possible interleavings: Threads “agree” to stay out of each other’s.
Concurrent Programming. Concurrency  Concurrency means for a program to have multiple paths of execution running at (almost) the same time. Examples:
Producer-Consumer Problem The problem describes two processes, the producer and the consumer, who share a common, fixed-size buffer used as a queue.bufferqueue.
CSC321 Concurrent Programming: §5 Monitors 1 Section 5 Monitors.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 7: Process Synchronization Background The Critical-Section Problem Synchronization.
Java Thread and Memory Model
Dr. R R DOCSIT, Dr BAMU. Basic Java :Multi Threading Cont. 2 Objectives of This Session Explain Synchronization in threads Demonstrate use of.
1 CMSC421: Principles of Operating Systems Nilanjan Banerjee Principles of Operating Systems Acknowledgments: Some of the slides are adapted from Prof.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Software Systems Advanced Synchronization Emery Berger and Mark Corner University.
Chapter 6: Process Synchronization. 6.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Module 6: Process Synchronization Background The.
Monitors and Blocking Synchronization Dalia Cohn Alperovich Based on “The Art of Multiprocessor Programming” by Herlihy & Shavit, chapter 8.
6.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts with Java – 8 th Edition Module 6: Process Synchronization Codes.
Problems with Semaphores Used for 2 independent purposes –Mutual exclusion –Condition synchronization Hard to get right –Small mistake easily leads to.
CS533 – Spring Jeanie M. Schwenk Experiences and Processes and Monitors with Mesa What is Mesa? “Mesa is a strongly typed, block structured programming.
COSC 3407: Operating Systems Lecture 9: Readers-Writers and Language Support for Synchronization.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 6: Process Synchronization.
Windows CE Overview and Scheduling Presented by Dai Kawano.
1 Previous Lecture Overview  semaphores provide the first high-level synchronization abstraction that is possible to implement efficiently in OS. This.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
June 11, 2002Serguei A. Mokhov, 1 The Monitor COMP346 - Operating Systems Tutorial 7 Edition 1.2, June 15, 2002.
© 2004, D. J. Foreman 1 Monitors and Inter-Process Communication.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 5: Process Synchronization.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
CS162 Section 2. True/False A thread needs to own a semaphore, meaning the thread has called semaphore.P(), before it can call semaphore.V() False: Any.
CS703 - Advanced Operating Systems
CSE 120 Principles of Operating
CS703 – Advanced Operating Systems
Background on the need for Synchronization
Monitors, Condition Variables, and Readers-Writers
CSCI 511 Operating Systems Chapter 5 (Part C) Monitor
Sarah Diesburg Operating Systems COP 4610
Implementing Mutual Exclusion
Implementing Mutual Exclusion
CSE 451: Operating Systems Autumn 2003 Lecture 7 Synchronization
CSE 451: Operating Systems Autumn 2005 Lecture 7 Synchronization
CSE 451: Operating Systems Winter 2003 Lecture 7 Synchronization
“The Little Book on Semaphores” Allen B. Downey
Monitors and Inter-Process Communication
EECE.4810/EECE.5730 Operating Systems
Don Porter Portions courtesy Emmett Witchel
Review The Critical Section problem Peterson’s Algorithm
Presentation transcript:

3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington1 Synchronization Part 2 CSE 410, Spring 2008 Computer Systems

3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington2 Readings and References Reading »Chapter 7, Sections 7.4 through 7.7, Operating System Concepts, Silberschatz, Galvin, and Gagne Other References »The Java Tutorial, Synchronizing Threads » »

3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington3 Shared Stack Data Structure void Stack::Push(Item *item) { item->next = top; top = item; } Suppose two threads, red and blue, share this code and a Stack s The two threads both operate on s »each calls s->Push(…) Execution is interleaved by context switches

3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington4 Now suppose that a context switch occurs at an “inconvenient” time, so that the actual execution order is 1 item->next = top; 2 item->next = top; 3 top = item; 4 top = item; Stack Example context switch from red to blue context switch from blue to red

Disaster Strikes top time 0 top time 1 top time 2 top time 3 top time 4 item->next = top; top = item;

3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington6 Shared Stack Solution How do we fix this using locks? »See also how to make a lock void Stack::Push(Item *item) { lock->Acquire(); item->next = top; top = item; lock->Release(); }

3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington7 Correct Execution Only one thread can hold the lock lock->Acquire(); item->next = top; top = item; lock->Release(); lock->Acquire(); wait for lock acquisition item->next = top; top = item; lock->Release();

Correct Execution top Red acquires the lock Blue tries to acquire the lock top Red releases the lock Blue acquires the lock top Blue releases the lock

3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington9 How can Pop wait for a Stack item? This works okay if we don't want to wait inside Pop and can just return But in order to wait we need to go to sleep inside the critical section »other threads won't be able to run because Pop holds the lock! »condition variables make it possible to go to sleep inside a critical section, by releasing the lock and going to sleep in one atomic operation Stack::Push(Item * item) { lock->Acquire(); push item on stack lock->Release(); } Item * Stack::Pop() { lock->Acquire(); pop item from stack lock->Release(); return item; } Synchronized stack using locks

3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington10 Monitors Monitor: a lock and condition variables Key addition is the ability to inexpensively and reliably wait for a condition change Can be implemented as a separate class »The class contains code and private data »Since the data is private, only monitor code can access it »Only one thread is allowed to run in the monitor at a time Can be implement directly in other classes using locks and condition variables

3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington11 Condition Variables A condition variable is a queue of threads waiting for something inside a critical section There are three operations »Wait()--release lock & go to sleep (atomic); reacquire lock upon awakening »Signal()--wake up one waiting thread, if any »Broadcast()--wake up all waiting threads A thread must hold the lock when doing condition variable operations

3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington12 Stack with Condition Variables Pop can now wait for something to be pushed onto the stack Stack::Push(Item *item) { lock->Acquire(); push item on stack condition->signal( lock ); lock->Release(); } Item *Stack::Pop() { lock->Acquire(); while( nothing on stack ) { condition->wait( lock ); } pop item from stack lock->Release(); return item; }

3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington13 Synchronization in Win2K/XP Windows has locks (known as mutexes) »CreateMutex --returns a handle to a new mutex »WaitForSingleObject --acquires the mutex »ReleaseMutex --releases the mutex »Similar for semaphores, too. »Don’t use PulseEvents Windows has condition variables (known as events) »CreateEvent --returns a handle to a new event »WaitForSingleObject --waits for the event to happen »SetEvent --signals the event, waking up one waiting thread

3/17/2016cse synchronization-p2 © Perkins, DW Johnson and University of Washington14 Synchronization in Java Java has locks (on any object) »The Java platform associates a lock with every object that has synchronized code »A method or a code block {...} can be synchronized »The lock is acquired before the block is entered and released when the block is exited Java has condition variables (wait lists) »The Object class defines wait(), notify(), notifyAll() methods »By inheritance, all objects of all classes have those methods