Other Monks have already suggested using a third file, and I agree that what is missing here is a resource, but I would be inclined to not use file locking at all. If you consider this a serialization problem instead of a contention problem, a different solution presents itself: Funnel all requests for the files to be copied to a second program, which can deal with the requests in turn. Maybe this funnel is done by appending to a text file (Which the OS will usually keep atomic), or writing records to a queue table in a database, or using a socket.
Then all the second program does is watch for a signal to do the copy, and act on that. Since it is the only one doing the copy, no contention issue. If you want to discard some copy requests based on other criteria (can't copy within one minute of last copy, etc.) that becomes easy.
This also has other advantages, in that it can handle copy requests from programs that don't use Perl's locking semantics, can log all of the copy requests, etc.
--
Spring: Forces, Coiled Again!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.