Multi-Object Synchronization. Main Points Problems with synchronizing multiple objects Definition of deadlock – Circular waiting for resources Conditions.

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks.
Advertisements

Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community.
1 Concurrency: Deadlock and Starvation Chapter 6.
Deadlock and Starvation
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Operating Systems Lecture Notes Deadlocks Matthew Dailey Some material © Silberschatz, Galvin, and Gagne, 2002.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
1 Deadlock Solutions: Avoidance, Detection, and Recovery CS 241 March 30, 2012 University of Illinois.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Chapter 6 Concurrency: Deadlock and Starvation
Ceng Operating Systems Chapter 2.4 : Deadlocks Process concept  Process scheduling  Interprocess communication  Deadlocks Threads.
Deadlocks Andy Wang Operating Systems COP 4610 / CGS 5765.
Chapter 3 Deadlocks TOPICS Resource Deadlocks The ostrich algorithm
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
Tanenbaum Ch 6 Silberschatz Ch 7
Avishai Wool lecture Introduction to Systems Programming Lecture 5 Deadlocks.
Deadlock Chapter 3 R1 R2 P2P1 Allocated Requested.
CS 162 Week 3 Discussion (2/13 – 2/14). Today’s Section Project Administrivia (5 min) Quiz (5 min) Review Lectures 6 and 7 (15 min) Worksheet and Discussion.
Deadlocks Vivek Pai / Kai Li Princeton University.
Chapter 7: Deadlocks. 7.2 Chapter Objectives To develop a description of deadlocks, which prevent sets of concurrent processes from completing their tasks.
Deadlock CSCI 444/544 Operating Systems Fall 2008.
OS Spring 2004 Concurrency: Principles of Deadlock Operating Systems Spring 2004.
CPSC 4650 Operating Systems Chapter 6 Deadlock and Starvation
OS Fall’02 Concurrency: Principles of Deadlock Operating Systems Fall 2002.
1 Concurrency: Deadlock and Starvation Chapter 6.
CSE 451: Operating Systems Winter 2011 Deadlock Mark Zbikowski Gary Kimura.
CPS110: Deadlock Landon Cox February 5, Concurrency so far  Mostly tried to constrain orderings  Locks, monitors, semaphores  It is possible.
Concurrency: Deadlock and Starvation Chapter 6. Goal and approach Deadlock and starvation Underlying principles Solutions? –Prevention –Detection –Avoidance.
1 Concurrency: Deadlock and Starvation Chapter 6.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Dave Bremer Otago Polytechnic,
Deadlock Chapter 3 Thursday, February 22, Today’s Schedule Assignment #4 from Chapter 3 posted Deadlock - Chapter 3  Skip multiple resources (3.4.2.
CS 153 Design of Operating Systems Spring 2015 Lecture 11: Scheduling & Deadlock.
1 Announcements The fixing the bug part of Lab 4’s assignment 2 is now considered extra credit. Comments for the code should be on the parts you wrote.
1 Deadlocks 2 Resources Examples of computer resources –printers –tape drives –tables Processes need access to resources in reasonable order Suppose.
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
1 Deadlocks Chapter 3. 2 Resources Examples of computer resources –printers –tape drives –tables Processes need access to resources in reasonable order.
Operating Systems 软件学院 高海昌 Operating Systems Gao Haichang, Software School, Xidian University 22 Contents  1. Introduction** 
1 Deadlocks Chapter Resource 3.2. Introduction to deadlocks 3.3. The ostrich algorithm 3.4. Deadlock detection and recovery 3.5. Deadlock avoidance.
Operating Systems (OS)
1 MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc.
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 6 Deadlocks Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc. All.
CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.
Operating Systems Lecture 4 Deadlock Adapted from Operating Systems Lecture Notes, Copyright 1997 Martin C. Rinard. Zhiqing Liu School of Software Engineering.
CS333 Intro to Operating Systems Jonathan Walpole.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings.
Operating Systems COMP 4850/CISG 5550 Deadlocks Dr. James Money.
Deadlocks Mark Stanovich Operating Systems COP 4610.
Deadlocks.  Deadlocks: Occurs when threads are waiting for resources with circular dependencies Often involve nonpreemptable resources, which cannot.
Multi-Object Synchronization. Multi-Object Programs What happens when we try to synchronize across multiple objects in a large program? – Each object.
Copyright ©: Nahrstedt, Angrave, Abdelzaher1 Deadlock.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community.
Process Management Deadlocks.
Introduction to Operating Systems
Concurrency: Deadlock and Starvation
CS703 - Advanced Operating Systems
Introduction to Operating Systems
CS 333 Introduction to Operating Systems Class 7 - Deadlock
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Andy Wang Operating Systems COP 4610 / CGS 5765
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
SE350: Operating Systems Lecture 5: Deadlock.
CSE 153 Design of Operating Systems Winter 2019
Introduction to Deadlocks
Chapter 3 Deadlocks 3.1. Resource 3.2. Introduction to deadlocks
CENG334 Introduction to Operating Systems
EECE.4810/EECE.5730 Operating Systems
CSE 542: Operating Systems
CSE 542: Operating Systems
Presentation transcript:

Multi-Object Synchronization

Main Points Problems with synchronizing multiple objects Definition of deadlock – Circular waiting for resources Conditions for its occurrence Solutions for avoiding and breaking deadlock

Large Programs What happens when we try to synchronize across multiple objects in a large program? – Each object with its own lock, condition variables – Is concurrency modular? Deadlock Performance Semantics/correctness

Deadlock Definition Resource: any (passive) thing needed by a thread to do its job (CPU, disk space, memory, lock) – Preemptable: can be taken away by OS – Non-preemptable: must leave with thread Starvation: thread waits indefinitely Deadlock: circular waiting for resources – Deadlock => starvation, but not vice versa

Example: two locks Thread A lock1.acquire(); lock2.acquire(); lock2.release(); lock1.release(); Thread B lock2.acquire(); lock1.acquire(); lock1.release(); lock2.release();

Bidirectional Bounded Buffer Thread A buffer1.put(data); buffer2.get(); Thread B buffer2.put(data); Buffer1.get();

Two locks and a condition variable Thread A lock1.acquire(); … lock2.acquire(); while (need to wait) condition.wait(lock2); lock2.release(); … lock1.release(); Thread B lock1.acquire(); … lock2.acquire(); …. condition.signal(lock2); lock2.release(); … lock1.release();

Yet another Example

Dining Lawyers Each lawyer needs two chopsticks to eat. Each grabs chopstick on the right first.

Conditions for Deadlock Limited access to resources – If infinite resources, no deadlock! No preemption – If resources are virtual, can break deadlock Multiple independent requests – “wait while holding” Circular chain of requests

Circular Waiting

Solution #1: Detect and Fix Algorithm – Scan wait for graph – Detect cycles – Fix cycles How? – Remove one thread, reassign its resources Requires exception handling code to be very robust – Roll back actions of one thread Databases: all actions are provisional until committed

Solution #2: Deadlock Prevention Eliminate one of the four conditions for deadlock Lock ordering – Always acquire locks in the same order – Example: move file from one directory to another – Widely used in OS kernels Design system to release resources and retry if need to wait – No “wait while holding” – Example: telephone circuit setup Infinite resources? – Ex: UNIX reserves a process for the sysadmin to run “kill” Acquire all needed resources in advance

Solution #3: Banker’s Algorithm Banker’s algorithm – State maximum resource needs in advance – Allocate resources dynamically when resource is needed -- wait if granting request would lead to deadlock – Request can be granted if some sequential ordering of threads is deadlock free

Possible System States

Definitions Safe state: – For any possible sequence of future resource requests, it is possible to eventually grant all requests – May require waiting even when resources are available! Unsafe state: – Some sequence of resource requests can result in deadlock Doomed state: – All possible computations lead to deadlock

Banker’s Algorithm Grant request iff result is a safe state Sum of maximum resource needs of current threads can be greater than the total resources – Provided there is some way for all the threads to finish without getting into deadlock Example: proceed iff – total available resources - # allocated >= max remaining that might be needed by this thread in order to finish – Guarantees this thread can finish

Example: Banker’s Algorithm n chopsticks in middle of table n lawyers, each can take one chopstick at a time When is it ok for lawyer to take a chopstick? What if each lawyer needs k chopsticks?

Lock-Free Data Structures Assume compare and swap atomic instruction – Limitation: swap a single memory location – Only supported on some processor architectures Rewrite critical section – Create copy of data structure – Modify copy – Swap in pointer to copy iff no one else has – Restart if pointer has changed

Lock-Free Bounded Buffer get() { do { mine = ConsistentCopy(p); if (mine.front == mine.last) mine.queue.Add(self); else { item = mine.buf[ mine.front % size]; mine.front++; } while ((compare&swap(mine, p) != p); wake up waiter if needed return item. }