Help with using fcntl file locking 
Author Message
 Help with using fcntl file locking

This is on RH 7.1, Perl 5.6.  System is compiled to use flock() so to get
fcntl file locking [until/if I can get Perl recompiled with use-flock
turned off] I need to do the fcntl calls explicitly.  I rarely have much
luck doing the low level stuff with the packs and all and getting it right
and this case is no exception.  If someone could provide me with some help
on what's going wrong I'd appreciate it.  Here's a little test program:

================================================
#!/usr/bin/perl -w
use Fcntl ;
use strict ;

use constant target => "/tmp/locktest" ;
use constant structflock => "SSLLL" ;  # I *think* this is right

if (open (F, target))
{   print "Opening locktest file\n" ; }
else
{   die "Can't find target file\n" ; }

print "Locking target file\n" ;
my $lockit = pack(structflock, F_WRLCK, 0,0,0,0) ;
fcntl (F, F_SETLKW, $lockit) ;
print "Holding lock\n" ;
system("sleep 30") ;
print "Releasing lock\n" ;
close F ;
system("sleep 30") ;
print "Exiting\n" ;
===============================

I test this by doing:   locktest.pl& locktest.pl&

When I do this program using flock() it works exactly as I hoped/expected:
the second instance of the program stalls until the first does a 'releasing
lock'.  But I've tried a bunch of tweaks and twists [e.g., using F_RDLCK
and F_RdLCK | F_WRLCK] and no matter what I try, I get results like:

[bernie]> ./locktest.pl & ./locktest.pl&
Opening locktest file
Locking target file
Holding lock
[2] 1380
Opening locktest file
Locking target file
Holding lock
[3] 1382
Releasing lock
Releasing lock
Exiting
Exiting

with both processes happily "holding the lock" at the same time.  I'm
clearly not grokking something simple/basic here.  The unix man page
doesn't list any errors for F_SETLKW so I assume that it can never fail: it
stalls until it can do its thing and then just returns.   THANKS!!

  /Bernie\
--
Bernie Cosell                     Fantasy Farm Fibers

    -->  Too many people, too few sheep  <--          



Sat, 08 Nov 2003 06:35:58 GMT  
 Help with using fcntl file locking
On Mon, 21 May 2001 18:35:58 -0400, Bernie Cosell

Quote:

} This is on RH 7.1, Perl 5.6.  System is compiled to use flock() so to get
} fcntl file locking [until/if I can get Perl recompiled with use-flock
} turned off] I need to do the fcntl calls explicitly.

A modest bit of poking around and I've gotten the program working.
PHEW!!  I doubt anyone cares, but here's what it took:

Quote:
} ================================================
} #!/usr/bin/perl -w
} use Fcntl ;
} use strict ;
}
} use constant target => "/tmp/locktest" ;
} use constant structflock => "SSLLL" ;  # I *think* this is right
}
} if (open (F, target))

  # change the above to:
Quote:
}} if (open (F, "+<".target))

  # You can't get a write lock unless the file is open for writing!

[...]

Quote:
} my $lockit = pack(structflock, F_WRLCK, 0,0,0,0) ;
} fcntl (F, F_SETLKW, $lockit) ;

And that's it and it works -- the entire problem was that I was trying
to get a write-lock on a file that I didn't have open for writing.

Quote:
} ...  The unix man page
} doesn't list any errors for F_SETLKW so I assume that it can never fail:

The man page lies (or omits).  There are a LOT Of reasons-for-failure
for this one [e.g., invalid FD].  the key debugging stage for this was
the obvious thing I should've done first:
    fcntl(F,F_SETLKW,$lockit) or die "Error in fcntl: $!\n" ;

  /Bernie\
--

Roanoke Electronic Village



Sun, 09 Nov 2003 02:53:40 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Has anyone used Fcntl to handle file locking?

2. Req for example: file lock using fcntl

3. File locking using Fcntl

4. Nee file lock example using fcntl

5. Need example of file locking using fcntl

6. Trouble using fcntl to lock file

7. HELP! fcntl and file locking

8. Using fcntl for locking

9. File locking with fcntl()

10. problem locking file with fcntl (HP-UX)

11. File locking via 'fcntl()'

12. File Locking: why not use fcntl?

 

 
Powered by phpBB® Forum Software