1 / 23

Operating Systems CMPSCI 377 Lecture 8: Synchronization II

Operating Systems CMPSCI 377 Lecture 8: Synchronization II. Emery Berger University of Massachusetts Amherst. Last Time: Synchronization. Threads communicate to ensure consistency If not: race condition (non-deterministic result) Accomplished by synchronization operations

allendale
Download Presentation

Operating Systems CMPSCI 377 Lecture 8: Synchronization II

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. Operating SystemsCMPSCI 377Lecture 8: Synchronization II Emery Berger University of Massachusetts Amherst

  2. Last Time: Synchronization • Threads communicate to ensure consistency • If not: race condition(non-deterministic result) • Accomplished by synchronization operations • How to write concurrent code • How to implement synchronization operations

  3. This Time: Locks & Semaphores • Implementing locks • Semaphores

  4. Atomic Read-Write-Modify Instructions • Atomicallyread old value,write new value • SMP invalidates copies in other caches • Cache coherence support • Examples: • Test & Set (most arch) • Exchange (x86) • Compare & Swap (68K, Sparc)

  5. Implementing Locks: Test & Set int testset (int value) { int old = value; value = 1; return old; } pseudo-code: red = atomic • What’s the effect of testset(value) when: • value = 0? • value = 1?

  6. Implementing Locks: Test & Set int testset (int value) { int old = value; value = 1; return old; } class Lock { private int value; Lock() { value = 0; } void acquire() {…} void release() {…} } pseudo-code: red = atomic • acquire – wait until lock released, then take it • release – release lock void acquire() { while (testset(value)) {} } void release() { value = 0; }

  7. Busy Waiting (“Spinning”) • What’s wrong with this implementation? • CPU utilization? • Different priorities? void acquire() { while (testset(value)) {} } void release() { value = 0; } spin-lock

  8. Minimizing Busy Waiting • Can’t implement locks with test & set without any waiting (w/o disabling interrupts) • Add queue to lock and sleep: blocking lock void acquire() { while (1) { if (testset(value)) { put thread on queue, sleep } else { break; } } } void release() { value = 0; wake up threads; }

  9. Locks as Synch Operations • Locks provide mutual exclusion • Only one thread enters section at a time • Simplifies writing concurrent programs • Low-level • Can complicate coding • e.g., “allow at most n threads to access resource” • What are some alternatives?

  10. This Time: Locks & Semaphores • Implementing locks • Semaphores

  11. Brief Segue:Edsger W. Dijkstra (1930-2002) • Shortest Path • First modern compiler (Algol-60) • Stack for implementing recursion, vector • First modular OS (THE) • Predicate transformers • On-the-fly garbage collection • Self-stabilizing systems • Mutual exclusionproblem: • “Deadly embrace”(now called deadlock) • Semaphores

  12. Semaphores • What’s a “semaphore” anyway? A visual system for sending information by means of two flags that are held one in each hand, using an alphabetic code based on the position of the signaler's arms.

  13. Semaphores • What’s a “semaphore” anyway? A visual signaling apparatus with flags, lights, or mechanically moving arms, as one used on a railroad.

  14. Semaphores • What’s a “semaphore” anyway? A visual signaling apparatus with flags, lights, or mechanically moving arms, as one used on a railroad. • Intuitively: regulates traffic at critical section

  15. Semaphores in CS • Computer science: Dijkstra (1965) A non-negative integer counter with atomic increment & decrement.  Blocks rather than going negative. • Higher-level than locks but not too high level

  16. Semaphores: Key Concepts • P(sem),a.k.a. wait = decrement counter • If sem = 0, block until greater than zero • P = “prolagen” (proberen te verlagen, “try to decrease”) • V(sem), a.k.a. signal = increment counter • Wake 1 waiting process • V = “verhogen” (“increase”) In Holland the good Dr. Dijkstra Took a break from a walk on his bijkstra And said: "Which shall it be? Take a P or a V? For the two seem to me quite alijkstra!"

  17. Implementing Semaphores class Semaphore { private int value; private Queue q; Semaphore(int v) { value = v; } } void wait() { if (value > 0) { value = value – 1; } if (value == 0) { add this process to Q; sleep(); } } void signal() { value = value + 1; if (anyone on Q) { remove P from Q; wakeup(P); } }

  18. Variants of Semaphores • Binary semaphore • just two values (0 or 1), initially 1 (“free”) • Counting semaphore • useful when units of resource are available • initialized to number of resources • thread can access as long as one unit available

  19. Binary Semaphores: Example • “too much milk” with locks thread A Lock.acquire() if (no milk) buy milk Lock.release() thread B Lock.acquire() if (no milk) buy milk Lock.release() thread A sem.wait() if (no milk) buy milk sem.signal() thread B sem.wait() if (no milk) buy milk sem.signal() • “too much milk” with binary semaphores(initially 1)

  20. Binary Semaphore: Example • More flexible than locks! • By initializing semaphore to 0,threads can wait for an event to occur thread A // wait for thread B sem.wait(); // do stuff … thread B // do stuff, then // wake up A sem.signal();

  21. Counting Semaphores: Example • Controlling resources: • Allow threads to use at most 5 files simultaneously • Initialize to 5 thread A sem.wait(); // use a file sem.signal(); thread B sem.wait(); // use a file sem.signal();

  22. Summary • Implementing locks • Test & Set • Spin locks, blocking locks • Semaphores • Generalization of locks • Binary, “Dijkstra-style” • Useful for: • Controlling resources • Waiting on events

  23. Next Time • Synchronization III! • Monitors

More Related