1 / 18

Concurrency Intro

Concurrency Intro. Interleaving (multiprogramming) produces many problems Relative speed of two processes is not generally predictable Must be careful when sharing global resources O.S. Data Structures, Files, etc. Protecting system from bad processes

Download Presentation

Concurrency Intro

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. Concurrency Intro • Interleaving (multiprogramming) produces many problems • Relative speed of two processes is not generally predictable • Must be careful when sharing global resources • O.S. Data Structures, Files, etc. • Protecting system from bad processes • Difficult to allocate resources optimally if a process is blocked or suspended • Errors tend to be difficult to find • Often dependent on exact timing • Example: Interrupt A requires 1ms to process, but error occurs if interrupt B happens during that period • Multiprocessing increases problems, but not drastically

  2. Definitions • Mutual Exclusion – Making sure two processes can’t both have a resource at the same time • Critical Section – The area of a program where a resource is being used. (Processes must have mutual exclusion while executing a critical section.)

  3. Definitions • Deadlock – When two or more processes halt, unable to proceed • P1 is using A, needs B • P2 is using B, needs A Both wait for one another indefinitely!! (i.e. the wait is mutual) • Starvation – A process is denied a resource indefinitely • P1 using A, P2 and P3 wait for A • P2 gets A when P1 done • P1 comes in, P1 and P3 wait for A • And so on …

  4. Concurrency Requirements • Mutual Exclusion must be enforced • Only one process at a time may be accessing the critical section • A process can halt outside the critical section without harm • No deadlock or starvation (a process that wants to will eventually get into critical section)

  5. Concurrency Requirements • If no process is in a critical section, a process requesting entry must be allowed to enter without delay • No assumptions about number or relative speed of processes or processors • A process remains inside its critical section for a limited period of time • Can be solved by software or hardware, with or without O.S. support

  6. Process P1 -- Start CS -- Load r, n Add r, 1 Store r, n -- End CS -- Process P2 -- Start CS – Load r, n Add r, 1 Store r, n -- End CS -- Use of critical section avoids race conditions!

  7. Software Approaches • Turn variable (figure 5.2a) P0 P1 while(turn!=0)/**/; while(turn!=1)/**/; (Do nothing loop) (Do nothing loop) critical section critical section turn = 1; turn = 0; • Shared variable turn indicates who is allowed to enter next • can enter if turn = me • On exit, point variable to other process • Starvation if other process never enters

  8. Software Approaches • “Busy” Flag (figure 5.2b) P0 P1 while(flag[1])/**/; while(flag[0]) /**/; flag[0] = true; flag[1] = true; critical section critical section flag[0] = false; flag[1] = false; • Each process has a flag to indicate it is in the critical section • Fails mutual exclusion if processes are in lockstep *** Somebody explain this!!! ***

  9. Software Approaches • Busy Flag Modified (figure 5.2c) P0 P1 flag[0] = true; flag[1] = true; while ( flag[1] ) /**/; while ( flag[0] ) /**/; critical section critical section flag[0] = false; flag[1] = false; • Deadlocks if processes are in lockstep *** Explain how !! ***

  10. Software Approaches • Busy Flag Again (figure 5.2d) P0 P1 flag[0] = true; flag[1] = true; while ( flag[1] ) while ( flag[0] ) { { flag[0] = false; flag[1] = false; delay delay flag[0] = true; flag[1] = true; } } critical section critical section flag[0] = false; flag[1] = false; -- Defer if other process wants the CS • Livelock if processes are in lockstep

  11. Software Approaches • Dekker’s Algorithm (figure 5.3a) • Use flags for mutual exclusion, turn variable to break deadlock • Handles mutual exclusion, deadlock, and starvation

  12. Software Approaches • Peterson’s Algorithm (figure 5.3b) P0 P1 flag[0] = true; flag[1] = true; turn = 1; turn = 0; while ( flag[1] && while ( flag[0] && turn == 1 ) /**/; turn == 0 ) /**/; critical section critical section flag[0] = false; flag[1] = false; • What if we have more than two processes? -- A process must check everyone else’s flag

  13. Hardware Support • Disable Interrupts • Only works for uniprocessors • Delays response to external events • Special Instructions • Must appear as a indivisible unit to other processors • Test and Set – If V is 0, set to 1 and return true, else return false • May let user specify test/set values • Exchange – Swap reg and memory • Example uses: figure 5.5a, b • Properties • Simple • One variable per critical section • Often use busy waiting • Possibility of deadlock or starvation

  14. Semaphores • First defined by Dijkstra • Semaphore is an integer with a non-negative initial value • Two atomic operations are defined: • Wait (S) S --; if S<0 then block the process else continue • Signal (S) S++; if S <0, wake one blocked process

  15. Binay Semaphore • Can only take on values 0 and 1. • Initial value is 1 • Ideal for implementing Critical sections S: Binare semaphore:= 1; Process p1 Process p2 Wait (S) Wait (S) -- CS -- -- CS -- Signal (S) Signal (S)

  16. Semaphores • Provide a very useful form of mutual exclusion • Initialize semaphore s to 1 do forever: wait(s) critical section signal(s) • Implementing Wait/Signal • Hardware primitives (Fig. 5.17, pg 229) • Busy wait only occurs inside wait/signal • Software methods • Semaphore internals are the “critical section” for the low-level methods • Generally these are short enough to be acceptable • Meaning of count: • if count ³ 0 : how many processes can call wait() without blocking • if count < 0 : how many processes are currently blocked on this semaphore

  17. Producers & Consumers • One process produces some type of data • The other process consumes that data • Data stored in a shared buffer (infinite size) • Require mutual exclusion to access buffer -- A Full buffer must not be overwritten! -- An empty buffer must not be consumed!!

  18. Producers & Consumers s: bin sem, delay: sem, n=0; • Producer do forever produce item wait(s) append to queue n++ if n = 1 then signal(delay) signal(s) • Consumer wait(delay) do forever wait(s) remove from queue n-- m = n signal(s) if m = 0 then wait(delay)

More Related