File Locking in COBOL COBOL

In runtime environments in which more than one run unit can be running concurrently, the possibility arises that one run unit must deny concurrent access to a file or set of files by other run units. This is accomplished through file locking. There are several methods provided in RM/COBOL to specify file locking.

The LOCK MODE clause in the file control entry may specify the EXCLUSIVE phrase. The LOCK MODE IS EXCLUSIVE clause causes each OPEN statement to open the file in exclusive mode.

The EXCLUSIVE phrase may be specified in the OPEN statement.This causes the OPEN statement to open each file in exclusive mode.

The LOCK phrase may be specified for an individual file in the OPEN statement. This causes the OPEN statement to open that file in exclusive mode.

When the LOCK MODE clause is not specified for a file and the OPEN statement does not specify the EXCLUSIVE or WITH LOCK phrases, whether the OPEN statement opens the file in exclusive or shared mode depends on the environment supporting the file and a configurable default.

A file may be opened in the input mode as a shared or exclusive file when the same file is open only in the shared or exclusive input mode by other run units. The exclusive input mode prevents other run units only from concurrent updates of the file, not from concurrent reading of the file.

When an attempt is made

  • to open a file for which some other run unit has the same file open in exclusive extend, exclusive input-output, or exclusive output mode,
  • to open a file in input-output or extend mode for which some other run unit has the same file open in exclusive input mode,
  • to open a file in exclusive extend or exclusive input-output mode for which some other run unit has the same file open in any mode, or
  • to open a file in exclusive input mode for which some other run unit has the same file open in extend, input-output or output mode the OPEN statement is unsuccessful. The file status data item, if there is one, is set to a value indicating this condition and any applicable USE procedure for the file is executed.

Regardless of lock mode, a file that is open in any mode by another run unit cannot be opened in the output mode.

In shared file environments, opening a file in exclusive mode can improve performance of the other input-output statements executed while the file is open. This is because exclusive mode guarantees that no other run unit will update the file while this run unit has the file open. Therefore, physical records can be locally buffered by the run unit when the file is open in exclusive mode, resulting in lower operating system overhead. For files open in the input-output mode, operating system overhead is further reduced since record locking operations are not necessary.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

COBOL Topics