Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!uwm.edu!rpi!dali.cs.montana.edu!uakari.primate.wisc.edu!samsung!umich!umeecs!msi-s0.msi.umn.edu!cs.umn.edu!uc!nic.MR.NET!ns!dmb From: dmb@ns.network.com (Duane M. Butler) Newsgroups: comp.os.msdos.programmer Subject: Implementation of File Locking Message-ID: <1990Aug3.182050.8793@ns.network.com> Date: 3 Aug 90 18:20:50 GMT Sender: news@ns.network.com Organization: Network Systems Corp., Mpls., MN Lines: 33 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