Guardian Programmer's Guide

Table Of Contents
Coordinating Concurrent File Access
Guardian Programmer’s Guide 421922-014
3 - 6
Locking a File
ERROR := FILE_OPEN_(FILENAME2:LENGTH2,FILENUM2,
READ^ONLY,
PROTECTED^ACCESS);
IF ERROR <> 0 THEN ...
ERROR := FILE_OPEN_(FILENAME3:LENGTH3,
FILENUM3,
!access!,
EXCLUSIVE^ACCESS);
IF ERROR <> 0 THEN ...
The first FILE_OPEN_ call uses the default values to open FILENAME1 for reading
and writing with shared exclusion mode. The second call opens FILENAME2 for read-
only access with protected exclusion mode. The last call opens FILENAME3 for
reading and writing (by default) but with exclusive access.
If the open cannot proceed due to an exclusion mode held by another process, then
the FILE_OPEN_ procedure returns an error.
Locking a File
So far this guide has discussed methods of exclusion that are applied when a file is
opened. You can apply a temporary exclusion to a file by locking it with the LOCKFILE
procedure. While you have the file locked, no other process can access any part of the
locked file (with one exception described later in this subsection). The lock can be
removed using the UNLOCKFILE procedure:
CALL LOCKFILE(FILENUM);
.
.
.
CALL UNLOCKFILE(FILENUM);
If your process tries to lock a file that is locked by another process, your call to
LOCKFILE does not finish until the other process unlocks the file. If your process tries
to write to a locked file, the write operation fails and an error code is returned. If your
process tries to read from a locked file, the read operation does not finish until the
other process unlocks the file.
You can use function 4 of the SETMODE procedure to change the processing that
occurs when you try to lock or read from a file that is locked by another process.
SETMODE function 4 allows several such options. For example:
You can specify that lock and read operations on files that are locked by another
process should finish immediately and return with an error indication:
LITERAL SET^LOCK^MODE = 4,
REJECT^MODE = 1;
.
.
CALL SETMODE(FILENUM,
SET^LOCK^MODE,
REJECT^MODE);