http://qs321.pair.com?node_id=254377


in reply to Order of flock and open

Since flock() can fail after a successful open(), you should make sure that your open() isn't destructive. In other words, if you're opening a file for writing, open it in such a way that if the file exists and contains data it won't be truncated.

Check out the links to Dominus' File Locking Tricks and Traps slides in the Question about Flock and die thread for more details.