Cannot lock a file.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Hon Seng Phuah

    Cannot lock a file.

    Hi all,

    I have following code:

    use 5.004'
    use Fcntl ':flock';

    sub check_file_stat us
    {
    $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?
  • Alex939393929

    #2
    Re: Cannot lock a file.

    hsphuah@usa.com (Hon Seng Phuah) wrote in message news:<3898598f. 0403240014.4614 c926@posting.go ogle.com>...[color=blue]
    > Hi all,
    >
    > I have following code:
    >
    > use 5.004'
    > use Fcntl ':flock';
    >
    > sub check_file_stat us
    > {
    > $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?[/color]

    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_stat us
    {
    $attempt_lock = 0;
    $max_attempts = 3;
    my $check_in_file_ name = 'file1.txt';
    open(in_file, "<$check_in_fil e_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_stat us();

    Comment

    • Hon Seng Phuah

      #3
      Re: Cannot lock a file.

      If I change the open to be write. the flock return 1. Why open read
      file cannot use exclusive lock like my case below? Does anyone have
      any idea?

      -HS Phuah

      hsphuah@usa.com (Hon Seng Phuah) wrote in message news:<3898598f. 0403240014.4614 c926@posting.go ogle.com>...[color=blue]
      > Hi all,
      >
      > I have following code:
      >
      > use 5.004'
      > use Fcntl ':flock';
      >
      > sub check_file_stat us
      > {
      > $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?[/color]

      Comment

      • nobull@mail.com

        #4
        Re: Cannot lock a file.

        hsphuah@usa.com (Hon Seng Phuah) wrote in message news:<3898598f. 0403241640.5de1 12cd@posting.go ogle.com>...[color=blue]
        > If I change the open to be write. the flock return 1. Why open read
        > file cannot use exclusive lock like my case below? Does anyone have
        > any idea?[/color]

        To prevent denial of service attacks. (It's an OS feature, nothing to
        do with Perl).

        Some OSs go futher and disallow taking a shared lock if you have the
        file open write-only, which, IMNSHO is just plain dumb.
        [color=blue][color=green]
        > > while (!flock(in_file , LOCK_EX) && $attemp_lock < $max_attempts)
        > > {
        > > sleep(5);
        > > }[/color][/color]

        You have not specified LOCK_NB. There's no point having a re-try
        loop. If a blocking flock() fails it is usually because the
        filehandle (or the file) in question is of a type that doesn't support
        the type of locking locking you are asking for. This isn't something
        that will change in 5 seconds.

        Oh, and BTW you forgot to increment your counter.

        This newsgroup does not exist (see FAQ). Please do not start threads
        here.

        Oh, and please don't top-post it is considered rude.

        Comment

        Working...