Hi there, I'm very new to the site, and I'm a little overwhelmed :-) I was looking around and see that the discussions are quite active, so I'm hoping someone could help .. please:-) I've been coding perl for sometime now, but there has always been one thing that gets my goat .. and that is flocking.

When I first began I used

flock(file,2);

now I use:

use Fcntl ':flock'; flock(file,LOCK_EX); seek file, 0, 0;
Plucked pretty much from the perl man pages. I really try to read everything I can before asking but this one has got me stumped. Just when I figured I got it down pat, it seems I am running into to problems.

creating a simple read and write counter to demonstrate the flocking abilities, I see that every so often, the file is cleared. On every instance I read & write to the file I use the second method above, except for reading I used a shared_lock (LOCK_SH) When I change the shared lock to exclusive, it seems to run better, but then it happens again.

Searching around I came apon this page: From the Monastery and the last reply added by user KM has got me thinking. It seems reasonable, but I have to say I'm confused.

This method that the user speaks of:

#!/usr/bin/perl -wT use Fcntl qw(:flock); my $file = 'test_lock.txt'; my $SEMAPHORE = $file . '.lck'; open(S, ">$SEMAPHORE") or die "$SEMAPHORE: $!"; flock(S, LOCK_EX) or die "flock() failed for $SEMAPHORE: $!"; open (FH, ">>$file") or die "Can't open $file: $!"; print "About to write\n"; print FH "I have written ($$)\n"; print "Written\n"; close FH; print "Going to sleep...\n"; sleep 10; print "Woken up...\n"; close S;
... does this lock the file being written to, because it is hanging when the other file is locked arg :( Should I use this method? Is it more secure?

Please help, I'm still learning everday, and this is one question I would like to resolve and get a good nights sleep :-)

Thank you sincerly for any replies.


In reply to Flocking .. insanity is close. by Anonymous Monk

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.