I've had a few problems lately with file locking. Admittedly my first solution where I was having the problems was shoddy, to say the least.
Let's say I have a system where more than one process will try to grab a pair of files (two associated files), read it/them, copy it/them elsewhere, and delete the originals. I want only one copy of the originals to be floating around. The initial solution was
get handles,
lock,
copy,
unlock,
unlink.
My processes were both able to get file handles, then if the first released its lock before the second tried to get one, comedy ensued. s/comedy/trouble/.
I thought I might have fixed that by unlinking before releasing the locks (might work?), but I was still getting some duplicates, because a filehandle still points to the file data even if it's been unlinked, and that handle can still get a lock on the filehandle (naivety made me think this might work, I suppose).
The newest fix, which seems to be working (touch wood) only because the file is actually two files, is to get a lock on one, then get a filehandle to the other, and then get a lock on it. If any of these fails, then give up. I think this is only working because one of the two files is essentially behaving like a lock file for the other.
My question is really this: is there a way to do this with only one file, not using a lock file?
I read the file locking tutorial
7058 (and the associated replies) by
turnstep, but it still leaves some threads hanging. Also, I suppose this isn't really a perl question, but I'm guessing there might be some people out there who have done similar...
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.