COBOL Manual for TNS and TNS/R Programs

Disk Input and Output
HP COBOL Manual for TNS and TNS/R Programs522555-006
28-38
Avoiding Deadlock
Avoiding Deadlock
Deadlock is a situation in which two processes are in contention for a single-user
resource. If both processes require exclusive use of both resources, they are
deadlocked. Neither can proceed until the other surrenders control of the resource it
has under its control. If two servers get into a deadlock, their respective requesters are
also deadlocked because neither requester receives a reply from its server.
One example of deadlock is when one process has opened a file (call it ABLE) and
needs to open another file (call it BAKER), while another process has opened BAKER
and now needs to open ABLE.
Another example of deadlock is when one process has locked a record in a file (call it
record n ) and wants to read another record in the file (call it record m ), while some
other process has locked record m and wants to read record n.
The ways to avoid deadlock are:
Locking and Unlocking Files and Records
Setting Time Limits on Input-Output Operations
Locking and Unlocking Files and Records
The most complete way for a process to lock a file is to open the file for exclusive
access (open it with the exclusion mode EXCLUSIVE). This strategy prevents all other
processes from reading or writing the file.
If it is not feasible for one process to have exclusive control of a file for when it has the
file open, there are alternatives.
One alternative is protected access. When a process opens a file for protected access
(opens it with the exclusion mode PROTECTED), other processes can read and write
the file while the opening process has it open, but cannot write the file until the opening
process closes the file.
Another alternative is shared access. When a process opens a file for shared access
(opens it with the exclusion mode SHARED), other processes can read and write the
file while the opening process has it open.
When a file is open for shared access and one process needs exclusive access to the
file temporarily, that process can lock the file with a LOCKFILE statement, operate on
it, and then unlock it with an UNLOCKFILE statement. If a process only needs
temporary exclusive access to a record of the file, the process can temporarily lock the
record with a READ statement with a LOCK phrase and then unlock it with an
UNLOCKRECORD statement.