Title: Emery Berger
1Operating SystemsCMPSCI 377Lecture 8
Synchronization II
- Emery Berger
- University of Massachusetts Amherst
2Last 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
3This Time Locks Semaphores
- Implementing locks
- Semaphores
4Atomic 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)
5Implementing Locks Test Set
- int testset (int value)
- int old value
- value 1
- return old
pseudo-code red atomic
- Whats the effect of testset(value) when
- value 0?
- value 1?
6Implementing Locks Test Set
class Lock private int value Lock()
value 0 void acquire() void
release()
- int testset (int value)
- int old value
- value 1
- return old
pseudo-code red atomic
- acquire wait until lock released, then take it
- release release lock
void acquire() while (testset(value))
void release() value 0
7Busy Waiting (Spinning)
- Whats wrong with this implementation?
- CPU utilization?
- Different priorities?
void acquire() while (testset(value))
void release() value 0
spin-lock
8Minimizing Busy Waiting
- Cant 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
9Locks 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?
10This Time Locks Semaphores
- Implementing locks
- Semaphores
11Brief SegueEdsger W. Dijkstra (1930-2002)
- Predicate transformers
- On-the-fly garbage collection
- Self-stabilizing systems
- Mutual exclusion problem
- Deadly embrace(now called deadlock)
- Semaphores
- Shortest Path
- First modern compiler (Algol-60)
- Stack for implementing recursion, vector
- First modular OS (THE)
12Semaphores
- Whats 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.
13Semaphores
- Whats a semaphore anyway?
A visual signaling apparatus with flags, lights,
or mechanically moving arms, as one used on a
railroad.
14Semaphores
- Whats 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
15Semaphores 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
16Semaphores 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!"
17Implementing Semaphores
class Semaphore private int value private
Queue q Semaphore(int v) value v
void wait() if (value gt 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)
18Variants 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
19Binary Semaphores Example
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)
20Binary 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()
21Counting 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()
22Summary
- Implementing locks
- Test Set
- Spin locks, blocking locks
- Semaphores
- Generalization of locks
- Binary, Dijkstra-style
- Useful for
- Controlling resources
- Waiting on events
23Next Time
- Synchronization III!
- Monitors