1 / 15

Lecture 05 Process Synchronization

Lecture 05 Process Synchronization. Background The Critical-Section Problem. Outline. What is the problem with cooperating processes that share data? Mutual Exclusion: Software Approaches Example taken from: William Stalling . Operating Systems. Internals and Design Principles .

aglaia
Télécharger la présentation

Lecture 05 Process 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. Lecture 05Process Synchronization Background The Critical-Section Problem M.B. Ibáñez

  2. Outline • What is the problem with cooperating processes that share data? • Mutual Exclusion: Software Approaches Example taken from: William Stalling. Operating Systems. Internals and Design Principles. Third Edition. Prentice Hall. 1997. M.B. Ibáñez

  3. Process A i = 0 while ( i < 10 ) { i++; } System.out.println (“A wins”); Process B i = 0 while ( i > -10 ) { i--; } System.out.println (“B wins”); Example: Counting Conteststatic int i M.B. Ibáñez

  4. Protocol Igloo M.B. Ibáñez

  5. Concepts • Mutual Exclusion • Mechanisms that ensure that only one person or process is doing certain things at one time • Critical Section • A section of code in which only one process may be executing at a given time • Synchronization • The use of atomic operations to ensure the correct operation of cooperating processes M.B. Ibáñez

  6. Process 0 … while turn <> 0 do {nothing}; < critical section > turn := 1; ... Process 1 … while turn <> 1 do {nothing}; < critical section > turn := 0; ... First Solution: Busy Waitingvar turn: 0..1 M.B. Ibáñez

  7. Comments • This solution guarantees the mutual exclusion property. • Drawbacks: • Processes must strictly alternate in their use of their critical section • If one process fails, the other process is permanently blocked • Busy waiting: the thwarted process can do nothing productive until it gets permission to enter its critical section M.B. Ibáñez

  8. Process 0 … while flag[1] do {nothing}; flag[0] := true; < critical section >; flag[0] := false; ... Process 1 … while flag[0] do {nothing}; flag[1] := true; < critical section >; flag[1] := false; ... Second Solutionvar flag: array [0..1] of boolean; M.B. Ibáñez

  9. Comments • This solution does not guarantee mutualexclusion. Consider the following sequence: P0 executes the while statement and finds flag[1] set to false. P1 executes the while statement and finds flag[0] set to false P0 sets flag[0] to true and enter its critical section P1 sets flag[1] to true and enter its critical section M.B. Ibáñez

  10. Process 0 … flag[0] := true; while flag[1] do {nothing}; < critical section >; flag[0] := false; ... Process 0 … flag[1] := true; while flag[0] do {nothing}; < critical section >; flag[1] := false; ... Third Solutionvar flag: array [0..1] of boolean; M.B. Ibáñez

  11. Comments • Mutual exclusion is guarantee • Drawbacks: • If one process fails inside its critical section, the other process is blocked • If both processes set their flags to true before either has executed the while statement, then each think that the other has entered its critical section, causing deadlock. M.B. Ibáñez

  12. Process 0 … flag[0] := true; while flag[1] do { flag[0] := false; <delay for a short time>; flag[0] := true; }; <critical section>; flag[0] := false; ... Process 1 … flag[1] := true; while flag[0] do { flag[1] := false; <delay for a short time>; flag[1] := true; }; <critical section>; flag[1] := false; ... Fourth Solution M.B. Ibáñez

  13. Comments • The mutual exclusion is guaranteed • Consider the following sequence of events: P0 sets flag[0] to true P1 sets flag[1] to true P0 checks flag[1] P1 checks flag[0] P0 sets flag[0] to false P1 sets flag[1] to false P0 sets flag[0] to true P1 sets flag[1] to true M.B. Ibáñez

  14. procedure P0; begin repeat flag[0] := true; while flag[1] do if turn = 1 then begin flag[0] := false; while turn = 1 do {nothing}; flag[0] := true; end <critical section>; turn := 1; flag[0] := false; <remainder> forever end; procedure P1; begin repeat flag[1] := true; while flag[0] do if turn = 0 then begin flag[1] := false; while turn = 0 do {nothing}; flag[1] := true; end <critical section>; turn := 0; flag[1] := false; <remainder> forever end; A Correct SolutionDekker’s Algorithm I M.B. Ibáñez

  15. var flag array[0..1] ofboolean; turn: 0..1; begin flag[0] := false; flag[1] := false; turn := 1; parbegin P0; P1; parend end A Correct SolutionDekker’s Algorithm II M.B. Ibáñez

More Related