[comp.lang.c] BSD ?= SysV File Locking

jrb@petro.UUCP (Jon Boede) (10/16/87)

I have, for some time now, been using the library proceedure lockf() to do
my file locking and have found it a wonderful and elegant mechanism for doing
so (no, really :-)

However, I have only been doing this on SysV machines and now wonder how
portable my code will be to BSD UNIX.

Does BSD use the lockf() convention like SysV?  I've localized functions using
it in hopes that if it didn't I'd only have to fiddle with one area.  If it
doesn't, what does the file locking under BSD 4.3 look like?

PS.  If anyone has a Tandy 6000 (68000 based XENIX 3.1) I have an assembler
patch that fixes the _xlockin[g] dain-bramage in /lib/libc.a and would be happy
to mail it to you if you'd like (it's about 5 lines).

Jon
-- 
Jon Boede	...!{gatech,ihnp4,ssbn,swrinde,tness1,utanes}!petro!jrb
512/599-1847        -^^^^^^-           2555 N.E. Loop 410, #1403, 78217
	"People who are incapable of making decisions are
	 the ones who hit those barrels at freeway exits."

chris@mimsy.UUCP (Chris Torek) (10/24/87)

This is more properly an operating systems question; and I have
redirected followups to comp.unix.questions.

4.2/4.3BSD does not support SysV-style file locking via lockf().
It does, however, support file locking.  lockf() allows locking a
range of bytes within a file, while 4BSD's flock() locks entire
files.  flock() locks are purely advisory:  flock() locks only
against other flock()s.  I do not know the precise details of
lockf() semantics.

The two systems are likely to have source-code compatible locking
once the POSIX standard is adopted.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
Domain:	chris@mimsy.umd.edu	Path:	uunet!mimsy!chris

gwyn@brl-smoke.ARPA (Doug Gwyn ) (10/27/87)

In article <463@petro.UUCP> jrb@petro.UUCP (Jon Boede) writes:
>Does BSD use the lockf() convention like SysV?  I've localized functions using
>it in hopes that if it didn't I'd only have to fiddle with one area.  If it
>doesn't, what does the file locking under BSD 4.3 look like?

flock() was adopted from the 1984 /usr/group Standard and earlier
drafts of IEEE 1003.1.  Later drafts of IEEE 1003.1 (POSIX) have
adopted the more general locking provided by fcntl() on SVR2+ and
later.  Berkeley systems have for some time supplied flock(),
which is not identical to either of the other two.  The lockf()
and flock() approaches can be emulated using fcntl() locking.

It is good that you've localized this function, so that you can
easily adapt it to other file locking kernel interfaces.

The 4.2BSD version:
	#include <sys/file.h>
	/* defines the following advisory lock symbols used to construct
	   an "operation" code for flock(): */
	/* #define LOCK_SH	1	/* shared (read) lock */
	/* #define LOCK_EX	2	/* exclusive (write) lock */
	/* #define LOCK_NB	4	/* don't block when locking */
	/* #define LOCK_UN	8	/* unlock */
	extern int flock(int fd, int operation); /* returns 0 if successful */