What I have done in similar cases is lock a different file. E.g. before opening "ImportantData.txt", my programs first open and lock "ImportantData.lock" and hold it until they are really done with the file "ImportantData.txt".

Updated:Checking back, I see a lot of people tell you "no problem", "don't worry", etc. Wrong! You are right to be concerned. They do not understand the issues.

Example: have two processes which are each going to add 1 to the value in the file.

Process 1 opens file IN, "<in.txt" Process 1 locks file IN Process 1 opens file OUT, ">out.txt"; Process 1 locks file OUT Process 2 opens file IN, "<in.txt"; Process 2 tries to lock IN and waits (The vulnerable window is greatly reduced if you use LOCK_NB, but no +t eliminated. But then you have to close and reopen IN in a busy loop +). Process 1 reads the value "100" from IN Process 1 writes the value "101" to OUT In any order (or atomically): Process 1 closes IN (thereby unlocking it) Process 1 closes OUT (thereby unlocking it) Process 1 rename "out.txt" to "in.txt" Process 2 acquires lock on file IN Process 2 opens file OUT, ">out.txt"; Process 2 locks file OUT Process 2 reads the value "100" from IN Process 2 writes the value "101" to OUT In any order (or atomically): Process 2 closes IN (thereby unlocking it) Process 2 closes OUT (thereby unlocking it) Process 2 rename "out.txt" to "in.txt"
Result: file "out.txt" has the value "101" instead of the correct "102"

If you use use the default blocking lock, you have a huge vulnerability. You not only have to worry about another process opening the file between your open and lock, you have to worry about any process opening it from the time you lock it until the rename is completed!


In reply to Re: Order of flock and open by Thelonius
in thread Order of flock and open by svsingh

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.