Re: Record Locking with fcntl(): what to do if unlock (F_UNLCK) fails?
- From: Dances With Crows <danSPANceswitTRAPhcrows@xxxxxxxxx>
- Date: Wed, 27 Sep 2006 15:51:41 -0500
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,
--
One OS to rule them all, with DRM to find them
One OS to bring them all and with the EULA bind them
In the Land of Redmond where the shadows lie.
There is no Darkness in Eternity/But only Light too dim for us to see
.
- Follow-Ups:
- Re: Record Locking with fcntl(): what to do if unlock (F_UNLCK) fails?
- From: The Natural Philosopher
- Re: Record Locking with fcntl(): what to do if unlock (F_UNLCK) fails?
- References:
- Record Locking with fcntl(): what to do if unlock (F_UNLCK) fails?
- From: denis . papathanasiou
- Record Locking with fcntl(): what to do if unlock (F_UNLCK) fails?
- Prev by Date: Re: Open source - Free software
- Next by Date: Re: i/o error problem with dvdrecord disks
- Previous by thread: Record Locking with fcntl(): what to do if unlock (F_UNLCK) fails?
- Next by thread: Re: Record Locking with fcntl(): what to do if unlock (F_UNLCK) fails?
- Index(es):
Relevant Pages
|