COBOL Manual for TNS/E Programs (H06.08+, J06.03+)
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.
To avoid deadlock when processes share and lock files, have each process lock and unlock records
and files in the same sequence.
NOTE: If a process is protected by TMF, all locks are retained (despite unlocking statements)
until the current transaction is completed or backed out.
Setting Time Limits on Input-Output Operations
Even when each program that shares files carefully arranges to lock files and records in the same
order, a newly introduced program or a change to an existing program can fail to conform and
cause deadlock. In HP COBOL, you can prevent this deadlock by using the TIME LIMITS phrase
with the statements OPEN, START, READ, and LOCKFILE.
Without the TIME LIMITS phrase, if your process attempts to open a file, establish a starting position
in a file, read a file, or lock a file and that file or the necessary record in that file is already locked,
the process suspends activity until the lock is removed.
With the TIME LIMITS phrase, you can specify a time limit on the suspension. If the time expires
before the inhibiting lock is removed, the file operation terminates with file status code “30” and
GUARDIAN-ERR special register value 40. The program can diagnose the failure of the file operation
and either retry the operation or abandon the current group of operations to allow another process
to complete its operations.
Suppose you have a file declared by these entries:
SELECT OAK-FILE ASSIGN TO "$OAK.ACORN.TREE"
ORGANIZATION IS SEQUENTIAL
ACCESS MODE IS SEQUENTIAL
FILE STATUS IS OAK-STATUS.
FD OAK-FILE
LABEL RECORDS ARE OMITTED.
01 OAK-RECORD.
02 ...
Suppose that you open the file with this statement:
OPEN INPUT OAK-FILE WITH TIME LIMITS SHARED.
Suppose that your program includes a declaratives section such as:
DECLARATIVES.
OAK-FILE-USE-SECTION.
USE AFTER STANDARD ERROR PROCEDURE ON OAK-FILE.
OAK-DECL.
IF OAK-STATUS = "30"
AND GUARDIAN-ERR = 40
MOVE "Operation timed out on OAK file" TO REPORT
MOVE TIME-OUT-CODE TO OAK-ERROR
DISPLAY REPORT
END-IF.
END DECLARATIVES.
Suppose that you attempt to read the file OAK with a statement such as:
READ OAK-FILE RECORD TIME LIMIT 5 AT END PERFORM END-OAK.
IF OAK-ERROR = TIME-OUT-CODE
...
882 Disk Input and Output










