Synchronization Andy Wang Operating Systems COP 4610 CGS

  • Slides: 41
Download presentation
Synchronization Andy Wang Operating Systems COP 4610 / CGS 5765

Synchronization Andy Wang Operating Systems COP 4610 / CGS 5765

Motivating Example: Too Much Milk n Two robots are programmed to maintain the milk

Motivating Example: Too Much Milk n Two robots are programmed to maintain the milk inventory at a store… n They are not aware of each other’s presence… Robot: Dumber

Motivating Example: Too Much Milk Dumb 4: 00 Look into fridge: Out of milk

Motivating Example: Too Much Milk Dumb 4: 00 Look into fridge: Out of milk Dumber

Motivating Example: Too Much Milk Dumb 4: 00 Look into fridge: Out of milk

Motivating Example: Too Much Milk Dumb 4: 00 Look into fridge: Out of milk 4: 05 Head for the warehouse Dumber

Motivating Example: Too Much Milk Dumb 4: 05 Head for the warehouse Dumber 4:

Motivating Example: Too Much Milk Dumb 4: 05 Head for the warehouse Dumber 4: 10 Look into fridge: Out of milk

Motivating Example: Too Much Milk Dumber 4: 10 Look into fridge: Out of milk

Motivating Example: Too Much Milk Dumber 4: 10 Look into fridge: Out of milk 4: 15 Head for the warehouse

Motivating Example: Too Much Milk Dumb 4: 20 Dumber 4: 15 Head for the

Motivating Example: Too Much Milk Dumb 4: 20 Dumber 4: 15 Head for the warehouse Arrive with milk

Motivating Example: Too Much Milk Dumb 4: 20 Dumber 4: 15 Head for the

Motivating Example: Too Much Milk Dumb 4: 20 Dumber 4: 15 Head for the warehouse Arrive with milk

Motivating Example: Too Much Milk Dumb 4: 20 Arrive with milk 4: 25 Go

Motivating Example: Too Much Milk Dumb 4: 20 Arrive with milk 4: 25 Go party Dumber

Motivating Example: Too Much Milk Dumb 4: 20 Arrive with milk 4: 25 Go

Motivating Example: Too Much Milk Dumb 4: 20 Arrive with milk 4: 25 Go party Dumber 4: 30 Arrive with milk: “Uh oh…”

Definitions n Synchronization: uses atomic operations to ensure cooperation among threads n Mutual exclusion:

Definitions n Synchronization: uses atomic operations to ensure cooperation among threads n Mutual exclusion: ensures one thread can do something without the interference of other threads n Critical section: a piece of code that only one thread can execute at a time

More on Critical Section n A lock prevents a thread from doing something A

More on Critical Section n A lock prevents a thread from doing something A thread should lock before entering a critical section n A thread should unlock when leaving the critical section n A thread should wait if the critical section is locked n n Synchronization often involves waiting

Too Much Milk: Solution 1 n Two properties: n Only one robot will go

Too Much Milk: Solution 1 n Two properties: n Only one robot will go get milk n Someone should go get the milk if needed n Basic idea of solution 1 n Leave a note (kind of like a lock) n Remove the note (kind of like a unlock) n Don’t go get milk if the note is around (wait)

Too Much Milk: Solution 1 if (no milk) { if (no note) { //

Too Much Milk: Solution 1 if (no milk) { if (no note) { // leave a note; // go get milk; // remove the note; } }

Too Much Milk: Solution 1 Dumb 4: 00 if (no milk) { Dumber

Too Much Milk: Solution 1 Dumb 4: 00 if (no milk) { Dumber

Too Much Milk: Solution 1 Dumber 4: 00 if (no milk) { 4: 01

Too Much Milk: Solution 1 Dumber 4: 00 if (no milk) { 4: 01 if (no milk) {

Too Much Milk: Solution 1 Dumber 4: 00 if (no milk) { 4: 01

Too Much Milk: Solution 1 Dumber 4: 00 if (no milk) { 4: 01 if (no milk) { 4: 02 if (no note) {

Too Much Milk: Solution 1 Dumber 4: 00 if (no milk) { 4: 01

Too Much Milk: Solution 1 Dumber 4: 00 if (no milk) { 4: 01 if (no milk) { 4: 02 if (no note) { 4: 03 if (no note) {

Too Much Milk: Solution 1 Dumber 4: 00 if (no milk) { 4: 01

Too Much Milk: Solution 1 Dumber 4: 00 if (no milk) { 4: 01 if (no milk) { 4: 02 if (no note) { 4: 03 4: 04 if (no note) { // leave a note

Too Much Milk: Solution 1 Dumber 4: 01 if (no milk) { 4: 02

Too Much Milk: Solution 1 Dumber 4: 01 if (no milk) { 4: 02 if (no note) { 4: 03 4: 04 if (no note) { // leave a note 4: 05 // leave a note

Too Much Milk: Solution 1 Dumber 4: 02 4: 03 4: 04 if (no

Too Much Milk: Solution 1 Dumber 4: 02 4: 03 4: 04 if (no note) { // leave a note 4: 05 4: 06 if (no note) { // go get milk // leave a note

Too Much Milk: Solution 1 Dumb 4: 03 4: 04 4: 06 Dumber if

Too Much Milk: Solution 1 Dumb 4: 03 4: 04 4: 06 Dumber if (no note) { // leave a note 4: 05 // leave a note 4: 07 // go get milk

Too Much Milk: Solution 2 n Okay…solution 1 does not work n The notes

Too Much Milk: Solution 2 n Okay…solution 1 does not work n The notes are posted too late… n What if both robots begin by leaving their own notes?

Too Much Milk: Solution 2 // leave a note; if (no note from the

Too Much Milk: Solution 2 // leave a note; if (no note from the other) { if (no milk) { // go get milk; } } // remove the note;

Too Much Milk: Solution 2 Dumb 4: 00 // leave a note Dumber

Too Much Milk: Solution 2 Dumb 4: 00 // leave a note Dumber

Too Much Milk: Solution 2 Dumber 4: 00 // leave a note 4: 01

Too Much Milk: Solution 2 Dumber 4: 00 // leave a note 4: 01 // leave a note

Too Much Milk: Solution 2 Dumber 4: 00 // leave a note 4: 01

Too Much Milk: Solution 2 Dumber 4: 00 // leave a note 4: 01 // leave a note 4: 02 if (no note from Dumber) {…}

Too Much Milk: Solution 2 Dumber 4: 00 // leave a note 4: 01

Too Much Milk: Solution 2 Dumber 4: 00 // leave a note 4: 01 // leave a note 4: 02 if (no note from Dumber) {…} 4: 03 if (no note from Dumb) {…}

Too Much Milk: Solution 2 Dumber 4: 00 // leave a note 4: 01

Too Much Milk: Solution 2 Dumber 4: 00 // leave a note 4: 01 // leave a note 4: 02 if (no note from Dumber) {…} 4: 04 // remove the note 4: 03 if (no note from Dumb) {…}

Too Much Milk: Solution 2 Dumber 4: 00 // leave a note 4: 01

Too Much Milk: Solution 2 Dumber 4: 00 // leave a note 4: 01 // leave a note 4: 02 if (no note from Dumber) {…} 4: 04 // remove the note 4: 03 if (no note from Dumb) {…}

Too Much Milk: Solution 2 Dumber 4: 01 // leave a note 4: 02

Too Much Milk: Solution 2 Dumber 4: 01 // leave a note 4: 02 if (no note from Dumber) {…} 4: 03 if (no note from Dumb) {…} 4: 04 // remove the note 4: 05 // remove the note

Too Much Milk: Solution 2 Dumber 4: 01 // leave a note 4: 02

Too Much Milk: Solution 2 Dumber 4: 01 // leave a note 4: 02 if (no note from Dumber) {…} 4: 03 if (no note from Dumb) {…} 4: 04 // remove the note 4: 05 // remove the note

Too Much Milk: Solution 3 Dumber // leave Dumb’s note while (Dumber’s note) {

Too Much Milk: Solution 3 Dumber // leave Dumb’s note while (Dumber’s note) { }; if (no milk) { // go get milk } // remove Dumb’s note // leave Dumber’s note if (no Dumb’s note) { if (no milk) { // go get milk } } // remove Dumber’s note

Too Much Milk Solution 3 n How do we verify the correctness of a

Too Much Milk Solution 3 n How do we verify the correctness of a solution? n Test arbitrary interleaving of locking and checking locks n In this case, leaving notes and checking notes

Dumber Challenges Dumb: Case 1 Dumber // leave Dumb’s note while (Dumber’s note) {

Dumber Challenges Dumb: Case 1 Dumber // leave Dumb’s note while (Dumber’s note) { }; // leave Dumber’s note if (no milk) { // go get milk } if (no Dumb’s note) { } // remove Dumber’s note Time // remove Dumb’s note

Dumber Challenges Dumb: Case 2 Dumber // leave Dumb’s note while (Dumber’s note) {

Dumber Challenges Dumb: Case 2 Dumber // leave Dumb’s note while (Dumber’s note) { }; Time if (no milk) { // go get milk } // remove Dumb’s note // leave Dumber’s note if (no Dumb’s note) { } // remove Dumber’s note

Dumber Challenges Dumb: Case 3 Dumber // leave Dumb’s note Time while (Dumber’s note)

Dumber Challenges Dumb: Case 3 Dumber // leave Dumb’s note Time while (Dumber’s note) { }; if (no milk) { // go get milk } // remove Dumb’s note // leave Dumber’s note if (no Dumb’s note) { } // remove Dumber’s note

Dumb Challenges Dumber: Case 1 Dumber // leave Dumber’s note if (no Dumb’s note)

Dumb Challenges Dumber: Case 1 Dumber // leave Dumber’s note if (no Dumb’s note) { // leave Dumb’s note while (Dumber’s note) { }; if (no milk) { // go get milk } Time } // remove Dumber’s note if (no milk) { } // remove Dumb’s note

Dumb Challenges Dumber: Case 2 Dumber // leave Dumber’s note // leave Dumb’s note

Dumb Challenges Dumber: Case 2 Dumber // leave Dumber’s note // leave Dumb’s note while (Dumber’s note) { }; Time if (no milk) { // go get milk } // remove Dumb’s note if (no Dumb’s note) { } // remove Dumber’s note

Dumb Challenges Dumber: Case 3 Dumber // leave Dumber’s note // leave Dumb’s note

Dumb Challenges Dumber: Case 3 Dumber // leave Dumber’s note // leave Dumb’s note while (Dumber’s note) { }; if (no Dumb’s note) { } // remove Dumber’s note Time if (no milk) { // go get milk } // remove Dumb’s note

Lessons Learned n Although it works, Solution 3 is ugly n Difficult to verify

Lessons Learned n Although it works, Solution 3 is ugly n Difficult to verify correctness n Two threads have different code n n Difficult to generalize to N threads While Dumb is waiting, it consumes CPU time (busy waiting) n More elegant with higher-level primitives lock acquire(); if (no milk) { // go get milk } lock release();