Re: Record Locking with fcntl(): what to do if unlock (F_UNLCK) fails?



Dances With Crows wrote:
On 27 Sep 2006 07:02:56 -0700, denis.papathanasiou@xxxxxxxxx staggered
into the Black Sun and said:
int lock_register(int fd, int type)
{
struct flock fl;

fl.l_type = type;
fl.l_start = fl.l_len = 0;
fl.l_whence = SEEK_SET;
return(fcntl(fd, F_SETLK, &fl));
}
#define place_lock(fd) lock_register((fd), F_WRLCK)
#define remove_lock(fd) lock_register((fd), F_UNLCK)

in the tests done so far, it works exactly as expected. One potential
problem, though, is what to do if the call to remove_lock fails. I'm
concerned about the possibility about creating an unresolvable
deadlock in a case like this.

At that point, remove_lock will return -1, and the calling code should
handle that in whatever manner is appropriate. If you'd like to check
which PID is holding the lock you'd like to release, "man 2 fcntl"
should help, particularly the text near the F_GETLK keyword. WARNING:
This junk may or may not be portable. The basics of fcntl will be, but
specifics might not be. Don't ask me; I haven't messed with fcntl on
non-Linux platforms.

Is invoking a close() on the file descriptor enough to remove the
lock, or do we need to take other action when that occurs?

If process N is holding a lock on file F, having process M close() file
F won't help process M one bit. Fortunately, you can figure out which
process is holding the lock with a few more lines of code. It's not
even insanely difficult--unless there's some sort of subtle trap that
I've missed in the man page--which *could* happen! A bit of short code
made it clear that it seems to work as advertised though. HTH,


Which is why nearly all apps that NEED file and record locking don't use fcntl() but simply vector all applications to the file through a daemon that implements the necessary abstractions in a completely different way.

Cf Mysqld et al.
.



Relevant Pages

  • Re: Record Locking with fcntl(): what to do if unlock (F_UNLCK) fails?
    ... which PID is holding the lock you'd like to release, "man 2 fcntl" ... If process N is holding a lock on file F, ... F won't help process M one bit. ...
    (comp.os.linux.misc)
  • Re: Locking in C++
    ... even if the file is locked by fcntl or lockf, ... > But it's even worse than that; The locking functions provided to obtain ... > a lock operate on file descriptors, ...
    (comp.unix.programmer)
  • Re: Linux, fcntl vs flock and pthreads
    ... First we were using fcntl to do file locking. ... I tried using flock instead and it ... >seconds is needed to get the lock. ...
    (comp.unix.programmer)
  • Re: flock/fcntl
    ... Note also that some versions of "flock" cannot lock things ... provide its own fcntl-based emulation, ... Is there an easy way to obtain the fcntl ...
    (comp.lang.perl.misc)
  • Re: Problem with gcc
    ... Unix should be "no problem" for the file lock problem. ... Use fcntl f.example or lockf (man lockf - seen on linux, netbsd, ... I don't know about Solaris or others. ...
    (comp.unix.programmer)