Hello Wise Monks! I have since modified my counter upon the suggestion of various wise monks. However, my problem still persists: my counter resets to zero whenever 10 or more instances of it are run at the same time. What's still wrong with my code? It's true I could just give up and use an existing perl module to handle this, but my modified code below should be good enough, right?

$completeadd = "sacrifice.txt"; open(SAC, "$completeadd") || die "sacrificial file open failed: $!\n"; flock(SAC, LOCK_EX) || die "sacrificial Lock failed: $!"; $completeadd = "counter.txt"; open(MFILE, "$completeadd") || die "file open failed: $!\n"; flock(MFILE, LOCK_EX) || die "Lock failed: $!"; @filedata1=<MFILE>; chomp @filedata1; close(MFILE); if ($filedata1[0]) { $filedata1[0]=$filedata1[0] + 1; } else { $filedata1[0] = 1; } open(MFILE, ">$completeadd") || die "file open failed: $!\n"; flock(MFILE, LOCK_EX) || die "Lock failed: $!"; print MFILE "$filedata1[0]"; close(MFILE); close(SAC);

In reply to Safe Counter Follow Up # 2 by Gorby
in thread Safe Counter by Gorby

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.