Re: Cannot lock a file.

From: Alex939393929 (aprutgers_at_hotmail.com)
Date: 03/24/04


Date: 24 Mar 2004 13:38:12 -0800

hsphuah@usa.com (Hon Seng Phuah) wrote in message news:<3898598f.0403240014.4614c926@posting.google.com>...
> Hi all,
>
> I have following code:
>
> use 5.004'
> use Fcntl ':flock';
>
> sub check_file_status
> {
> $attempt_lock = 0;
> $max_attempts = 3;
> open(in_file, "$check_in_file_name");
> while (!flock(in_file, LOCK_EX) && $attemp_lock < $max_attempts)
> {
> sleep(5);
>
> }
> close(in_file);
> }
>
> I cannot get the exlusive lock for the $check_in_file_name variable
> file name. Any idea? How should I correct my mistake?

tried it with perl 5.6.0, no problemo, note flock does not
work for network files (e.g. files retrieved via nfs mount point).

see perldoc -f flock

use 5.004;
use Fcntl ':flock';

sub check_file_status
{
   $attempt_lock = 0;
   $max_attempts = 3;
   my $check_in_file_name = 'file1.txt';
   open(in_file, "<$check_in_file_name")||print "$check_in_file_name:$!\n";
   while (!flock(in_file, LOCK_EX) && $attemp_lock < $max_attempts)
   {
      print "lock attempt $!\n";
      sleep(5);
      $attemp_lock++;

   }
   close(in_file);
}

check_file_status();



Relevant Pages

  • Re: perl should be improved and perl6
    ... Humans make mistakes (like the constant mistake you keep ... AD> Oh and BTW all you need is a fucking shift key. ... AD> prototypes in perl are not a good thing and no experienced ... AD> hacker uses them. ...
    (comp.lang.perl.misc)
  • Re: scheduling a perl script
    ... s99999999s2003@yahoo.com (mike) wrote: ... > How can i implement in perl such that on the 11th, my script will not ... > mistake that test.txt is not there while in fact, ...
    (comp.lang.perl.misc)
  • Re: debugging (stepping thru in debugger) within a try block
    ... They also don't exist in Perl, at least not under those names. ... Every single time I see somebody retype their code, ... mistake, it's a real one, not a mistake introduced by typos. ...
    (comp.lang.perl.modules)
  • Re: perl should be improved and perl6
    ... AD> Uri Guttman wrote: ... j> When passing a parameter to a function, in Perl, I do not know any way ... Humans make mistakes (like the constant mistake you keep ... AD> those are prototypes and you have it wrong. ...
    (comp.lang.perl.misc)
  • Re: Question for the regulars
    ... >> What is the worst mistake you've ever seen in a Perl script? ... before the recommendations, and reading the reasoning that led to the ... Sinan Unur ...
    (comp.lang.perl.misc)