Guardian Programmer's Guide

Table Of Contents
Coordinating Concurrent File Access
Guardian Programmer’s Guide 421922-014
3 - 9
Avoiding Multiple-Process Deadlocks
By making sure that each process acquires its locks in the same order, you ensure that
no deadlock can occur. Here, process B waits when it tries to lock file 1. Process A is
then able to get both the locks it needs to continue. Process A eventually releases its
locks, allowing process B to continue.
Note that more than two processes and two files may be involved in this kind of
deadlock situation. Process A may wait for process B, which waits for process C,
which waits for process D, which waits for process A. The solution is the same.
Always acquire the locks in the same order in each process.
Figure 3-3. Avoiding the Two-Process Deadlock
VST017.VSD
Process A Process B
CALL LOCKFILE (FILE1);
CALL LOCKFILE (FILE);
CALL UNLOCKFILE (FILE1);
CALL UNLOCKFILE (FILE2);
CALL LOCKFILE (FILE1);
wait
CALL LOCKFILE (FILE2);
wait
Process B waits for
Process A to
release lock on
FILE1
Process A releases
flock on FILE!, so
Process B blocks
FILE1
Process B waits for
Process A to release
lock on FILE2
Process A releases
flock on FILE2, so
Process B blocks
FILE2