Re: Record Locking with fcntl(): what to do if unlock (F_UNLCK) fails?
- From: The Natural Philosopher <a@xxx>
- Date: Thu, 28 Sep 2006 09:56:38 +0100
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.
.
- References:
- Record Locking with fcntl(): what to do if unlock (F_UNLCK) fails?
- From: denis . papathanasiou
- Re: Record Locking with fcntl(): what to do if unlock (F_UNLCK) fails?
- From: Dances With Crows
- Record Locking with fcntl(): what to do if unlock (F_UNLCK) fails?
- Prev by Date: Re: Open source - Free software
- Next by Date: Re: Same text at the beginning of line. Simple Shell-Script?
- Previous by thread: Re: Record Locking with fcntl(): what to do if unlock (F_UNLCK) fails?
- Next by thread: ADSENSE HELP GROUP IS A FRAUD
- Index(es):
Relevant Pages
|