[comp.os.msdos.programmer] Implementation of File Locking

dmb@ns.network.com (Duane M. Butler) (08/04/90)

Hello,

  I have a program (written using Microsoft C 5.1) that manages files in a
given directory.  I want this program to work in network environments where
multiple users can use my program to access files at the same time.  However,
some operations of my program modify control files within the directory.  When
these operations are taking place, I want to restrict all access for the
duration of the operation.  The question is how can other instances of the
program detect when the control files are being modified?  The following
options are not acceptable:

    1) Use the "locking" C run-time function.  This is not acceptable because
       it requires the use of SHARE.EXE.

    2) Buy the network interface kits for the 'popular' network OS's.  This
       is not acceptable because I want to solve this in general, not for a
       specific network OS.  Also, I do this in my "garage" and don't have the
       bucks. :-)

One idea I'm thinking about is to create a temporary lock file in the directory
where the files are.  Each time my program accesses the files in the directory,
it first checks for the existence of this lock file.  If present, it will try
again in 10 seconds or so, and give up after 3 trys.  When the program attempts
to modify the control files, an attempt is made to create a lock file.  What I
don't know is if the create file operation is atomic.  In other words, is it
possible that two instances of the program could believe they created the lock
file at the same time?

If anyone has solved this problem I would appreciate hearing from you.


Duane Butler
EMail: dmb@network.com

cb@sequoia.execu.com (Christopher D. Brown) (08/06/90)

In article <1990Aug3.182050.8793@ns.network.com> dmb@ns.network.com (Duane M. Butler) writes:
[preamble deleted]
>One idea I'm thinking about is to create a temporary lock file in the directory
>where the files are.  Each time my program accesses the files in the directory,
>it first checks for the existence of this lock file.  If present, it will try
>again in 10 seconds or so, and give up after 3 trys.  When the program attempts
>to modify the control files, an attempt is made to create a lock file.  What I
>don't know is if the create file operation is atomic.  In other words, is it
>possible that two instances of the program could believe they created the lock
>file at the same time?
>
>If anyone has solved this problem I would appreciate hearing from you.

PVCS, built by Polytron now from Sage, uses directory creation rather
than file creation to implement semiphors.  I do not know if they had
a reason or viewed the choice as arbitary.

BTW.  As much as I feel that available standards (e.g. MS-DOS locking
via SHARE) ought to be used, in this case your choice is the only
one that yields reliable coverage of a wide variety of environments.

cb
-- 
Christopher D. Brown

Digital: {uunet|texbell|cs.utexas.edu}!execu!cb
Analog: (512) 327-7070
Physical: Execucom, 108 Wild Basin Road, Two Wild Basin, Austin, TX 78764