Ok, one process writes to a temporary file, and when it has finished writing, renames the file. At the sametime, another process writes to it's own copy of the temporary file, and when it finished, renames the file... The second process will clobber what's been written by the first process.

To have many processes successfully writing to the same file, you would need some sort of locking mechanism in place to protect critical parts of the program. And the locking can be implemented using IPC::Semaphore on *nix and Win32::Semaphore on windows.

Now, combine the semaphore with the temporary file, and the new algorithm looks like:
process start if read then obtain shared read lock on the original text file read the data release shared lock elsif write then obtain exclusive write lock on the temporary file write the data obtain exclusive write lock on the original file overwrite the original file with new data release lock on original file release lock on temporary file end if

This algorithm penalizes the writer, assuming that there are more readers than writers, and that writing is a lengthy process. The benefit is that the data is kept consistant with locking, also the readers can still read the original file while the new file is being written.

Another variant is to omit the temporary file and let the writing process lock the original text file, but that imposes penalty on reading processes where all readers must wait for the writer to finish, if the writing of data is an expensive process.

If you want to minimize the penalties to both the reader and the writer, then you need some more elaborate caching and locking algorithm, which is probably out of scope.


In reply to Re: Re: Apache and file generation - flock? by Roger
in thread Apache and file generation - flock? by ecuguru

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.