Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: A flock()alypse now

by Aristotle (Chancellor)
on Jul 03, 2002 at 14:38 UTC ( [id://179172]=note: print w/replies, xml ) Need Help??


in reply to A flock()alypse now

Disclaimer: I have yet to have had to do any real work with locking; what I know comes from a lot of theory I've read on the subject. Ok, now that we've dealt with that,

I would think the following is perfectly valid, and perfect period:

use Fcntl qw(:DEFAULT :flock :seek); sysopen FH, "file.name", O_RDWR | O_CREAT or die "horribly - $!"; # no + O_TRUNC!! flock FH, LOCK_EX or die "screaming - $!"; my @slurp = <FH>; do_something_with(\@slurp); seek FH, 0, SEEK_SET; print FH @slurp; truncate FH, tell FH; close FH;
As far as I can tell, it deals with everything. It requires no semaphore files so I needn't worry about potential write permission woes nor cleaning up after myself when I exit, and it operates on exactly one filehandle, which is the one holding the lock. Someone tell me if there's something missing in my picture; if not, then I believe (barring other requirements) this is The Way To Do It.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re: Re: A flock()alypse now
by ferrency (Deacon) on Jul 03, 2002 at 14:51 UTC
    The only concern I'd have with this technique is the problem tomhukins described earlier. I'm not sure if what he describes is likely, and whether it's more likely with a read/write filehandle than otherwise, but it's something to consider.

    Using a semaphore-like technique (whether it locks the file being modified or another file) also lends itself to situations where your data processing is abstracted away from your data input and output.

    Thanks for the example!

    Alan

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://179172]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-03-29 09:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found