I have read the excellent tutorial:

http://www.perlmonks.org/?node_id=7058

But somehow I can't get it to work. For example, here is a little script that (a) opens a file, (b) puts an exclusive lock on it, (c) closes the file (hence in theory releasing the lock), then (d) reopens the file and finally (e) tries to put another exclusive lock on it.

use strict; my $fpath = 'blah.dat'; open(FILEHANDLE_1, ">", $fpath); print "Opened file '$fpath' into FILEHANDLE_1.\n"; flock(FILEHANDLE_1, 2); print "Put exclusive lock on FILEHANDLE_1\n"; close(FILE_HANDLE_1); print "Closed FILEHANDLE_1. It should now be possible to put an exclus +ive lock on file '$fpath'\n"; open(FILEHANDLE_2, ">", $fpath); print "Opened file '$fpath' into FILEHANDLE_2.\n". "Will now try to put an exclusive lock on it.\n"; flock(FILEHANDLE_2, 2); print "Successfully put exclusive lock on FILEHANDLE_2\n";

The problem is that the last line is never printed at all! In other words, the script is not able to secure the second lock, eventhough the first lock should have been released by the closing of FILEHANDLE_1.

What am I doing wrong? Note that I am running this on Windows 7. Could this be a problem with that OS? Does this script run fine on Linux? Thx. Alain Désilets

In reply to Can't get flock() to work. Please help... by alain_desilets

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.