synchronization

41
Synchronization

Upload: milly

Post on 25-Feb-2016

23 views

Category:

Documents


1 download

DESCRIPTION

Synchronization. Motivating Example: Too Much Milk. Two robots are programmed to maintain the milk inventory at a store… They are not aware of each other’s presence…. Robot: Dumber. Robot: Dumb. Dumb 4:00Look into fridge: Out of milk. Dumber. Motivating Example: Too Much Milk. - PowerPoint PPT Presentation

TRANSCRIPT

Page 2: Synchronization

Motivating Example: Too Much Milk

Two robots are programmed to maintain the milk inventory at a store…

They are not aware of each other’s presence…

Robot: Dumb Robot: Dumber

Page 3: Synchronization

Motivating Example: Too Much Milk

Dumb4:00 Look into fridge:

Out of milk

Dumber

Page 4: Synchronization

Motivating Example: Too Much Milk

Dumb4:00 Look into fridge:

Out of milk4:05 Head for the

warehouse

Dumber

Page 5: Synchronization

Motivating Example: Too Much Milk

Dumb4:05 Head for the

warehouse

Dumber

4:10 Look into fridge: Out of milk

Page 6: Synchronization

Motivating Example: Too Much Milk

Dumb Dumber4:10 Look into fridge:

Out of milk4:15 Head for the

warehouse

Page 7: Synchronization

Motivating Example: Too Much Milk

Dumb

4:20 Arrive with milk

Dumber4:15 Head for the

warehouse

Page 8: Synchronization

Motivating Example: Too Much Milk

Dumb

4:20 Arrive with milk

Dumber4:15 Head for the

warehouse

Page 9: Synchronization

Motivating Example: Too Much Milk

Dumb4:20 Arrive with milk4:25 Go party

Dumber

Page 10: Synchronization

Motivating Example: Too Much Milk

Dumb4:20 Arrive with milk4:25 Go party

Dumber

4:30 Arrive with milk: “Uh oh…”

Page 11: Synchronization

Definitions

Synchronization: use atomic operations to ensure cooperation among threads

Mutual exclusion: ensure one thread can do something without the interference of other threads

Critical section: a piece of code that only one thread can execute at a time

Page 12: Synchronization

More on Critical Section

A lock prevents a thread from doing something A thread should lock before entering a critical

section A thread should unlock when leaving the

critical section A thread should wait if the critical section is

locked Synchronization often involves waiting

Page 13: Synchronization

Too Much Milk: Solution 1

Two properties: Someone should go get the milk if needed Only one robot will go get milk

Basic idea of solution 1 Leave a note (kind of like a lock) Remove the note (kind of like a unlock) Don’t go get milk if the note is around (wait)

Page 14: Synchronization

Too Much Milk: Solution 1

if (no milk) {if (no note) {

// leave a note;// go get milk;// remove the note;

}}

Page 15: Synchronization

Too Much Milk: Solution 1

Dumb4:00 if (no milk) {

Dumber

Page 16: Synchronization

Too Much Milk: Solution 1

Dumb4:00 if (no milk) {

Dumber

4:01 if (no milk) {

Page 17: Synchronization

Too Much Milk: Solution 1

Dumb4:00 if (no milk) {

Dumber

4:01 if (no milk) {4:02 if (no note) {

Page 18: Synchronization

Too Much Milk: Solution 1

Dumb4:00 if (no milk) {

4:03 if (no note) {

Dumber

4:01 if (no milk) {4:02 if (no note) {

Page 19: Synchronization

Too Much Milk: Solution 1

Dumb4:00 if (no milk) {

4:03 if (no note) {4:04 // leave a note

Dumber

4:01 if (no milk) {4:02 if (no note) {

Page 20: Synchronization

Too Much Milk: Solution 1

Dumb

4:03 if (no note) {4:04 // leave a note

Dumber4:01 if (no milk) {4:02 if (no note) {

4:05 // leave a note

Page 21: Synchronization

Too Much Milk: Solution 1

Dumb

4:03 if (no note) {4:04 // leave a note

4:06 // go get milk

Dumber4:02 if (no note) {

4:05 // leave a note

Page 22: Synchronization

Too Much Milk: Solution 1

Dumb4:03 if (no note) {4:04 // leave a note

4:06 // go get milk

Dumber

4:05 // leave a note

4:07 // go get milk

Page 23: Synchronization

Too Much Milk: Solution 2

Okay…solution 1 does not work The notes are posted too late… What if both robots begin by leaving their own

notes?

Page 24: Synchronization

Too Much Milk: Solution 2

// leave a note; if (no note from the other) {if (no milk) {

// go get milk;}

} // remove the note;

Page 25: Synchronization

Too Much Milk: Solution 2

Dumb4:00 // leave a note

Dumber

Page 26: Synchronization

Too Much Milk: Solution 2

Dumb4:00 // leave a note

Dumber

4:01 // leave a note

Page 27: Synchronization

Too Much Milk: Solution 2

Dumb4:00 // leave a note

4:02 if (no note from Dumber) {…}

Dumber

4:01 // leave a note

Page 28: Synchronization

Too Much Milk: Solution 2

Dumb4:00 // leave a note

4:02 if (no note from Dumber) {…}

Dumber

4:01 // leave a note

4:03 if (no note from Dumb) {…}

Page 29: Synchronization

Too Much Milk: Solution 2

Dumb4:00 // leave a note

4:02 if (no note from Dumber) {…}

4:04 // remove the note

Dumber

4:01 // leave a note

4:03 if (no note from Dumb) {…}

Page 30: Synchronization

Too Much Milk: Solution 2

Dumb4:00 // leave a note

4:02 if (no note from Dumber) {…}

4:04 // remove the note

Dumber

4:01 // leave a note

4:03 if (no note from Dumb) {…}

Page 31: Synchronization

Too Much Milk: Solution 2

Dumb

4:02 if (no note from Dumber) {…}

4:04 // remove the note

Dumber4:01 // leave a note

4:03 if (no note from Dumb) {…}

4:05 // remove the note

Page 32: Synchronization

Too Much Milk: Solution 2

Dumb

4:02 if (no note from Dumber) {…}

4:04 // remove the note

Dumber4:01 // leave a note

4:03 if (no note from Dumb) {…}

4:05 // remove the note

Page 33: Synchronization

Too Much Milk: Solution 3

Dumb// leave Dumb’s notewhile (Dumber’s note) { };if (no milk) {

// go get milk}// remove Dumb’s note

Dumber// leave Dumber’s noteif (no Dumb’s note) { if (no milk) { // go get milk }}// remove Dumber’s note

Page 34: Synchronization

Too Much Milk Solution 3

How do we verify the correctness of a solution?

Test arbitrary interleaving of locking and checking locks In this case, leaving notes and checking notes

Page 35: Synchronization

Dumber Challenges Dumb: Case 1

Dumb// leave Dumb’s notewhile (Dumber’s note) { };

if (no milk) {// go get milk

}

// remove Dumb’s note

Dumber

// leave Dumber’s note

if (no Dumb’s note) {}// remove Dumber’s note

Tim

e

Page 36: Synchronization

Dumber Challenges Dumb: Case 2

Dumb// leave Dumb’s note

while (Dumber’s note) { };

if (no milk) {// go get milk

}// remove Dumb’s note

Dumber

// leave Dumber’s note

if (no Dumb’s note) {}// remove Dumber’s note

Tim

e

Page 37: Synchronization

Dumber Challenges Dumb: Case 3

Dumb// leave Dumb’s note

while (Dumber’s note) { };

if (no milk) {}// remove Dumb’s note

Dumber

// leave Dumber’s noteif (no Dumb’s note) {}// remove Dumber’s note

Tim

e

Page 38: Synchronization

Dumb Challenges Dumber: Case 1

Dumb

// leave Dumb’s notewhile (Dumber’s note) { };

if (no milk) {}// remove Dumb’s note

Dumber// leave Dumber’s noteif (no Dumb’s note) {

if (no milk) { // go get milk }}// remove Dumber’s note

Tim

e

Page 39: Synchronization

Dumb Challenges Dumber: Case 2

Dumb

// leave Dumb’s note

while (Dumber’s note) { };

if (no milk) { // go get milk}// remove Dumb’s note

Dumber// leave Dumber’s note

if (no Dumb’s note) {}// remove Dumber’s note

Tim

e

Page 40: Synchronization

Dumb Challenges Dumber: Case 3

Dumb

// leave Dumb’s notewhile (Dumber’s note) { };

if (no milk) { // go get milk}// remove Dumb’s note

Dumber// leave Dumber’s note

if (no Dumb’s note) {}// remove Dumber’s note

Tim

e

Page 41: Synchronization

Lessons Learned Although it works, Solution 3 is ugly

Difficult to verify correctness Two threads have different code

Difficult to generalize to N threads While Dumb is waiting, it consumes CPU time

(busy waiting) More elegant with higher-level primitives

lockacquire();if (no milk) { // go get milk }lockrelease();