I fail to see the problem with flock. It's easy. However, there might be a way to avoid use of flock.

If you are using log files, you will typically append to the files, instead of overwriting them each time you log something. Modern Unix kernels (I've no idea how non-Unix kernels deal with the problem) will garantee that writes done to files opened for append are atomic. That means that if you are printing small strings (typically strings not exceeding 512, 1024 or 4096 bytes) you will be fine - no clobbering.

However, if that isn't the case, you still need to use flock. But that's really easy. Assume you open the file(s) for append, all you need is:

use Fcntl ':flock'; .... # Code done in every child. open my $fh => ">> /path/to/log/file" or die "Failed to open: $!"; flock $fh => LOCK_EX or die "Failed to lock: $!"; print $fh $your_text; close $fh or die "Failed to close: $!";
That's all there's to it! One use statement, to include the constant LOCK_EX and just one extra statement.

The perlopentut manual should tell you more about flocking. You also might want to look into man perlipc.

-- Abigail


In reply to Re: Fork novice requests help... by Abigail
in thread Fork novice requests help... by thor

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.