Top Banner
EECS 482 Introduction to Operating Systems Winter 2019 Baris Kasikci Slides by: Harsha V. Madhyastha
25

EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when

May 30, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when

EECS 482Introduction to Operating

Systems

Winter 2019

Baris Kasikci

Slides by: Harsha V. Madhyastha

Page 2: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when

Recap● How to leverage hardware support to implement

high-level synchronization primitives?

February 5, 2018

Page 3: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when

Lock implementation #1lock() {

disable interruptswhile (status != FREE) {

enable interruptsdisable interrupts

}status = BUSYenable interrupts

}

February 5, 2018

unlock() {disable interruptsstatus = FREEenable interrupts

}

Page 4: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when

Lock implementation #2// status=0 means lock is freelock() {

while (test_and_set(status) == 1) {}

}

unlock() {status = 0

}

February 5, 2018

Page 5: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when

Lock implementation #3lock() {

disable interruptsif (status == FREE) {

status = BUSY} else {

add thread to queue of threads waiting for lockswitch to next ready thread

}enable interrupts

}unlock() {

disable interruptsstatus = FREEif (any thread is waiting for this lock) {

move waiting thread to ready queuestatus = BUSY

}enable interrupts

}

February 5, 2018

Page 6: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when

Interrupt enable/disable pattern

● Atomically add thread to lock wait queue and switch● Thread leaves interrupts disabled when calling switch

● Who will enable interrupts?● When will a thread return from a switch?

● Switch invariant• All threads promise to disable interrupts when calling switch• All threads assume interrupts are disabled when returning

from switch

February 5, 2018

Page 7: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when
Page 8: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when

Locks on multiprocessors● Disabling interrupts insufficient to ensure

atomicity if there are multiple CPUs

● Need to extend lock implementation #3 to use test_and_set

February 5, 2018

Page 9: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when

Lock implementation #4//guard is initialized to 0lock() {

disable interruptswhile (test_and_set(guard)) {}

if (status == FREE) {status = BUSY

} else {add thread to queue of threads waiting for lockswitch to next ready thread

}guard = 0enable interrupts

}

February 5, 2018

Page 10: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when

Lock implementation #4unlock() {

disable interruptswhile (test_and_set(guard)) {}

status = FREEif (any thread is waiting for this lock) {

move waiting thread to ready queuestatus = BUSY

}

guard = 0enable interrupts

}

What’s the switch invariant for multiprocessors?

February 5, 2018

Page 11: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when

Summary of lock implementations

● High-level takeaways:• Disable/enable interrupts and test_and_set(guard) to

protect critical section inside synchronization code• Atomically add thread to a waiting list and sleep

● How did we achieve this?• Switch to another thread and hand off task of

enabling interrupts and resetting guard

● What if no other thread to run?• Atomically suspend CPU with interrupts enabled

February 5, 2018

Page 12: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when

Project 2● Covered everything you need to know to

implement all of the project

● Use assertions liberally• Assert any property that you expect to be true• Enables catching errors closer to where they occur

● Example:• Any thread is executing on at most one CPU

February 5, 2018

Page 13: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when

Deadlock● Synchronization is about constraining executions

● Deadlock is what happens when an execution is over-constrained

• A must happen before B, B must happen before A

● Example: Swapping classes• Alice is in 482, Bob is in 485, and they want to switch

» wait for spot to open» add new class» drop old class

February 5, 2018

Page 14: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when

Deadlock● Resources

• Things needed by a thread that it waits for• Examples: locks, disk space, memory, CPU

● Deadlock• Cyclical waiting for resources which prevents progress• Results in starvation: threads wait forever

February 5, 2018

Page 15: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when
Page 16: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when

Deadlock example

● Will a deadlock always occur?

February 5, 2018

Thread Ax.locky.lock...y.unlockx.unlock

Thread By.lockx.lock...x.unlocky.unlock

Page 17: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when

Dining philosophers

● 5 philosophers sit at round table

● 1 chopstick between each pair of philosophers

● Each philosopher needs 2 chopsticks to eat

February 5, 2018

A

B

CD

E

Page 18: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when

Dining philosophers● Algorithm for philosopher:

wait for chopstick on right to be freepick up chopstick on rightwait for chopstick on left to be freepick up chopstick on leftput both chopsticks down

● Can this deadlock?

February 5, 2018

A

B

CD

E

Page 19: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when

Generic example of multi-threaded program

phase 1:while (!done) {

acquire some resourcework

}

phase 2:release all resources

February 5, 2018

Page 20: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when

Waits-for graph

● Cycle represents a deadlock

February 5, 2018

thread A

thread B

resource 2resource 1

Page 21: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when

Strategies for handling deadlock

● Ignore

● Detect and fix• Detect cycles in the wait-for graph• How to fix once detected?

» Kill thread, grab resources» Roll back execution

● Prevent

February 5, 2018

Page 22: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when

Four necessary conditions for deadlock● Limited resources

• Not enough to serve all threads simultaneously

● No preemption• Can’t force threads to give up resources

● Hold and wait• Threads hold resources while waiting to acquire other

resources

● Cyclical chain of requestsFebruary 5, 2018

Page 23: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when

Eliminating hold-and-wait● Two ways to avoid hold and wait:

• Wait for all resources needed to be free; grab them all atomically

• If cannot get a resource, release all and start over

● Move resource acquisition to beginningPhase 1a: acquire all resourcesPhase 1b: while (!done) {

work}

Phase 2: release all resources

February 5, 2018

Page 24: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when

Eliminating circular chain● Impose global ordering of resources

February 5, 2018

A

B

CD

E

2

5

4

3

1

Page 25: EECS 482 Introduction to Operating Systemsweb.eecs.umich.edu/.../lec09...deadlock-pre.pdf · Deadlock Synchronization is about constrainingexecutions Deadlock is what happens when

Preventing deadlock● What if we don’t grant resources that will lead

to cycle in waits-for-graph?

February 5, 2018

thread A

thread B

xy

Thread Ax.locky.lock...y.unlockx.unlock

Thread By.lockx.lock...x.unlocky.unlock