Lecture 6 Page 1 CS 111 Summer 2013 Concurrency Solutions and Deadlock CS 111 Operating Systems Peter Reiher.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Ch. 7 Process Synchronization (1/2) I Background F Producer - Consumer process :  Compiler, Assembler, Loader, · · · · · · F Bounded buffer.
Chapter 6: Process Synchronization
EEE 435 Principles of Operating Systems Interprocess Communication Pt II (Modern Operating Systems 2.3)
CH7 discussion-review Mahmoud Alhabbash. Q1 What is a Race Condition? How could we prevent that? – Race condition is the situation where several processes.
CS444/CS544 Operating Systems Synchronization 2/21/2006 Prof. Searleman
CS444/CS544 Operating Systems Synchronization 2/16/2006 Prof. Searleman
5.6 Semaphores Semaphores –Software construct that can be used to enforce mutual exclusion –Contains a protected variable Can be accessed only via wait.
Semaphores. Announcements No CS 415 Section this Friday Tom Roeder will hold office hours Homework 2 is due today.
CPS110: Implementing threads/locks on a uni-processor Landon Cox.
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.
Intro to OS CUCS Mossé Processes and Threads What is a process? What is a thread? What types? A program has one or more locus of execution. Each execution.
Synchronization CSCI 444/544 Operating Systems Fall 2008.
02/19/2007CSCI 315 Operating Systems Design1 Process Synchronization Notice: The slides for this lecture have been largely based on those accompanying.
5.4 Which of the following scheduling algorithms could result in starvation? a. First-come, first-served b. Shortest job first c. Round robin d. Priority.
1 Race Conditions/Mutual Exclusion Segment of code of a process where a shared resource is accessed (changing global variables, writing files etc) is called.
CS510 Concurrent Systems Introduction to Concurrency.
Object Oriented Analysis & Design SDL Threads. Contents 2  Processes  Thread Concepts  Creating threads  Critical sections  Synchronizing threads.
SYNCHRONIZATION Module-4. scheduling Scheduling is an operating system mechanism that arbitrate CPU resources between running tasks. Different scheduling.
Lecture 8 Page 1 CS 111 Online Implementing Locks Create a synchronization object – Associated it with a critical section – Of a size that an atomic instruction.
CS 153 Design of Operating Systems Spring 2015 Lecture 11: Scheduling & Deadlock.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Implementing Synchronization. Synchronization 101 Synchronization constrains the set of possible interleavings: Threads “agree” to stay out of each other’s.
Games Development 2 Concurrent Programming CO3301 Week 9.
COMP 111 Threads and concurrency Sept 28, Tufts University Computer Science2 Who is this guy? I am not Prof. Couch Obvious? Sam Guyer New assistant.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Mutual Exclusion.
CSE 451: Operating Systems Section 5 Midterm review.
Kernel Locking Techniques by Robert Love presented by Scott Price.
Lecture 8 Page 1 CS 111 Online Other Important Synchronization Primitives Semaphores Mutexes Monitors.
Chapter 6 – Process Synchronisation (Pgs 225 – 267)
Lecture 6 Page 1 CS 111 Summer 2014 Concurrency Solutions and Deadlock CS 111 Operating Systems Peter Reiher.
Lecture 8 Page 1 CS 111 Fall 2015 Synchronization, Critical Sections and Concurrency CS 111 Operating Systems Peter Reiher.
CS399 New Beginnings Jonathan Walpole. 2 Concurrent Programming & Synchronization Primitives.
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Synchronization Emery Berger and Mark Corner University.
Lecture 12 Page 1 CS 111 Online Using Devices and Their Drivers Practical use issues Achieving good performance in driver use.
Lecture 8 Page 1 CS 111 Winter 2014 Synchronization, Critical Sections and Concurrency CS 111 Operating Systems Peter Reiher.
Implementing Lock. From the Previous Lecture  The “too much milk” example shows that writing concurrent programs directly with load and store instructions.
CS 153 Design of Operating Systems Winter 2016 Lecture 7: Synchronization.
CSCI1600: Embedded and Real Time Software Lecture 17: Concurrent Programming Steven Reiss, Fall 2015.
CS510 Concurrent Systems Jonathan Walpole. Introduction to Concurrency.
Implementing Mutual Exclusion Andy Wang Operating Systems COP 4610 / CGS 5765.
Lecture 4 Page 1 CS 111 Summer 2013 Scheduling CS 111 Operating Systems Peter Reiher.
Mutual Exclusion -- Addendum. Mutual Exclusion in Critical Sections.
© Janice Regan, CMPT 300, May CMPT 300 Introduction to Operating Systems Mutual Exclusion Mutexes, Semaphores.
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.
Lecture 6 Page 1 CS 111 Summer 2015 Concurrency Solutions and Deadlock CS 111 Operating System Principles Peter Reiher.
CSE 120 Principles of Operating
CS703 – Advanced Operating Systems
Outline Other synchronization primitives
Other Important Synchronization Primitives
Multithreading.
Jonathan Walpole Computer Science Portland State University
UNIVERSITY of WISCONSIN-MADISON Computer Sciences Department
Critical section problem
Background and Motivation
Outline Parallelism and synchronization
Implementing Mutual Exclusion
Concurrency: Mutual Exclusion and Process Synchronization
CSCI1600: Embedded and Real Time Software
Implementing Mutual Exclusion
CSE 153 Design of Operating Systems Winter 19
CSE 153 Design of Operating Systems Winter 2019
CS333 Intro to Operating Systems
CSCI1600: Embedded and Real Time Software
CSE 451 Section 1/27/2000.
EECE.4810/EECE.5730 Operating Systems
CSE 542: Operating Systems
CSE 542: Operating Systems
Chapter 3: Process Management
Presentation transcript:

Lecture 6 Page 1 CS 111 Summer 2013 Concurrency Solutions and Deadlock CS 111 Operating Systems Peter Reiher

Lecture 6 Page 2 CS 111 Summer 2013 Outline Concurrency issues – Asynchronous completion Other synchronization primitives Deadlock – Causes – Solution approaches

Lecture 6 Page 3 CS 111 Summer 2013 Asynchronous Completion The second big problem with parallelism – How to wait for an event that may take a while – Without wasteful spins/busy-waits Examples of asynchronous completions – Waiting for a held lock to be released – Waiting for an I/O operation to complete – Waiting for a response to a network request – Delaying execution for a fixed period of time

Lecture 6 Page 4 CS 111 Summer 2013 Using Spin Waits to Solve the Asynchronous Completion Problem Thread A needs something from thread B – Like the result of a computation Thread B isn’t done yet Thread A stays in a busy loop waiting Sooner or later thread B completes Thread A exits the loop and makes use of B’s result Definitely provides correct behavior, but...

Lecture 6 Page 5 CS 111 Summer 2013 Well, Why Not? Waiting serves no purpose for the waiting thread – “Waiting” is not a “useful computation” Spin waits reduce system throughput – Spinning consumes CPU cycles – These cycles can’t be used by other threads – It would be better for waiting thread to “yield” They are actually counter-productive – Delays the thread that will post the completion – Memory traffic slows I/O and other processors

Lecture 6 Page 6 CS 111 Summer 2013 Another Solution Completion blocks Create a synchronization object – Associate that object with a resource or request Requester blocks awaiting event on that object – Yield the CPU until awaited event happens Upon completion, the event is “posted” – Thread that notices/causes event posts the object Posting event to object unblocks the waiter – Requester is dispatched, and processes the event

Lecture 6 Page 7 CS 111 Summer 2013 Blocking and Unblocking Exactly as discussed in scheduling lecture Blocking – Remove specified process from the “ready” queue – Yield the CPU (let scheduler run someone else) Unblocking – Return specified process to the “ready” queue – Inform scheduler of wakeup (possible preemption) Only trick is arranging to be unblocked – Because it is so embarrassing to sleep forever Complexities if multiple entities are blocked on a resource – Who gets unblocked when it’s freed?

Lecture 6 Page 8 CS 111 Summer 2013 A Possible Problem The sleep/wakeup race condition void sleep( eventp *e ) { while(e->posted == FALSE) { add_to_queue( &e->queue, myproc ); myproc->runstate |= BLOCKED; yield(); } void wakeup( eventp *e) { struct proce *p; e->posted = TRUE; p = get_from_queue(&e-> queue); if (p) { p->runstate &= ~BLOCKED; resched(); } /* if !p, nobody’s waiting */ } Consider this sleep code: And this wakeup code: What’s the problem with this?

Lecture 6 Page 9 CS 111 Summer 2013 A Sleep/Wakeup Race Let’s say thread B is using a resource and thread A needs to get it So thread A will call sleep() Meanwhile, thread B finishes using the resource – So thread B will call wakeup() No other threads are waiting for the resource

Lecture 6 Page 10 CS 111 Summer 2013 The Race At Work void sleep( eventp *e ) { while(e->posted == FALSE) { void wakeup( eventp *e) { struct proce *p; e->posted = TRUE; p = get_from_queue(&e-> queue); if (p) { } /* if !p, nobody’s waiting */ } Nope, nobody’s in the queue! add_to_queue( &e->queue, myproc ); myproc->runsate |= BLOCKED; yield(); } Yep, somebody’s locked it! Thread A Thread B The effect? Thread A is sleeping But there’s no one to wake him up CONTEXT SWITCH!

Lecture 6 Page 11 CS 111 Summer 2013 Solving the Problem There is clearly a critical section in sleep() – Starting before we test the posted flag – Ending after we put ourselves on the notify list During this section, we need to prevent – Wakeups of the event – Other people waiting on the event This is a mutual-exclusion problem – Fortunately, we already know how to solve those

Lecture 6 Page 12 CS 111 Summer 2013 Lock Contention The riddle of parallel multi-tasking: – If one task is blocked, CPU runs another – But concurrent use of shared resources is difficult – Critical sections serialize tasks, eliminating parallelism What if everyone needs to share one resource? – One process gets the resource – Other processes get in line behind him – Parallelism is eliminated; B runs after A finishes – That resource becomes a bottle-neck

Lecture 6 Page 13 CS 111 Summer 2013 What If It Isn’t That Bad? Say each thread is only somewhat likely to need a resource Consider the following system – Ten processes, each runs once per second – One resource they all use 5% of time (5ms/sec) – Half of all time slices end with a preemption Chances of preemption while in critical section – Per slice: 2.5%, per sec: 22%, over 10 sec: 92% Chances a 2nd process will need resource – 5% in next time slice, 37% in next second But once this happens, a line forms

Lecture 6 Page 14 CS 111 Summer 2013 Resource Convoys All processes regularly need the resource – But now there is a waiting line – Nobody can “just use the resource”, must get in line The delay becomes much longer – We don’t just wait a few  sec until resource is free – We must wait until everyone in front of us finishes – And while we wait, more people get into the line Delays rise, throughput falls, parallelism ceases Not merely a theoretical transient response

Lecture 6 Page 15 CS 111 Summer 2013 Resource Convoy Performance throughput offered load ideal convoy

Lecture 6 Page 16 CS 111 Summer 2013 Avoiding Contention Problems Eliminate the critical section entirely – Eliminate shared resource, use atomic instructions Eliminate preemption during critical section – By disabling interrupts … not always an option Reduce lingering time in critical section – Minimize amount of code in critical section – Reduce likelihood of blocking in critical section Reduce frequency of critical section entry – Reduce use of the serialized resource – Spread requests out over more resources

Lecture 6 Page 17 CS 111 Summer 2013 Lock Granularity How much should one lock cover? – One object or many – Important performance and usability implications Coarse grained - one lock for many objects – Simpler, and more idiot-proof – Results in greater resource contention Fine grained - one lock per object – Spreading activity over many locks reduces contention – Time/space overhead, more locks, more gets/releases – Error-prone: harder to decide what to lock when – Some operations may require locking multiple objects (which creates a potential for deadlock)

Lecture 6 Page 18 CS 111 Summer 2013 Other Important Synchronization Primitives Semaphores Mutexes Monitors

Lecture 6 Page 19 CS 111 Summer 2013 Semaphores Counters for sequence coord. and mutual exclusion Can be binary counters or more general – E.g., if you have multiple copies of the resource Call wait() on the semaphore to obtain exclusive access to a critical section – For binary semaphores, you wait till whoever had it signals they are done Call signal() when you’re done For sequence coordination, signal on a shared semaphore when you finish first step – Wait before you do second step

Lecture 6 Page 20 CS 111 Summer 2013 Mutexes A synchronization construct to serialize access to a critical section Typically implemented using semaphores Mutexes are one per critical section – Unlike semaphores, which protect multiple copies of a resource

Lecture 6 Page 21 CS 111 Summer 2013 Monitors An object oriented synchronization primitive – Sort of very OO mutexes – Exclusion requirements depend on object/methods – Implementation should be encapsulated in object – Clients shouldn't need to know the exclusion rules A monitor is not merely a lock – It is an object class, with instances, state, and methods – All object methods protected by a semaphore Monitors have some very nice properties – Easy to use for clients, hides unnecessary details – High confidence of adequate protection