Open for append + syswrite seems to work perfectly on Win32. Running the following code with an initial arg of 10, sets 10 copies of the code all writing to the same file. When they complete, a quick sort shows that every line, from each process, is in the file and uncorrupted.
It seems that specifiying append mode, not only does an initial seek to the end, but also ensures that each time a write is done, a seek-to-end is done implicitly also. (Or maybe my testcase is crap?).
#! perl -slw use strict; ## Decide a sync point if first instance ## or use the supplied sync time otherwise my $time = $ARGV[ 1 ] || time() +2 ; ## Ensure "recursion" stops. $ARGV[ 0 ]--; ## "Recursively" start asynchronous copies of ourself. system qq[start cmd /c $0 $ARGV[ 0 ] $time ] if $ARGV[ 0 ] > 0; ## Wait until sync time to give other copies a chance to get going. select undef, undef, undef, 0.1 until time() > $time; for( 1 .. 1000 ) { open FH, '>> :raw', 'data/append.tst' or die $!; syswrite FH, "$$\t$_\n"; close FH; ## Slow things a little so that the don't all comeout together. select undef, undef, undef, 0.001; }
In reply to Re^5: Writing to a log file without colliding
by BrowserUk
in thread Writing to a log file without colliding
by cgraf
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |