[net.arch] File Locking

mwm@ucbtopaz.CC.Berkeley.ARPA (03/09/85)

>> As a miniumum, by default if one process has a file open with write 
>> permission no other process should be able to read or write that file.
>Of course, this means that (for example) you can't read a log file as
>it is being produced to watch what's happening.

[Ok, I give up. I'll post anyway.]

The slickest file locking system I've seen works like so:

open for read: no locks whatsoever are created.

open for append: this include open for write, with the file being truncated
beforehand. Processes trying to read past the end of the file block,
waiting for you to either write enough bytes for the read to return, or for
you to close the file, which returns the typical short record or EOF.

open for modify: When you do a read of n bytes, those n bytes (and only
those bytes) are locked until you either do another read, or close the
file. Note that this works *per file*, not per file descriptor, so you can
block on yourself (a one-process deadlock?), like so:

	fd1 = open("file", "rw") ;
	fd2 = open("file", "r") ;
	read(fd1, 10, buffer1) ;
	read(fd2, 20, buffer2) ;		/* blocks */

This is the (v6) unix philosophy: you get very sharp tools, and it's your
problem to avoid cutting yourself.

I don't have manuals, but I believe there is also a per-file locking
mechanism of some kind.

In general, OS-9 does a large number of things right (file names are
created with the case given in the call, but open is case insensitive when
looking for files), and is well worth a look if you're interested in OS's.

	<mike