Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Hi

I needed to use flock and found the docs confusing and the demo code slightly dangerous ( lock is another builtin for threading)

So I hacked a demo script, where only one instance will count from 1 to 10 when simultaneously running.

use v5.12; use warnings; #use Data::Dump; use Fcntl qw(:flock); use Time::HiRes qw/time sleep/; $|=1; my $lock_path = "./LOCK_DEMO"; my $wait = 10; # ------- traditional way test for existence say "First One !?!" if ! -e $lock_path; # NB: This fails sometimes because of race condition # ------ Open open my $fh, ">", $lock_path or die "Can't open $lock_path: $!"; # ------ Lock File say "Wait for Access"; flock($fh, LOCK_EX) or die "Cannot lock $lock_path - $!"; # ------ Unrivaled Actions say "There can be only one HIGHLANDER!"; for (1..$wait) { say $_; sleep 1; } say "I'm done"; # ------ Release Lock say "Un-Locking"; flock($fh, LOCK_UN) or die "Cannot unlock $lock_path - $!"; # ============ The last one has to switch off the light # ------ allow concurrent lock sleep 0.01; # ----- Test Non-Blocking Lock say "Try Lock without waiting"; my $can_lock = flock($fh, LOCK_EX | LOCK_NB); # ------ Close close $fh; # ----- Last script cleans up LOCK-File for next demo if ($can_lock) { say "LAST ONE!!!"; say "delete $lock_path"; unlink $lock_path or die "Cannot delete $lock_path - $!"; } else { say "not last one" } # ----- Keep cmd window open for a while for (1..20) { print "."; sleep 1; }

On windows you can start 4 simultanous instances from CMD with

> start perl demo_lock.pl & start perl demo_lock.pl & start perl demo_ +lock.pl & perl demo_lock.pl

The first three scripts will pop up in separate windows which you could/should arrange to be visible.

Have fun! :)

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery


In reply to File lock demo by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2022-08-17 01:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?