[comp.unix.questions] System V file/record locking?

prc@maxim.ERBE.SE (Robert Claeson) (02/02/89)

I was reading through the UNIX System V Programmer's Reference the other
night (excellent reading if you can't sleep -- it never fails :-). While
reading about file and record locking, the following questions appeared,
and I couldn't find any good answers for them in my manuals. If there's
someone who knows, please e-mail me.

1. What's the difference between the lockf(3) function and the locking
   calls to the fcntl(2) system call? Since lockf is a function in the
   standard library, one would expect it to be implemented using fcntl.
   If this is not so, does the NFS locking protocol work on fcntl-style
   locks as well (it works on lockf locks)?

2. lockf always uses "exclusive" locks. Does this mean that it sets
   a write lock on the file segment?

3. What is the "l_whence" field used for in the flock struct (used as
   argument to the locking functions of fcntl)? The same thing as in
   the lseek system call?

4. The documentation mentions a "F_CHKFL" function of fcntl in the man
   page for fcntl(5). What does this function do and how do I use it?
-- 
Robert Claeson, ERBE DATA AB, P.O. Box 77, S-175 22 Jarfalla, Sweden
"No problems." -- Alf
Tel: +46 758-202 50  EUnet:    rclaeson@ERBE.SE  uucp:   uunet!erbe.se!rclaeson
Fax: +46 758-197 20  Internet: rclaeson@ERBE.SE  BITNET: rclaeson@ERBE.SE

jcm@mtunb.ATT.COM (was-John McMillan) (02/03/89)

In article <477@maxim.ERBE.SE> prc@maxim.ERBE.SE (Robert Claeson) writes:
...
>1. What's the difference between the lockf(3) function and the locking
>   calls to the fcntl(2) system call?  Since lockf is a function in the
>   standard library, one would expect it to be implemented using fcntl.

	LOCKF, indeed, uses FCNTL.  It just saves your management
	of the 'flock' struct, and generalizes the error returns
	(into EDEADLK).

>2. lockf always uses "exclusive" locks. Does this mean that it sets
>   a write lock on the file segment?
	
	It always uses F_WRLCK.

>3. What is the "l_whence" field used for in the flock struct (used as
>   argument to the locking functions of fcntl)?  The same thing as in
>   the lseek system call?

	Yes.

>4. The documentation mentions a "F_CHKFL" function of fcntl in the man
>   page for fcntl(5). What does this function do and how do I use it?

	(^; NOMEX ON ;^)  I believe it's an abstraction of
	FSS which is un-utilized in fs/s5.  Ie., it's a hook for
	FUTURE features, future types of alien-file-system <-> UNIX
	manglings.  ...But then, even I don't trust MY beliefs....
	
jc mcmillan	-- att!mtunb!jcm	-- Speaking for 'self, not THEM.

hutch@lzaz.ATT.COM (R.HUTCHISON) (02/08/89)

From article <477@maxim.ERBE.SE>, by prc@maxim.ERBE.SE (Robert Claeson):
...
} 1. What's the difference between the lockf(3) function and the locking
}    calls to the fcntl(2) system call? Since lockf is a function in the
}    standard library, one would expect it to be implemented using fcntl.
}    If this is not so, does the NFS locking protocol work on fcntl-style
}    locks as well (it works on lockf locks)?

Yes, lockf() uses fcntl().  I believe it was added at the prompting of
/usr/group.  They wanted a standard way of locking files.
} 
} 2. lockf always uses "exclusive" locks. Does this mean that it sets
}    a write lock on the file segment?

Yes.  It's a shame that it doesn't let you place read (non-exclusive) 
locks.

} 
} 3. What is the "l_whence" field used for in the flock struct (used as
}    argument to the locking functions of fcntl)? The same thing as in
}    the lseek system call?

Yes.  Same rules apply as for lseek().

} 
} 4. The documentation mentions a "F_CHKFL" function of fcntl in the man
}    page for fcntl(5). What does this function do and how do I use it?

It doesn't do anything although the header file hints that it might.
Perhaps in file systems other than system V it might be used.

} -- 
} Robert Claeson, ERBE DATA AB, P.O. Box 77, S-175 22 Jarfalla, Sweden
} "No problems." -- Alf
} Tel: +46 758-202 50  EUnet:    rclaeson@ERBE.SE  uucp:   uunet!erbe.se!rclaeson
} Fax: +46 758-197 20  Internet: rclaeson@ERBE.SE  BITNET: rclaeson@ERBE.SE

I'm surprised that there weren't any questions on mandatory locking.

Bob Hutchison
att!lzaz!hutch