in reply to Best practices for modifying a file in place: q's about opening files, file locking, and using the rename function
wrt linux, locks are advisory. ie even if you lock the file
it doesn't keep other processes out of the file.
I don't think it matters in your particular case, but one must bear this in mind and check that the particular file one is wanting to lock has no other processes writing to it. eg, an ftp process is downloading the file and your process wants to write to the file (for some reason). If the ftp doesn't lock the file you will have to use "fuser" to test if the file has no processes attached to it. This will require root privileges, probably, if the writing proces is a different user to the reading one...