Presentation is loading. Please wait.

Presentation is loading. Please wait.

Concurrent Programming James Adkison 02/28/2008. What is concurrency? “happens-before relation – A happens before B if A and B belong to the same process.

Similar presentations


Presentation on theme: "Concurrent Programming James Adkison 02/28/2008. What is concurrency? “happens-before relation – A happens before B if A and B belong to the same process."— Presentation transcript:

1 Concurrent Programming James Adkison 02/28/2008

2 What is concurrency? “happens-before relation – A happens before B if A and B belong to the same process and A occurred before B; or A is the sending of a message and B is the receiving of that message” (Deitel)

3 What is concurrency? “concurrent – Two events are concurrent if it cannot be determined which event occurred earlier by following the happens- before relation” (Deitel)

4 Assumptions Using a single processor system Working with threads, not processes Working with only two threads Any level of interleaving is possible Machine-level instructions are atomic (i.e. are executed indivisibly)

5 Why Use Concurrency? Improved throughput  throughput – Amount of work performed per unit time. Improved responsiveness  e.g. responsive GUI Appropriate / Required for problem domain

6 Concurrency Example: Producer / Consumer Producer:  Writes integers 1 – 4 (in order) to a shared buffer and terminates Consumer:  Reads and sums the integers 1 – 4 from the shared buffer  Display the sum total and terminate

7 Producer / Consumer – Output Correct (Desired) Output: Producer writes 1 Consumer reads 1 Producer writes 2 Consumer reads 2 Producer writes 3 Consumer reads 3 Producer writes 4 Terminating: Producer. Consumer reads 4 Consumer read values totaling: 10. Terminating: Consumer.

8 Producer / Consumer – Output Correct (Desired) Output: Producer writes 1 Consumer reads 1 Producer writes 2 Consumer reads 2 Producer writes 3 Consumer reads 3 Producer writes 4 Terminating: Producer. Consumer reads 4 Consumer read values totaling: 10. Terminating: Consumer. Sample Output: Consumer reads -1 Producer writes 1 Consumer reads 1 Producer writes 2 Producer writes 3 Consumer reads 3 Consumer reads values totaling: 6. Terminating: Consumer. Producer writes 4 Terminating: producer.

9 Critical Section (CS) “When a thread is accessing shared modifiable data.” (Deitel) Producer / Consumer: the shared mutable buffer int x = 10; Thread1Thread2 Read x Write 3xWrite -2x Critical Section

10 Mutual Exclusion “Restriction whereby execution by a thread of its critical section precludes execution by other threads of their critical sections.” (Deitel) “Mutual exclusion is crucial to correct execution when multiple threads access shared writable data.” (Deitel)

11 Implementing Mutual Exclusion A purely software solution Constraint 1:  A thread outside of its critical section can not block another thread from entering the critical section Constraint 2:  A thread must not be indefinitely postponed from entering its critical section

12 Mutual Exclusion – Version 1 // Thread1... while (!done) { // Enter mutual exclusion while ( threadNumber == 2 ); // Critical section threadNumber = 2; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( threadNumber == 1 ); // Critical section threadNumber = 1; // Outside critical section }... int threadNumber = 1; // globally accessible to Thread1 and Thread2

13 Version 1 – Is Mutual Exclusion Guaranteed? // Thread1... while (!done) { // Enter mutual exclusion while ( threadNumber == 2 ); // Critical section threadNumber = 2; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( threadNumber == 1 ); // Critical section threadNumber = 1; // Outside critical section }... int threadNumber = 1; // globally accessible to Thread1 and Thread2

14 Version 1 – Guarantees mutual exclusion // Thread1... while (!done) { // Enter mutual exclusion while ( threadNumber == 2 ); // Critical section threadNumber = 2; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( threadNumber == 1 ); // Critical section threadNumber = 1; // Outside critical section }... int threadNumber = 1; // globally accessible to Thread1 and Thread2

15 Version 1 – Guarantees mutual exclusion // Thread1... while (!done) { // Enter mutual exclusion while ( threadNumber == 2 ); // Critical section threadNumber = 2; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( threadNumber == 1 ); // Critical section threadNumber = 1; // Outside critical section }... int threadNumber = 1; // globally accessible to Thread1 and Thread2

16 Version 1 – Guarantees mutual exclusion // Thread1... while (!done) { // Enter mutual exclusion while ( threadNumber == 2 ); // Critical section threadNumber = 2; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( threadNumber == 1 ); // Critical section threadNumber = 1; // Outside critical section }... int threadNumber = 1; // globally accessible to Thread1 and Thread2

17 Version 1 – Guarantees mutual exclusion // Thread1... while (!done) { // Enter mutual exclusion while ( threadNumber == 2 ); // Critical section threadNumber = 2; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( threadNumber == 1 ); // Critical section threadNumber = 1; // Outside critical section }... int threadNumber = 1; // globally accessible to Thread1 and Thread2

18 Version 1 – Guarantees mutual exclusion // Thread1... while (!done) { // Enter mutual exclusion while ( threadNumber == 2 ); // Critical section threadNumber = 2; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( threadNumber == 1 ); // Critical section threadNumber = 1; // Outside critical section }... int threadNumber = 1; // globally accessible to Thread1 and Thread2

19 Version 1 – Guarantees mutual exclusion // Thread1... while (!done) { // Enter mutual exclusion while ( threadNumber == 2 ); // Critical section threadNumber = 2; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( threadNumber == 1 ); // Critical section threadNumber = 1; // Outside critical section }... int threadNumber = 1; // globally accessible to Thread1 and Thread2

20 Version 1 – Are Both Constraints Satisfied? // Thread1... while (!done) { // Enter mutual exclusion while ( threadNumber == 2 ); // Critical section threadNumber = 2; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( threadNumber == 1 ); // Critical section threadNumber = 1; // Outside critical section }... int threadNumber = 1; // globally accessible to Thread1 and Thread2

21 Version 1 – Violates Constraint1 // Thread1... while (!done) { // Enter mutual exclusion while ( threadNumber == 2 ); // Critical section threadNumber = 2; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( threadNumber == 1 ); // Critical section threadNumber = 1; // Outside critical section }... int threadNumber = 1; // globally accessible to Thread1 and Thread2

22 Version 1 – Any new problems? // Thread1... while (!done) { // Enter mutual exclusion while ( threadNumber == 2 ); // Critical section threadNumber = 2; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( threadNumber == 1 ); // Critical section threadNumber = 1; // Outside critical section }... int threadNumber = 1; // globally accessible to Thread1 and Thread2

23 Version 1 – Lockstep Synchronization Problem // Thread1... while (!done) { // Enter mutual exclusion while ( threadNumber == 2 ); // Critical section threadNumber = 2; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( threadNumber == 1 ); // Critical section threadNumber = 1; // Outside critical section }... int threadNumber = 1; // globally accessible to Thread1 and Thread2

24 Version 1 – Lockstep Synchronization Problem // Thread1... while (!done) { // Enter mutual exclusion while ( threadNumber == 2 ); // Critical section threadNumber = 2; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( threadNumber == 1 ); // Critical section threadNumber = 1; // Outside critical section }... int threadNumber = 1; // globally accessible to Thread1 and Thread2

25 Version 1 – Lockstep Synchronization Problem // Thread1... while (!done) { // Enter mutual exclusion while ( threadNumber == 2 ); // Critical section threadNumber = 2; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( threadNumber == 1 ); // Critical section threadNumber = 1; // Outside critical section }... int threadNumber = 1; // globally accessible to Thread1 and Thread2

26 Version 1 – Assessment Guarantees mutual exclusion Drawbacks:  Violates constraint 1  Lockstep synchronization problem

27 Version 1 – Definitions “busy waiting – Form of waiting where a thread continuously test a condition that will let the thread proceed eventually; while busy waiting, a thread uses processor time” (Deitel) “lockstep synchronization – Situation where asynchronous threads execute code in strict alternation” (Deitel)

28 Mutual Exclusion – Version 2 // Thread1... while (!done) { // Enter mutual exclusion while ( t2Inside ); t1Inside = true; // Critical section t1Inside = false; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( t1Inside ); t2Inside = true; // Critical section t2Inside = false; // Outside critical section }... boolean t1Inside = false; // t1Inside and t2Inside are globally boolean t2Inside = false; // accessible to Thread1 and Thread2

29 Version 2 – Is Constraint1 Satisfied Now? // Thread1... while (!done) { // Enter mutual exclusion while ( t2Inside ); t1Inside = true; // Critical section t1Inside = false; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( t1Inside ); t2Inside = true; // Critical section t2Inside = false; // Outside critical section }... boolean t1Inside = false; // t1Inside and t2Inside are globally boolean t2Inside = false; // accessible to Thread1 and Thread2

30 Version 2 – Obeys Constraint1 // Thread1... while (!done) { // Enter mutual exclusion while ( t2Inside ); t1Inside = true; // Critical section t1Inside = false; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( t1Inside ); t2Inside = true; // Critical section t2Inside = false; // Outside critical section }... boolean t1Inside = false; // t1Inside and t2Inside are globally boolean t2Inside = false; // accessible to Thread1 and Thread2

31 Version 2 – Obeys Constraint1 // Thread1... while (!done) { // Enter mutual exclusion while ( t2Inside ); t1Inside = true; // Critical section t1Inside = false; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( t1Inside ); t2Inside = true; // Critical section t2Inside = false; // Outside critical section }... boolean t1Inside = false; // t1Inside and t2Inside are globally boolean t2Inside = false; // accessible to Thread1 and Thread2

32 Version 2 – Does the Lockstep Synchronization Problem Persist? // Thread1... while (!done) { // Enter mutual exclusion while ( t2Inside ); t1Inside = true; // Critical section t1Inside = false; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( t1Inside ); t2Inside = true; // Critical section t2Inside = false; // Outside critical section }... boolean t1Inside = false; // t1Inside and t2Inside are globally boolean t2Inside = false; // accessible to Thread1 and Thread2

33 Version 2 – Resolves Lockstep Synchronization // Thread1... while (!done) { // Enter mutual exclusion while ( t2Inside ); t1Inside = true; // Critical section t1Inside = false; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( t1Inside ); t2Inside = true; // Critical section t2Inside = false; // Outside critical section }... boolean t1Inside = false; // t1Inside and t2Inside are globally boolean t2Inside = false; // accessible to Thread1 and Thread2

34 Version 2 – Resolves Lockstep Synchronization // Thread1... while (!done) { // Enter mutual exclusion while ( t2Inside ); t1Inside = true; // Critical section t1Inside = false; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( t1Inside ); t2Inside = true; // Critical section t2Inside = false; // Outside critical section }... boolean t1Inside = false; // t1Inside and t2Inside are globally boolean t2Inside = false; // accessible to Thread1 and Thread2

35 Version 2 – Resolves Lockstep Synchronization // Thread1... while (!done) { // Enter mutual exclusion while ( t2Inside ); t1Inside = true; // Critical section t1Inside = false; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( t1Inside ); t2Inside = true; // Critical section t2Inside = false; // Outside critical section }... boolean t1Inside = false; // t1Inside and t2Inside are globally boolean t2Inside = false; // accessible to Thread1 and Thread2

36 Version 2 – Any new problems? // Thread1... while (!done) { // Enter mutual exclusion while ( t2Inside ); t1Inside = true; // Critical section t1Inside = false; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( t1Inside ); t2Inside = true; // Critical section t2Inside = false; // Outside critical section }... boolean t1Inside = false; // t1Inside and t2Inside are globally boolean t2Inside = false; // accessible to Thread1 and Thread2

37 Version 2 – Violates Mutual Exclusion // Thread1... while (!done) { // Enter mutual exclusion while ( t2Inside ); t1Inside = true; // Critical section t1Inside = false; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( t1Inside ); t2Inside = true; // Critical section t2Inside = false; // Outside critical section }... boolean t1Inside = false; // t1Inside and t2Inside are globally boolean t2Inside = false; // accessible to Thread1 and Thread2

38 Version 2 – Violates Mutual Exclusion // Thread1... while (!done) { // Enter mutual exclusion while ( t2Inside ); t1Inside = true; // Critical section t1Inside = false; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( t1Inside ); t2Inside = true; // Critical section t2Inside = false; // Outside critical section }... boolean t1Inside = false; // t1Inside and t2Inside are globally boolean t2Inside = false; // accessible to Thread1 and Thread2

39 Version 2 – Resolves Lockstep Synchronization // Thread1... while (!done) { // Enter mutual exclusion while ( t2Inside ); t1Inside = true; // Critical section t1Inside = false; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion while ( t1Inside ); t2Inside = true; // Critical section t2Inside = false; // Outside critical section }... boolean t1Inside = false; // t1Inside and t2Inside are globally boolean t2Inside = false; // accessible to Thread1 and Thread2

40 Version 2 – Assessment Eliminates lockstep synchronization Does NOT guarantee mutual exclusion

41 Mutual Exclusion – Version 3 // Thread1... while (!done) { // Enter mutual exclusion t1WantsIn = true; while ( t2WantsIn ); // Critical section t1WantsIn = false; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion t2WantsIn = true; while ( t1WantsIn ); // Critical section t2WantsIn = false; // Outside critical section }... boolean t1WantsIn = false; // t1WantsIn and t2WantsIn are globally boolean t2WantsIn = false; // accessible to Thread1 and Thread2

42 Version 3 – Any new problems? // Thread1... while (!done) { // Enter mutual exclusion t1WantsIn = true; while ( t2WantsIn ); // Critical section t1WantsIn = false; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion t2WantsIn = true; while ( t1WantsIn ); // Critical section t2WantsIn = false; // Outside critical section }... boolean t1WantsIn = false; // t1WantsIn and t2WantsIn are globally boolean t2WantsIn = false; // accessible to Thread1 and Thread2

43 Version 3 – Introduces Deadlock // Thread1... while (!done) { // Enter mutual exclusion t1WantsIn = true; while ( t2WantsIn ); // Critical section t1WantsIn = false; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion t2WantsIn = true; while ( t1WantsIn ); // Critical section t2WantsIn = false; // Outside critical section }... boolean t1WantsIn = false; // t1WantsIn and t2WantsIn are globally boolean t2WantsIn = false; // accessible to Thread1 and Thread2

44 Version 3 – Introduces Deadlock // Thread1... while (!done) { // Enter mutual exclusion t1WantsIn = true; while ( t2WantsIn ); // Critical section t1WantsIn = false; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion t2WantsIn = true; while ( t1WantsIn ); // Critical section t2WantsIn = false; // Outside critical section }... boolean t1WantsIn = false; // t1WantsIn and t2WantsIn are globally boolean t2WantsIn = false; // accessible to Thread1 and Thread2

45 Version 3 – Introduces Deadlock // Thread1... while (!done) { // Enter mutual exclusion t1WantsIn = true; while ( t2WantsIn ); // Critical section t1WantsIn = false; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion t2WantsIn = true; while ( t1WantsIn ); // Critical section t2WantsIn = false; // Outside critical section }... boolean t1WantsIn = false; // t1WantsIn and t2WantsIn are globally boolean t2WantsIn = false; // accessible to Thread1 and Thread2

46 Version 3 – Assessment Guarantees mutual exclusion Prevents lockstep synchronization Introduces a new problem: deadlock

47 Version 3 – Definition “deadlock – Situation in which a process or thread is waiting for an event that will never occur and therefore cannot continue execution” (Deitel)

48 Mutual Exclusion – Version 4 // Thread1... while (!done) { // Enter mutual exclusion t1WantsIn = true; while ( t2WantsIn ) { t1WantsIn = false; // wait a random time t1WantsIn = true; } // Critical section t1WantsIn = false; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion t2WantsIn = true; while ( t1WantsIn ) { t2WantsIn = false; // wait a random time t2WantsIn = true; } // Critical section t2WantsIn = false; // Outside critical section }... boolean t1WantsIn = false; // t1WantsIn and t2WantsIn are globally boolean t2WantsIn = false; // accessible to Thread1 and Thread2

49 Version 4 – Any new problems? // Thread1... while (!done) { // Enter mutual exclusion t1WantsIn = true; while ( t2WantsIn ) { t1WantsIn = false; // wait a random time t1WantsIn = true; } // Critical section t1WantsIn = false; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion t2WantsIn = true; while ( t1WantsIn ) { t2WantsIn = false; // wait a random time t2WantsIn = true; } // Critical section t2WantsIn = false; // Outside critical section }... boolean t1WantsIn = false; // t1WantsIn and t2WantsIn are globally boolean t2WantsIn = false; // accessible to Thread1 and Thread2

50 Mutual Exclusion – Introduces Indefinite Postponement // Thread1... while (!done) { // Enter mutual exclusion t1WantsIn = true; while ( t2WantsIn ) { t1WantsIn = false; // wait a random time t1WantsIn = true; } // Critical section t1WantsIn = false; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion t2WantsIn = true; while ( t1WantsIn ) { t2WantsIn = false; // wait a random time t2WantsIn = true; } // Critical section t2WantsIn = false; // Outside critical section }... boolean t1WantsIn = false; // t1WantsIn and t2WantsIn are globally boolean t2WantsIn = false; // accessible to Thread1 and Thread2

51 Mutual Exclusion – Introduces Indefinite Postponement // Thread1... while (!done) { // Enter mutual exclusion t1WantsIn = true; while ( t2WantsIn ) { t1WantsIn = false; // wait a random time t1WantsIn = true; } // Critical section t1WantsIn = false; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion t2WantsIn = true; while ( t1WantsIn ) { t2WantsIn = false; // wait a random time t2WantsIn = true; } // Critical section t2WantsIn = false; // Outside critical section }... boolean t1WantsIn = false; // t1WantsIn and t2WantsIn are globally boolean t2WantsIn = false; // accessible to Thread1 and Thread2

52 Mutual Exclusion – Introduces Indefinite Postponement // Thread1... while (!done) { // Enter mutual exclusion t1WantsIn = true; while ( t2WantsIn ) { t1WantsIn = false; // wait a random time t1WantsIn = true; } // Critical section t1WantsIn = false; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion t2WantsIn = true; while ( t1WantsIn ) { t2WantsIn = false; // wait a random time t2WantsIn = true; } // Critical section t2WantsIn = false; // Outside critical section }... boolean t1WantsIn = false; // t1WantsIn and t2WantsIn are globally boolean t2WantsIn = false; // accessible to Thread1 and Thread2

53 Mutual Exclusion – Introduces Indefinite Postponement // Thread1... while (!done) { // Enter mutual exclusion t1WantsIn = true; while ( t2WantsIn ) { t1WantsIn = false; // wait a random time t1WantsIn = true; } // Critical section t1WantsIn = false; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion t2WantsIn = true; while ( t1WantsIn ) { t2WantsIn = false; // wait a random time t2WantsIn = true; } // Critical section t2WantsIn = false; // Outside critical section }... boolean t1WantsIn = false; // t1WantsIn and t2WantsIn are globally boolean t2WantsIn = false; // accessible to Thread1 and Thread2

54 Mutual Exclusion – Introduces Indefinite Postponement // Thread1... while (!done) { // Enter mutual exclusion t1WantsIn = true; while ( t2WantsIn ) { t1WantsIn = false; // wait a random time t1WantsIn = true; } // Critical section t1WantsIn = false; // Outside critical section }... // Thread2... while (!done) { // Enter mutual exclusion t2WantsIn = true; while ( t1WantsIn ) { t2WantsIn = false; // wait a random time t2WantsIn = true; } // Critical section t2WantsIn = false; // Outside critical section }... boolean t1WantsIn = false; // t1WantsIn and t2WantsIn are globally boolean t2WantsIn = false; // accessible to Thread1 and Thread2

55 Version 4 – Assessment Guarantees mutual exclusion Prevents lockstep synchronization Prevents deadlock Introduces a new problem: indefinite postponement

56 Version 4 – Definition “indefinite postponement – Situation in which a thread waits for an event that might never occur” (Deitel)

57 Mutual Exclusion – Dekker’s Algorithm “An elegant software implementation of mutual exclusion was first presented by Dekker, a Dutch mathematician” (Deitel)

58 Dijkstra’s development of Dekker’s Algorithm // Thread1 while (!done) { t1WantsIn = true; while ( t2WantsIn ) { if ( favoredThread == 2 ) { t1WantsIn = false; while (favoredThread == 2); t1WantsIn = true; } // Critical section favoredThread = 2; t1WantsIn = false; // Outside critical section } int favoredThread = 1; // favoredThread, t1WantsIn, and t2WantsIn boolean t1WantsIn = false; // are globally accessible boolean t2WantsIn = false; // to Thread1 and Thread2 // Thread1 while (!done) { t2WantsIn = true; while ( t1WantsIn ) { if ( favoredThread == 1 ) { t2WantsIn = false; while (favoredThread == 1); t2WantsIn = true; } // Critical section favoredThread = 1; t2WantsIn = false; // Outside critical section }

59 Dekker’s Algorithm – Assessment Guarantees mutual exclusion Prevents lockstep synchronization Prevents deadlock Prevents indefinite postponement

60 Peterson’s Algorithm “In 1981, G. L. Peterson published a simpler algorithm for enforcing two- process mutual exclusion with busy waiting.” (Deitel)

61 Peterson’s Algorithm // Thread1 while (!done) { t1WantsIn = true; favoredThread = 2; while ( t2WantsIn && favoredThread == 2 ); // Critical section t1WantsIn = false; // Outside critical section } // Thread2 while (!done) { t2WantsIn = true; favoredThread = 1; while ( t1WantsIn ) && favoredThread == 1 ); // Critical section t2WantsIn = false; // Outside critical section } int favoredThread = 1; // favoredThread, t1WantsIn, and t2WantsIn boolean t1WantsIn = false; // are globally accessible boolean t2WantsIn = false; // to Thread1 and Thread2

62 Mutual Exclusion Primitives Software: 2-Thread Mutual Exclusion  Dekker / Dijkstra – Dekker’s Algorithm  G. L. Peterson – Peterson’s Algorithm n-Thread Mutual Exclusion  Leslie Lamport – Lamport’s Bakery Algorithm Can be done in Hardware also  Semaphores – Dijkstra

63 Mutual Exclusion Primitives The methods we’ve seen so far are very basic and are error prone. Being so primitive, solving more complex concurrency problems becomes very challenging Conclusion, higher-level – simpler mutual exclusion structures are needed

64 Monitors Object that contains both data and procedures Allocates a serially reusable shared resource Only 1 thread can enter the monitor (mutual exclusion) Data inside monitor is only accessible within the monitor

65 Monitor – Definitions “serially reusable resource – Resource that may be used by only one process at a time (aka a dedicated resource)” (Deitel)

66 Monitors Continued Condition variables  wait (conditionVariable) Thread waits outside the monitor  signal (conditionVariable) A waiting thread may enter the monitor Methods:  signal-and-exit  signal-and-continue (Java)

67 Monitors Using Circular Buffers Circular Buffer (aka Bounded Buffer) Could be an array that wraps around Allows the Producer to get ahead of the Consumer without waiting Waits are necessary if the buffer becomes full or empty Efficient when Producer & Consumer execute at about the same speed

68 Deadlock Solutions Prevention  Clean solution, but often results in poor resource utilization Avoidance  Dijkstra’s Banker’s Algorithm Detection / Recovery

69 James Adkison Concurrent Programming Homework Questions  1. What’s the difference between deadlock and indefinite postponement (livelock)? Hint: Definitions are provided but were not shown during the presentation.  2. [True or False] When 8 and only 8 threads perform read-only operations on a immutable shared resource mutual exclusion must be guaranteed?  3. [True or False] Mutual exclusion must be guaranteed when threads share a resource that is mutable?

70 Works Cited Deitel, Harvey M., Paul J. Deitel, and David R. Choffnes. Operating Systems Thrid Edition. New Jersey: Pearson Education, Inc., 2004


Download ppt "Concurrent Programming James Adkison 02/28/2008. What is concurrency? “happens-before relation – A happens before B if A and B belong to the same process."

Similar presentations


Ads by Google