Sharing a file among two or more processes may be hazardous. When a file is
being shared among two or more processes and is being written to by one or more
of them, care must be taken to ensure that the processes are properly
interlocked. For example, if process A is trying to read a particular record
of the file, and at that time process B should execute and try to write that
record, the results are not predictable. process A may see the old record or
the new record, and not know whether it has read good data. If buffering is
being done, please bear in mind that an output request (FWRITE) does not cause
physical I/O to occur until a block is filled, which typically contains
several records. A process trying to read such a file could, for example, read
past the last record of the file which has been written on the disk because the
end-of-file pointer is not kept in the file, but is kept in core where it can
be updated quickly as writes occur. The necessary interlocking is provided by
the intrinsics FLOCK and FUNLOCK, which use a resource identification number
(RIN) as a flag to interlock multiple accessors.
In the simple case of a file shared between a writer process and a reader
process, where the writer is merely adding records to the file, the writer
calls FLOCK prior to writing each record and FUNLOCK after writing. The reader
calls FLOCK prior to reading each record, and FUNLOCK after reading. If the
writing process should execute while the reader is in the middle of a read, the
writer will be impeded on its FLOCK call until the reader signals that it is
done by calling FUNLOCK. Similarly, if the reader should execute while the
writer is performing a write, the reader will be impeded on its FLOCK call
until the writer calls FUNLOCK. FUNLOCK ensures that all buffers are posted on
the disk so that the reading processes can see all of the data.
More complicated cases arise when a file has two or more writing processes, or
when the writer may write more than one record at a time. If, for example, it
should be necessary to write pairs of records, with read prohibited until both
records of the pair are written, the writing process can call FLOCK before
writing the first record of the pair, and FUNLOCK after writing the second.
The shared file management scheme that MPE/iX provides you through the use of the
FLOCK and FUNLOCK intrinsics guarantees you exclusive access to a file being
shared by a set of processes that may be located in different jobs or sessions.
MPE/iX also provides you with RINs you can use to manage anything you may
consider a resource to your program, be it portions of a file, a device, or a
segment of code in your program. Managing shared resources with RINs is
described in Resource Management Programmer's Guide (32650-90024).
For more information about the FLOCK and FUNLOCK intrinsics, consult the MPE/iX
Intrinsics Reference Manual (32650-90028).