dining philosophers problem
sleeping barbers problem
gridlock: (automobile) traffic version of deadlock
TODO: diagram
deadlock: mutual dependency
in practice: OS lets user deal with deadlock
prevention
detection & recovery
OS may not avoid deadlock, but your system should -- if possible
4 conditions
2-color graph: resources & processes
TODO: diagram
graph algorithm: find loops (cycles)
avoidance: attack one of the four conditions
detection & recovery
multiple instances of similar resources (resource classes)
each process must declare (in advance) its maximum resource requirements
resource requests will be granted only if the algorithm can demonstrate that there exists some sequence such that all processes can successfully acquire & release resources
M = # resource classes
N = # processes
Available[M] = # resources available for each class (0..M-1)
Max[N,M] = maximum demand for each resource, for each process
Allocation[N,M] = current allocation of each resource, for each prosess
Need[N,M] = Max - Allocation
# each row is a vector with one element for each resource class
define X < Y iff for all i in [0..M-1], X[i] < Y[i]
X < Y
is a partial ordering. It is posible for some X ≠ Y
that neither X < Y
nor Y < X
# Safety algorithm (lemma? subroutine?)
Work[M]
Finish[N]
Work = Available # currently available resources
Finish = False[N]
loop:
find process i such that its maximum request could be satisfied (i.e. !Finish[i] & Need[i] < Work)
if no such i:
system is safe iff Finish == True[N]
return
# assume process i gets it's max request and is allowed to run to completion
# then it's currently-held resources become available
Work += Allocation[i]
Request[i][M] = request vector of process i
if Request[i] > Need[i]
raise exception
if Request[i] > Available
wait
if system would be unsafe after allocation
wait
otherwise
grant requested resource
Example:
resources: A = 10, B = 5, C = 7
current Available = (3, 3, 2)
max current need (max - current)
P0 (7, 5, 3) (0, 1, 0) (7, 4, 3)
P1 (3, 2, 2) (2, 0, 0) (1, 2, 2)
P2 (9, 0, 2) (3, 0, 2) (6, 0, 0)
P3 (2, 2, 2) (2, 1, 1) (0, 1, 1)
P4 (4, 3, 3) (0, 0, 2) (4, 3, 1)
<P1, P3, P4, P0, P2>
satisfies safety
P1
's maximal request could be satisfied immediatelydevices
access method
direction
disk drive: mechanical device
i
, write block j
working with raw devices: inconvienient
file abstraction
tmpfs
)disk is shared by multiple users: require permissions & ownership
file location
format
file type: "extension"
magic number: fast identifer check on file format
Unix file
command: heuristic
open
read
write
close
tell
seek
ioctl
fcntl
Unix-like (Posix)
fd = open(fname, O_RDONLY)
fd = open(fname, O_WRONLY, S_IRUSR|S_IWUSR)
n = read(fd, array_buf, n_read)
n = write(fd, array_buf, n_write)
close(fd)