[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: NFS locking issues



Hugo Graumann <graumann@ucalgary.ca> [2002-09-04 17:15:14 -0600]:
> 2) There is a system call to lock a file or parts of a file. In the
>    Stevens book "Advanced Programming for the Unix Environment" there
>    is a whole section on file locking including a c program to lock files.
>    If locking is working this program runs without error. If locking does
>    not work, It reports some weird message. I can send you the code if you
>    want. The program is about 70 lines long, so I am not sure if I can
>    post it to the list. It exists on the net, also. I found it in a gnome
>    mailing list where Havoc Pennington posted it in response to a gnome
>    file locking problem. A google search on "NFS file locking gnome" might
>    be how I stumbled on it.

Additionally perl, ruby, etc. scripts can call the C library file lock
routines.  Here is a perl script which can test out the rpc.lockd
functionality.  I wrote this script for that reason but I was not
having the trouble on linux but other OS's.  This is a simple and
brute force check.

Bob

#!/usr/bin/perl
use Fcntl ':flock'; # import LOCK_* constants
print("Just before opening file.\n");
open(LOCKFILE,">testlockfile") or
  die "Error: Could not write to lock file: testlockfile: $!\n";
print("Just before locking file.\n");
flock(LOCKFILE,LOCK_EX);
print("Just before unlocking file.\n");
flock(LOCKFILE,LOCK_UN);
print("All done.  File locked and unlocked.\n");
unlink("testlockfile");
exit(0);

Attachment: pgplHKwTv5z6E.pgp
Description: PGP signature


Reply to: