Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Safe to open+close for every write, without locking?

by dave_the_m (Prior)
on Dec 21, 2012 at 15:32 UTC ( #1009928=note: print w/replies, xml ) Need Help??

in reply to Safe to open+close for every write, without locking?

Because you've opened the file for appending, you won't see any data clobbering (i.e. data lost/overwritten) but you will see data overlapping and lines split. i.e. each process will buffer its data into (likely 8k) blocks then append that block to the file. The blocks from different processes may be interleaved. So if a line from one process straddles two blocks, the first half of the line will be written, then possibly 8K of lines and half lines from another process, followed by the remainder of that line.


  • Comment on Re: Safe to open+close for every write, without locking?

Replies are listed 'Best First'.
Re^2: Safe to open+close for every write, without locking?
by sedusedan (Monk) on Dec 21, 2012 at 16:42 UTC
    Can you provide a script to demonstrate that, perhaps?
      Here ya go:

      #!/usr/bin/perl open my $fh, '>>', '/tmp/out' or die "open: $!\n"; sub output { my ($id) = @_; my $line = sprintf "[%02d: my id is: %02d]\n", $id, $id; # 19 char +s print $fh $line x 500; # bigger than 8K, and $line straddles boun +dary warn "$id: sleeping...\n"; sleep 1; warn "$id: exiting\n"; exit; } for (1..3) { sleep 1; warn "$_: spawning...\n"; my $pid = fork(); die "fork failed: $!\n" unless defined $pid; next if $pid; # child output($_); } sleep 5; __END__
      And here's the output:
      $ rm -f /tmp/out $ perl5160 /tmp/p1 1: spawning... 1: sleeping... 2: spawning... 1: exiting 2: sleeping... 3: spawning... 3: sleeping... 2: exiting 3: exiting $ grep -v '^\[..: my id is: ..]$' /tmp/out [02[03: my id is: 03] : my id is: 03] $


        Hi Dave,

        Thanks for writing the script. I added a seek($fh, 0, 2) before print $fh ... and it still manages to overlap.

        So, flock() it is.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1009928]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2018-05-28 03:12 GMT
Find Nodes?
    Voting Booth?