Emery Berger - PowerPoint PPT Presentation

About This Presentation
Title:

Emery Berger

Description:

Can't implement locks with test & set without any waiting (w/o disabling interrupts) ... Higher-level than locks but not too high level ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 24
Provided by: csUm4
Category:
Tags: berger | emery | locks

less

Transcript and Presenter's Notes

Title: Emery Berger


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
  • 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
Implementing 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?

6
Implementing 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
7
Busy Waiting (Spinning)
  • Whats wrong with this implementation?
  • CPU utilization?
  • Different priorities?

void acquire() while (testset(value))
void release() value 0
spin-lock
8
Minimizing 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
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 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)

12
Semaphores
  • 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.

13
Semaphores
  • Whats a semaphore anyway?

A visual signaling apparatus with flags, lights,
or mechanically moving arms, as one used on a
railroad.
14
Semaphores
  • 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

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 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)
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
thread A Lock.acquire() if (no milk) buy
milk Lock.release()
thread B Lock.acquire() if (no milk) buy
milk Lock.release()
  • too much milk with locks

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
Write a Comment
User Comments (0)
About PowerShow.com