Synchronization - PowerPoint PPT Presentation

About This Presentation
Title:

Synchronization

Description:

Title: PowerPoint Presentation Last modified by: xyuan Created Date: 1/1/1601 12:00:00 AM Document presentation format: On-screen Show Other titles – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 42
Provided by: csFsuEdux
Learn more at: http://www.cs.fsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Synchronization


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 others presence

Robot Dumber
Robot Dumb
3
Motivating Example Too Much Milk
  • Dumb
  • 400 Look into fridge
  • Out of milk
  • Dumber

4
Motivating Example Too Much Milk
  • Dumb
  • 400 Look into fridge
  • Out of milk
  • 405 Head for the warehouse
  • Dumber

5
Motivating Example Too Much Milk
  • Dumb
  • 405 Head for the warehouse
  • Dumber
  • 410 Look into fridge
  • Out of milk

6
Motivating Example Too Much Milk
  • Dumb
  • Dumber
  • 410 Look into fridge
  • Out of milk
  • 415 Head for the warehouse

7
Motivating Example Too Much Milk
  • Dumb
  • 420 Arrive with milk
  • Dumber
  • 415 Head for the warehouse

8
Motivating Example Too Much Milk
  • Dumb
  • 420 Arrive with milk
  • Dumber
  • 415 Head for the warehouse

9
Motivating Example Too Much Milk
  • Dumb
  • 420 Arrive with milk
  • 425 Go party
  • Dumber

10
Motivating Example Too Much Milk
  • Dumb
  • 420 Arrive with milk
  • 425 Go party
  • Dumber
  • 430 Arrive with milk Uh oh

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)
  • Dont 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
Too Much Milk Solution 1
  • Dumb
  • 400 if (no milk)
  • Dumber

16
Too Much Milk Solution 1
  • Dumb
  • 400 if (no milk)
  • Dumber
  • 401 if (no milk)

17
Too Much Milk Solution 1
  • Dumb
  • 400 if (no milk)
  • Dumber
  • 401 if (no milk)
  • 402 if (no note)

18
Too Much Milk Solution 1
  • Dumb
  • 400 if (no milk)
  • 403 if (no note)
  • Dumber
  • 401 if (no milk)
  • 402 if (no note)

19
Too Much Milk Solution 1
  • Dumb
  • 400 if (no milk)
  • 403 if (no note)
  • 404 // leave a note
  • Dumber
  • 401 if (no milk)
  • 402 if (no note)

20
Too Much Milk Solution 1
  • Dumb
  • 403 if (no note)
  • 404 // leave a note
  • Dumber
  • 401 if (no milk)
  • 402 if (no note)
  • 405 // leave a note

21
Too Much Milk Solution 1
  • Dumb
  • 403 if (no note)
  • 404 // leave a note
  • 406 // go get milk
  • Dumber
  • 402 if (no note)
  • 405 // leave a note

22
Too Much Milk Solution 1
  • Dumb
  • 403 if (no note)
  • 404 // leave a note
  • 406 // go get milk
  • Dumber
  • 405 // leave a note
  • 407 // go get milk

23
Too Much Milk Solution 2
  • Okaysolution 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
Too Much Milk Solution 2
  • Dumb
  • 400 // leave a note
  • Dumber

26
Too Much Milk Solution 2
  • Dumb
  • 400 // leave a note
  • Dumber
  • 401 // leave a note

27
Too Much Milk Solution 2
  • Dumb
  • 400 // leave a note
  • 402 if (no note from Dumber)
  • Dumber
  • 401 // leave a note

28
Too Much Milk Solution 2
  • Dumb
  • 400 // leave a note
  • 402 if (no note from Dumber)
  • Dumber
  • 401 // leave a note
  • 403 if (no note from Dumb)

29
Too Much Milk Solution 2
  • Dumb
  • 400 // leave a note
  • 402 if (no note from Dumber)
  • 404 // remove the note
  • Dumber
  • 401 // leave a note
  • 403 if (no note from Dumb)

30
Too Much Milk Solution 2
  • Dumb
  • 400 // leave a note
  • 402 if (no note from Dumber)
  • 404 // remove the note
  • Dumber
  • 401 // leave a note
  • 403 if (no note from Dumb)

31
Too Much Milk Solution 2
  • Dumb
  • 402 if (no note from Dumber)
  • 404 // remove the note
  • Dumber
  • 401 // leave a note
  • 403 if (no note from Dumb)
  • 405 // remove the note

32
Too Much Milk Solution 2
  • Dumb
  • 402 if (no note from Dumber)
  • 404 // remove the note
  • Dumber
  • 401 // leave a note
  • 403 if (no note from Dumb)
  • 405 // remove the note

33
Too Much Milk Solution 3
  • Dumb
  • // leave Dumbs note
  • while (Dumbers note)
  • if (no milk)
  • // go get milk
  • // remove Dumbs note
  • Dumber
  • // leave Dumbers note
  • if (no Dumbs note)
  • if (no milk)
  • // go get milk
  • // remove Dumbers note

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
Dumber Challenges Dumb Case 1
  • Dumb
  • // leave Dumbs note
  • while (Dumbers note)
  • if (no milk)
  • // go get milk
  • // remove Dumbs note
  • Dumber
  • // leave Dumbers note
  • if (no Dumbs note)
  • // remove Dumbers note

Time
36
Dumber Challenges Dumb Case 2
  • Dumb
  • // leave Dumbs note
  • while (Dumbers note)
  • if (no milk)
  • // go get milk
  • // remove Dumbs note
  • Dumber
  • // leave Dumbers note
  • if (no Dumbs note)
  • // remove Dumbers note

Time
37
Dumber Challenges Dumb Case 3
  • Dumb
  • // leave Dumbs note
  • while (Dumbers note)
  • if (no milk)
  • // remove Dumbs note
  • Dumber
  • // leave Dumbers note
  • if (no Dumbs note)
  • // remove Dumbers note

Time
38
Dumb Challenges Dumber Case 1
  • Dumb
  • // leave Dumbs note
  • while (Dumbers note)
  • if (no milk)
  • // remove Dumbs note
  • Dumber
  • // leave Dumbers note
  • if (no Dumbs note)
  • if (no milk)
  • // go get milk
  • // remove Dumbers note

Time
39
Dumb Challenges Dumber Case 2
  • Dumb
  • // leave Dumbs note
  • while (Dumbers note)
  • if (no milk)
  • // go get milk
  • // remove Dumbs note
  • Dumber
  • // leave Dumbers note
  • if (no Dumbs note)
  • // remove Dumbers note

Time
40
Dumb Challenges Dumber Case 3
  • Dumb
  • // leave Dumbs note
  • while (Dumbers note)
  • if (no milk)
  • // go get milk
  • // remove Dumbs note
  • Dumber
  • // leave Dumbers note
  • if (no Dumbs note)
  • // remove Dumbers note

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()
Write a Comment
User Comments (0)
About PowerShow.com