If you are using log files, you will typically append to the files, instead of overwriting them each time you log something. Modern Unix kernels (I've no idea how non-Unix kernels deal with the problem) will garantee that writes done to files opened for append are atomic. That means that if you are printing small strings (typically strings not exceeding 512, 1024 or 4096 bytes) you will be fine - no clobbering.
However, if that isn't the case, you still need to use flock. But that's really easy. Assume you open the file(s) for append, all you need is:
That's all there's to it! One use statement, to include the constant LOCK_EX and just one extra statement.use Fcntl ':flock'; .... # Code done in every child. open my $fh => ">> /path/to/log/file" or die "Failed to open: $!"; flock $fh => LOCK_EX or die "Failed to lock: $!"; print $fh $your_text; close $fh or die "Failed to close: $!";
The perlopentut manual should tell you more about flocking. You also might want to look into man perlipc.
-- Abigail
In reply to Re: Fork novice requests help...
by Abigail
in thread Fork novice requests help...
by thor
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |