flock() time 
Author Message
 flock() time

How long does a second process keep attempting to access a file that has
been flocked by a previous process?

Is there a default time length, or is there a way to control this length
of time (i.e., tell the second process to try for only 10 seconds before
erroring out)?

I have looked in the docs, and this group, and the archives, but still
couldn't find the answer.

Awaiting any informative help,
Jerry



Wed, 23 Feb 2000 03:00:00 GMT  
 flock() time

Charles> raise an exception, e.g.,

Charles>   $SIG{ALRM} = sub { die "expired" };
Charles>   alarm(60);    
Charles>   eval { flock() };
Charles>   alarm(0);

Charles>      ...

Charles>     ...

actually, this'd be cooler and safer:

        eval {
                local $SIG{__DIE__} = 'DEFAULT';
                local $SIG{ALRM} = sub { die "expired" };
                alarm(60);
                #thing that might take time or not
        };
        alarm(0);

I'm probably still overlooking something, but that's a closer cut
than yours.

print "Just another Perl hacker," # but not what the media calls "hacker!" :-)
## legal fund: $20,990.69 collected, $186,159.85 spent; just 358 more days

--
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying

Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me



Thu, 24 Feb 2000 03:00:00 GMT  
 flock() time

 > How long does a second process keep attempting to access a file that has
 > been flocked by a previous process?
 >

 until the lock's released... or the process is euthanized :)

 > Is there a default time length, or is there a way to control this length
 > of time (i.e., tell the second process to try for only 10 seconds before
 > erroring out)?
 >
 >

raise an exception, e.g.,

  $SIG{ALRM} = sub { die "expired" };
  alarm(60);    
  eval { flock() };
  alarm(0);

     ...

    ...

HTH,
--
Charles DeRykus



Fri, 25 Feb 2000 03:00:00 GMT  
 flock() time



 > Charles> raise an exception, e.g.,
 >
 > Charles>   $SIG{ALRM} = sub { die "expired" };
 > Charles>   alarm(60);    
 > Charles>   eval { flock() };
 > Charles>   alarm(0);

 > Charles>      ...

 > Charles>     ...
 >
 > actually, this'd be cooler and safer:
 >
 >   eval {
 >           local $SIG{__DIE__} = 'DEFAULT';
 >           local $SIG{ALRM} = sub { die "expired" };
 >           alarm(60);
 >           #thing that might take time or not
 >   };
 >   alarm(0);

 >

 > I'm probably still overlooking something, but that's a closer cut
 > than yours.

Don't look back - paranoia may be gaining on ya :)

P.S. - much cooler solution though.

--
Charles DeRykus



Fri, 25 Feb 2000 03:00:00 GMT  
 flock() time

Quote:

>How long does a second process keep attempting to access a file that has
>been flocked by a previous process?

>Is there a default time length, or is there a way to control this length
>of time (i.e., tell the second process to try for only 10 seconds before
>erroring out)?

>I have looked in the docs, and this group, and the archives, but still
>couldn't find the answer.

That might depends on the system call, but usually it will be blocked till
the lock goes away.

Besides, the second process will not 'try to access the file'. Upon calling
the flock on a already locked file the process will be blocked (kind of
stopped) by the kernel until the lock goes away.
It will join the long, long queue of other blocked processes in the system :-)

ciao
  lutz
--

Do not take life too seriously, you will never get out of it alive.



Sun, 27 Feb 2000 03:00:00 GMT  
 flock() time

In comp.lang.perl.misc, on Sat, 06 Sep 1997 18:26:47 -0500

Quote:

>How long does a second process keep attempting to access a file that has
>been flocked by a previous process?

If you call flock() on a file descriptor *without* bit-oring LOCK_NB
(which stands for LOCK_NonBlocking ) into the operation code, the call
will block indefinitely.

If you *do* bit-or LOCK_NB into the operation code for the flock() call,
the call will return immediately.

Oh, and be *sure* that flock() is being called on a local file.  If you
try to do this on a file available via NFS or some other networked file
system, it will fail dismally.  You need to use fcntl() instead.

If you want some other condition to be able to interrupt the flock()
call, (such as time elapsed since first trying to lock the file, or
somesuch), then you should use LOCK_NB and combine it with sleep() in a
while loop...

WARNING:  Untested code which anyone may copy and use follows:

   # Just for completeness' sake. :)
   $LOCK_SH = 1;
   $LOCK_EX = 2;
   $LOCK_NB = 4;
   $LOCK_UN = 8;

   # Check this value on your system, it may *not* be 11.
   # Have a look at /usr/include/sys/errno.h (that's the usual place,
   # anyway), for EWOULDBLOCK.  This may be an alias for EAGAIN.  If so
   # then use the number for EAGAIN.
   #
   $EWOULDBLOCK = 11;

   # Maximum number of times to try flock()
   $max_tries = 10;
   # time to sleep,
   $time_to_sleep = 5;

   $retry = 1;
   $tries = 0;
   while ( $retry &&
           ( $tries < $max_tries ) ) {
      if (flock(FOO_FILE, $LOCK_EX | $LOCK_NB)) {
         #
         # We have locked the file, we're fine, stop retrying.
         #
         $retry = 0;
      } elsif ( $! != $EWOULDBLOCK ) {
         #
         # flock returned an error, and $! (errno) was *not* $EWOULDBLOCK.
         # This means the syscall died horribly.
         #
         $retry = 0; # Don't really need this, we're about to die.

         die "Error trying to lock file:  $!\n";
      } else {
         #
         # flock failed with errno == $EWOULDBLOCK
         # Sleep for a bit, then 'round we go again.
         # Note, you could do something else here, like ask the user a
         # question, or test some other condition, etc.
         #
         sleep($time_to_sleep);
         $tries++;
      }
   }

   if ($tries >= $max_tries) {
      # NB:  Careful, this might be an off-by-one error, I haven't
      # tested it.  Please do so before using this code.
      #
      # We tried ten times, and still couldn't get a lock.
      die ("Could not obtain lock on file in ",
           $max_tries * $time_to_sleep, " seconds\n");
   }

End of untested code.  No guarantees are made of exact correctness.
Use at own risk.  "Warning Warning! Danger Will Robinson, Danger!" :)

What you *probably* should do is put all the above {*filter*}into a
subroutine, with parameters for time to wait, and all that junk.

Later,

   David

--
Unico   Computer   Systems   Pty   Ltd
David Goh            Software Engineer



Mon, 28 Feb 2000 03:00:00 GMT  
 flock() time

Quote:

> Oh, and be *sure* that flock() is being called on a local file.  If you
> try to do this on a file available via NFS or some other networked file
> system, it will fail dismally.  You need to use fcntl() instead.

This is not necessarily the case--on systems where fcntl() is better
(SYSV derivatives) Perl emulates flock() via fcntl().  It's been this
way since 5.001m or so.

-Doug
--
sub g{my$i=index$t,$_[0];($i%5,int$i/5)}sub h{substr$t,5*$_[1]+$_[0],1}sub n{(
$_[0]+4)%5}$t='encryptabdfghjklmoqsuvwxz';$c='fxmdwbcmagnyubnyquohyhny';while(
$c=~s/(.)(.)//){($w,$x)=g$1;($y,$z)=g$2;$w==$y&&($p.=h($w,n$x).h($y,n$z))or$x==
$z&&($p.=h(n$w,$x).h(n$y,$z))or($p.=h($y,$x).h($w,$z))}$p=~y/x/ /;print$p,"\n";



Mon, 28 Feb 2000 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. suggest flock to provide time-out

2. flock doesn't flock under perl/linux

3. Flock me here flock me there.

4. Workaround for FLOCK?/When is FLOCK not supported?

5. how to use flock() on a machine, which dosn't support flock()

6. Using Flock II - The Revenge of Flock

7. ANNOUNCE: Time::Period - Evaluate a Time Point Relative to a Time Period

8. How I read this: srand(((time/$$)^($>*time))/(time/(time^$$)));

9. Time::CTime.pm Time::JulianDate.pm

10. Getting UTC of a particular calendar time in some time zone

11. Time::Local vs. Daylight Savings Time

12. Convert GMT time to North American times

 

 
Powered by phpBB® Forum Software