I was thinking of typical solutions for this problem:
- unlink() the file so it automatically gets freed
when you close it [wouldn't work here since you can't
create a new hard link from an open file handle, as nice
as that would be in a lot of situations]
- Mark the file as "delete on close" [I think
this is only a Win32 feature]
- Name the file such that the next run of the script
can delete it [only works in some situations and this
doesn't sound like one]
- Catch the signals that might be "interrupting"
the script and have the signal handler die and have an
END block clean up [you can't catch all signals and
having a signal handler means that there is a real (but
fairly small) chance that your script will crash after
it gets a caught signal]
- Have a parent spawn a child to run the script and
then have the parent wait for the child to finish and then
clean up if the child didn't [but now the parent is
the one that has to trap interrupts so we don't win much
here]
Then a (perhaps novel) approach came to me.
You could spawn a child that deamonizes itself
(so signals to the parent don't get sent to the child),
ignores all the signals it can (so no need for signal
handlers and the problems they cause in Perl), and then
waits for the parent to exit.
Ah, but how does a child wait for a parent to exit? Well,
just have the parent flock() the temporary file before
creating the child and pass a handle for the temporary file
to the child and the child can block on flock() waiting
for the parent to close the file. If the parent dies
suddenly, the operating system will still close the file,
which releases the lock and causes the child to wake up
and delete the file.
To prevent a race condition, I'd have the parent not even
try to delete the file (otherwise a new invocation of the
script might pick the same file name while a previous
child is just getting around to deleting what it considers
to be an old file). So in your case (of renaming the file),
the parent should just create an extra link (though this
makes the script less portable).
If you have trouble coding this, then let me/us know.
-
tye
(but my friends call me "Tye")
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.