in reply to File Locking revisited

I second the suggestion of using a third file. As with any type of locking it is very hard or impossible to lock an object and then also synchronize the move/removal of the same object -- there are almost always hidden catches (i.e. race conditions) to doing this. Sometimes you can code around them, but it tends to be much cleaner and simpler to give up and use a separate, higher level lock.

That said, sometimes I avoid locking if I have the luxury of moving the file to a private directory that no other process will use in the same file system. In this case, with a single file, the move tends to be atomic. Since you are using two files, there are probably other race conditions you have to worry about here so YMMV.