lab 1 solution
assignment 1 solution
tour of /proc
midterm
lab 2/ass 2: due Tuesday this week & next
threads vs. processes: depends on the problem
deadlock: mutal dependency
starvation: thread/process does not get its turn
Posix threads (pthreads): low-level interface
critical section: region of code that only one process at a time may execute
goals/requirements for critical section
race condition: incorrect/inconsisten results due to unfortunate timing
semaphore
mutex
condition variable
monitor
Decker's algorithm combines two separate approaches which are individually problematic
modern hardware requires hardware support
semaphore protects a resource
mutex protects code (mutual exclusion)
semaphore = mutex + counter + condition variable
semaphore:
P()
: proben/wait/down (get resource)V()
: verhogen/signal/up (release resource)waiting for resource/condition using semaphore & condition variable:
acquire lock (mutex)
while ! resource
pthread_[timed_]wait(cond_var, lock[, timeout])
critical section
signal cond_var
release lock
alt pattern:
acquire lock
while !resource
wait(cond, lock)
assert ownership of lock
release lock
non-critical code
acquire lock
release resource
signal cond
release lock
philosopher: deadlock
get left chopstick
get right chopstick
eat
release right chopstick
release left chopstick
sounds silly, but is yet another classical illustration of concurrency
barber sleeps until he gets a customer
when customer arrives:
when barber finishes with a customer:
with concurrency, if two customers arrive at the same time, both may try to take the same barber or waiting-room chair
assignment 2
TODO: diagram
prevent girdlock: don't enter intersection unless you can proceed past the intersection
multiple resources (or resource classes: slightly more complex problem)
process/thread acquires & releases exclusive access to resource
informally, a deadlock occurs when a process P1
holds a resource RA
and is waiting on RB
while the process P2
holds RB
and is waiting on RA
P1 <- RA <- P2 <- RB <- P3 <- ... <- RZ
and RZ <- P1
TODO: diagram
dealing with deadlocks: three basic approaches
deadlocks occur only if all of the following conditions are met
TODO: diagram
deadlock avoidance: attack one of the 4 conditions
deadlock detection: construct resource allocation graph & look for cycle
recovery