Microsoft KB Archive/66265

Xenix COBOL Incorrectly Locks Files Opened for EXTEND

PSS ID Number: Q66265 Article last modified on 11-15-1990

2.20 XENIX

Summary: The program shown below demonstrates that Microsoft COBOL for XENIX incorrectly handles file locking on SEQUENTIAL files opened for EXTEND. When two or more processes access the same data file and open it for EXTEND, no file status error is returned. While the file is locked by one process, the other process is delayed. But once the file is released, the second process will begin writing at the old end of the file when it was first opened and not at the new end. This information applies to Microsoft COBOL version 2.20 for the XENIX 286 operating system. Microsoft is currently researching this problem and will post new information here as it becomes available.

More Information: Opening a file for EXTEND should open the file and begin writing records at the end of the file. This works properly if only one process is writing to the file. However, when two or more processes are trying to write to the file, records are written to the wrong part of the file. For example, when a file is opened for EXTEND by one process, which begins writing records to it, and then a second process opens the file for EXTEND, the record pointers will be positioned as follows:

+--+ | Data file   | +--+               |                +- file pointer for process 1 |               +- file pointer for process 2 This is correct, both are pointing to the end of the file. However, the second process never generates any file status error (it should generate a file status of 94). The second process will not write any records to the file until the first process terminates, at which point the record pointers will appear as follows:

++ | Data file             | ++               |         |                |         +- file pointer at end of process 1 |               +- file pointer for process 2 The second process will now, incorrectly, begin writing records in the middle of the data file, overwriting the information that the first process wrote. It will think that the end of the file is still in the same place as when it opened the file. The following code example is SAMPLE.CBL, which will demonstrate the problem when run as two concurrent processes. To compile and link the program, enter the following commands: COBOL SAMPLE.CBL; RUNCOB SAMPLE

SAMPLE.CBL
IDENTIFICATION DIVISION. PROGRAM-ID. SAMPLE. ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT OUTPUT-FILE ASSIGN TO DISK ORGANIZATION IS SEQUENTIAL FILE STATUS IS FILE-STAT. DATA DIVISION. FILE SECTION. FD  OUTPUT-FILE LABEL RECORDS ARE STANDARD VALUE OF FILE-ID IS &quot;TEST.DAT&quot;. 01  OUTPUT-REC   PIC XXX. WORKING-STORAGE SECTION. 77  FILE-STAT    PIC XX. PROCEDURE DIVISION. OPEN EXTEND OUTPUT-FILE. MOVE &quot;ABC&quot; TO OUTPUT-REC. PERFORM 10000 TIMES WRITE OUTPUT-REC. Copyright Microsoft Corporation 1990.