Here is a simple example that emphasizes the danger of concurrent wites:
<?php for($i = 0; $i < 100; $i++) { $pid = pcntl_fork();
If the addition was safe, you should get a file with 100 lines, all of which are approximately 10,000 characters long and begin with an integer. And sometimes, when you run this script, this is exactly what you get. Sometimes several add-ons will conflict, but this will be distorted.
You can find damaged lines with grep '^[^0-9]' test.txt
This is because the append file is only atomic if :
- You make one call to fwrite ()
- and that fwrite () is less than PIPE_BUF (somewhere around 1-4k)
- and you write a fully compatible POSIX file system.
If you make more than one call for recording while adding a magazine or write more than 4k, all bets are disabled.
Now about whether it really matters: are you okay with a few corrupt lines in your journal under heavy load? Honestly, most of the time this is acceptable, and you can avoid the overhead of locking files.
Frank farmer
source share