Operation Notes |
 |
In order to call CKLOCK, the file must be opened with dynamic access enabled.
This can be done only with the CKOPENSHR procedure. CKOPEN will not open the file for shared access with
dynamic locking.
When users are sharing a file, it is essential to lock the
file before modifying it. An error is returned if any user attempts
to write, rewrite, or delete records without first locking the file.
It is also important to avoid situations where one user locks the
file and forgets to unlock it. If the file is already locked when
you call CKLOCK with lockcond set to zero, the call will fail with 30 returned
to status, and your process will continue. If, however, lockcond is set to 1, your process suspends until the other
user unlocks the file or logs off.
The following example opens file KSAMFILE for shared access with dynamic locking allowed.
It then locks the file unconditionally. If another user has locked
the file, the process suspends until the file is unlocked and then
continues by locking your file. The status value is checked as soon
as control returns to your process to ensure that the file has been
locked before continuing.
DATA DIVISION.
77 LOCKCOND PICTURE S9(4) COMP VALUE 1.
77 RESULT PICTURE 9(4) VALUE 0.
01 STATUSKEY.
02 STATUS-KEY1 PICTURE X VALUE " ".
02 STATUS-KEY2 PICTURE X VALUE " ".
01 FILETABLE.
02 FILENUMBER PICTURE S9(4) COMP VALUE 0.
02 FILENAME PICTURE X(8) VALUE "KSAMFILE".
02 I-O-TYPE PICTURE S9(4) COMP VALUE 0.
02 A-MODE PICTURE S9(4) COMP VALUE 0.
02 PREV-OP PICTURE S9(4) COMP VALUE 0.
PROCEDURE DIVISION.
START.
CALL "CKOPENSHR" USING FILETABLE, STATUSKEY.
IF STATUS-KEY1 = "0" THEN GO TO LOCK-FILE.
IF STATUS-KEY1 = "9" THEN
CALL "CKERROR" USING STATUSKEY, RESULT
DISPLAY "ERROR NO. ",RESULT.
LOCK-FILE.
CALL "CKLOCK" USING FILETABLE, STATUSKEY, LOCKCOND.
IF STATUSKEY="00"
THEN DISPLAY "CKLOCK IS OK"
ELSE IF STATUSKEY = "30"
THEN DISPLAY "FILE LOCKED BY ANOTHER PROCESS"
ELSE IF STATUS-KEY1="9"
THEN CALL "CKERROR" USING STATUSKEY, RESULT
DISPLAY "ERROR NO.", RESULT.