1 / 41

Synchronization

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:00 Look into fridge: Out of milk. Dumber. Motivating Example: Too Much Milk.

milly
Download Presentation

Synchronization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Synchronization

  2. 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

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

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

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

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

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

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

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

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

  11. 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

  12. 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

  13. 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)

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

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

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

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

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

  19. Dumb 4: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) { Too Much Milk: Solution 1

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

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

  22. Dumb 4: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 Too Much Milk: Solution 1

  23. 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?

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

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

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

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

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

  29. Dumb 4: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) {…} Too Much Milk: Solution 2

  30. Dumb 4: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) {…} Too Much Milk: Solution 2

  31. Dumb 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) {…} 4:05 // remove the note Too Much Milk: Solution 2

  32. Dumb 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) {…} 4:05 // remove the note Too Much Milk: Solution 2

  33. 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) { if (no milk) { // go get milk } } // remove Dumber’s note Too Much Milk: Solution 3

  34. 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

  35. 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 Dumber Challenges Dumb: Case 1 Time

  36. 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 Dumber Challenges Dumb: Case 2 Time

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

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

  39. 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 Dumb Challenges Dumber: Case 2 Time

  40. 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 Dumb Challenges Dumber: Case 3 Time

  41. 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 lockacquire(); if (no milk) { // go get milk } lockrelease();

More Related