File and Record Locking


 

When a file is being shared by multiple programs, some means must be employed prevent the programs from interfering with each other. PL/B provides the ability to lock files and records so they cannot be accessed by other programs. These methods are called file locking and record locking. File and record locking may not be active simultaneously. File and record locking is not supported by the PLBCE runtime

 

File Locking

File locking is a method that grants one program exclusive access to entire files and their indexes for a short period.

 

File locking is implemented with a FILEPI statement in PL/B. When a file or group of files is locked with a FILEPI statement, the user is granted exclusive access to that group of files for some number of PL/B statements.

 

The number of statements for which the files are locked is specified in the FILEPI statement. Once the specified number of statements are executed, the files become unlocked automatically. However, not all instructions apply against the count in the FILEPI statement. In addition, untrapped runtime errors and some instructions cancel the file lock. The lock may be programmatically canceled by specifying zero as the operand in the FILEPI statement.

 

When files are locked due to a FILEPI statement, another FILEPI statement may not be executed. This means that the program must lock all of the files that are locked for a particular operation at once. This limitation means that indefinite postponement due to mutual exclusion will never occur in a system that uses file locking.

 

Record Locking

Record locking is a method that provides exclusive access to individual records. This allows multiple users to access a file simultaneously while allowing the individual users to protect the data they are working on from other users.

 

Because users compete for records and not whole files, systems that use record locking will usually have better performance than those that use file locking.

 

Record locking is not limited to short periods of time. A program may lock a record and keep it locked as long as necessary.

 

Record locking is not supported when using SQLIO.

 

Three types of record locking are supported. The appropriate type may be selected using the PLB_RECORDLOCK keyword.

 

Record Locking Modes

Two record locking modes are available with PL/B: manual and automatic. The desired mode is specified when a file is opened.

 

Record Unlocking Modes

Two unlocking modes are available with PL/B: multiple and single. The desired mode is specified when a file is opened.

 

Record Locking Wait Options

If a record is locked by another user and an attempt is made to read or lock it, the time to wait for the record to become unlocked may be specified. The available options are:

Locking in Different Operating Environments

Various operating environments implement locking (file or record) differently. Some systems guarantee locks and others do not. A lock is guaranteed if the record or file cannot be access by another user even if that user does not attempt to lock the record or file. If locks are not guaranteed by the system, a program that updates a file or record without locking it first may interfere with those using locking and may even damage the file.

 

To prevent unexpected results on systems that do not guarantee locks, it is important to ensure that locking strategies are consistent in all programs accessing a file or group of files. Before implementing a locking strategy in some programs and not in others sharing the same file, review the appropriate sections of your operating system documentation to ensure that the results are expected. In addition, such systems should not be considered portable to environments other than the one for which they are designed.

 

Record Locking Linux OS Considerations

By default, the Sunbelt record locking implementation uses the Linux OS write lock (exclusive lock) for each data record locked in a PL/B data file. This insures that only one process can hold a lock for any one specific data record.

 

However, the Linux OS also supports a read lock which allows multiple processes to hold a read/shared lock on the same PL/B data file record at the same time. The Linux PL/B runtime supports a SETMODE *LINUXREADLOCK={0|1} instruction to allow files opened with LOCKAUTO, SINGLE, and NOWAIT parameters to use Linux OS read locks for PL/B read instructions. In this case, only the following PL/B read instructions can apply/use the Linux OS read/shared locks.:

 

File Type

PL/B Reads that support Linux OS read locks

FILE

READ

AFILE

READ, READKG, READKGP

IFILE

READ, READKS, READKP

 

 

See Also: Disk I/O Instructions

 



PL/B Language Reference SHARENF File Access Character String Variables (DISK I/O)