Presentation is loading. Please wait.

Presentation is loading. Please wait.

U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts Amherst Operating Systems CMPSCI 377 Lecture.

Similar presentations


Presentation on theme: "U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts Amherst Operating Systems CMPSCI 377 Lecture."— Presentation transcript:

1 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts Amherst Operating Systems CMPSCI 377 Lecture 8: Synchronization II

2 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 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 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 3 This Time: Locks & Semaphores Implementing locks Semaphores

4 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 4 Atomic Read-Write-Modify Instructions Atomically read 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 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 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 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 6 Implementing Locks: Test & Set int testset (int value) { int old = value; value = 1; return old; } pseudo-code: red = atomic class Lock { private int value; Lock() { value = 0; } void acquire() {…} void release() {…} } void acquire() { while (testset(value)) {} } void release() { value = 0; } acquire – wait until lock released, then take it release – release lock

7 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 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 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 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 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 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 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 10 This Time: Locks & Semaphores Implementing locks Semaphores

11 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 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 exclusion problem: “Deadly embrace” (now called deadlock) Semaphores

12 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 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 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 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 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 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 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 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 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 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”) 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!" V(sem), a.k.a. signal = increment counter Wake 1 waiting process V = “verhogen” (“increase”)

17 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 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 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 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 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 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 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 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 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 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 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 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 U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science 23 Next Time Synchronization III! Monitors


Download ppt "U NIVERSITY OF M ASSACHUSETTS, A MHERST Department of Computer Science Emery Berger University of Massachusetts Amherst Operating Systems CMPSCI 377 Lecture."

Similar presentations


Ads by Google