1 / 21

TDC 311

TDC 311. Process Synchronization. Processes. Whenever processes share things, there is a chance for screwing up things For example Two processes sharing one printer Two processes updating one database record Two processes sharing a single counter. Producer / Consumer Problem.

kelly-frye
Télécharger la présentation

TDC 311

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. TDC 311 Process Synchronization

  2. Processes • Whenever processes share things, there is a chance for screwing up things • For example • Two processes sharing one printer • Two processes updating one database record • Two processes sharing a single counter

  3. Producer / Consumer Problem • Two processes – one a producer of something, and one a consumer of something • The producer produces something and places the something into a buffer • The consumer consumes the something, taking the something out of the buffer • Once again two processes are sharing something – the buffer

  4. repeat repeat : while counter = 0 do no-op; produce an item in nextp (buffer is empty) : nextc := buffer[out]; while counter = n do no-op; out := out + 1 mod n; (buffer is full) counter := counter – 1; buffer[in] := nextp; : in := in + 1 mod n; consume the item in nextc counter := counter + 1; : until false; until false; PRODUCER CONSUMER

  5. Producer and Consumer • Note that both the producer and the consumer reference variable counter • In the producer: counter := counter + 1 • which in machine language is: • register1 := counter; • register1 := register1 + 1; • counter := register1;

  6. Producer and Consumer • In the consumer: counter := counter - 1 • Which in machine language is: • register2 := counter • register2 := register2 - 1 • counter := register2 • counter could be 4, 5 or 6 depending upon where the code is interrupted • For example:

  7. Producer and Consumer • counter is at 5 and producer starts: • register1 := counter; register1 = 5 • register1 := register1 + 1; register1 = 6 • the producer is swapped out and the consumer starts anew • register2 := counter; register2 = 5 • register2 := register2 - 1; register2 = 4 • the consumer is swapped out and the producer returns • counter := register1; counter = 6 • the producer is done, and the consumer returns • counter := register2; counter = 4 • counter should be 5!

  8. Critical Sections • Thus, the accessing of counter is a critical section • What happens above is called a race condition • Must ensure than ONLY one process at a time references counter • We need process synchronization

  9. Critical Section • To solve critical section problem, three things are needed • Mutual exclusion – only one at a time • Progress – process moves through event • Bounded waiting – everyone has to get a turn

  10. Semaphores • A simple, clean way to support process synchronization • Supported by many OSs and even some processors • A semaphore is nothing more than an integer. • But, this integer can only be accessed (except for initialization) through two atomic operations:

  11. Wait and Signal Semaphores • wait(s): if s = 0 then go to sleep; s := s – 1; • signal(s): s := s + 1; wakeup sleeping process;

  12. Semaphores • To use semaphores, create a structure that looks like the following: repeat : wait(semaphore) : (critical section) : signal(semaphore) : until false;

  13. Database Example • Database update in which you want to lock out all but one concurrent user procedure UpdateDatabase; var s: semaphore (:=1); begin : (prompt user for data) : wait(s); Update(record); signal(s); : end;

  14. CD-ROM Reader Example • CD-ROM reader which allows 5 concurrent users procedure ReadCD; var t: semaphore (:=5); begin : (prompt user for data) : wait(t); ReadCD(buffer); signal(t); : end;

  15. Expanded Producer/Consumer Problem • Let’s re-visit the producer/consumer problem and solve the problem using semaphores (on the whiteboard)

  16. var s: semaphore (:=1); n: semaphore (:=0); e: semaphore (:=sizeofbuffer); procedure producer; begin repeat produce item for buffer; wait (e); wait(s); append to buffer; signal(s); signal(n); forever; end; procedure consumer; begin repeat wait(n); wait(s); take from buffer; signal(s); signal(e); consume item forever; end;

  17. Deadlock • When two or more processes have conflicting needs for resources, we have deadlock • Example: P1 holds the disk and wants the tape; P2 holds the tape and wants the disk • Example: P1 has 80K or memory and wants 60K more; P2 has 70K of memory and wants 80K more; there is only 150K total of memory

  18. Deadlock • Three conditions must first exist for deadlock to occur: • Mutual exclusion • Hold and wait • No preemption • Then the following must happen: • Circular wait • How do you avoid deadlock?

  19. Deadlock Prevention • Indirect methods – stop one of the conditions 1-3 from happening (not desirable) • Direct methods – stop occurrence of condition 4 from happening • For example: Order all resources from 0 to N. If you have resource m, you can only ask for a resource > m, where Disk=5; Tape=7; Printer=12

  20. Deadlock Avoidance • Really is prevention, but not as restrictive • Avoidance allows the 3 conditions but makes dynamic decisions whether a current resource allocation request will lead to deadlock, such as Bankers’ Algorithm

  21. Deadlock Detection • Use an algorithm that can detect cycles in directed graphs • Costly, as OS must maintain graphs and check for cycles

More Related