Date: Tue, 16 Dec 1997 12:07:25 +0200 (IST) From: Eli Zaretskii To: Vik Heyndrickx cc: djgpp AT delorie DOT com, DJ Delorie Subject: Re: Q?on sharing/locking files In-Reply-To: <349512F7.617F@rug.ac.be> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Precedence: bulk On Mon, 15 Dec 1997, Vik Heyndrickx wrote: > Can I expect that when I manage to open a file for writing with fopen, > that writing is inhibited by all other accesses to this file? AFAIK, it's on OS- and compiler-dependent mess. On Unix, for example, several processes can happily write to the same file with interesting results. On DOS and MS-Windows it depends on whether some kind of file-sharing (SHARE or VSHARE) is or isn't loaded, and what kind of sharing did you specify when you open a file. The default for DJGPP is to open files in Compatibility mode. According to my references, this means that any other DJGPP program should have been able to open the same file and write all over it with no problems. However, Windows 95 disallows any attempts to open the file in either write or append mode (which is quite wise, IMHO). So even documented behavior is not always observed (or maybe I'm missing something). > Or should I lock the file, but how do I do this portably? First, I'd suggest to try hard to stay away of this problem if you can, since even locking won't always solve possible conflicts (see below). If the problem in point can be solved without requiring simultaneous file access, try that approach first. If you do need to lock, use the functions `lock' or `_dos_lock' in the library (there are also corresponding unlock functions). They aren't portable, but you could easily write a wrapper around them to have Unix-compatible `flock' and `lockf', which have similar functionality, but slightly different interface. (Btw, DJ, why don't we have `lockf'? My references seem to imply that it is POSIX. Isn't it?) You should know that on Unix also, locking files cannot ensure that two applications won't write to the same file. For starters, locking doesn't work reliably across networks, so any file in a volume mounted over NFS can be scrogged by two different programs. Also, locking on Unix is ``advisory'': it requires that all the programs that access a file use calls to `lockf' to see which parts of it are locked, so it's only good enough for cooperative programs. That is why every application that is serious about this issue implements its own locking mechanism (Emacs is one example). > The same question for open. `fopen' just calls `open', so the same considerations apply. The only difference is that with `open' you can enforce specific DOS sharing bits, whereas with `fopen' you can't. > Does the ANSI/POSIX standard enforce here something? Does DJGPP comply? AFAIK, ANSI doesn't say anything about these issues. DJ?