Locking is platform and device specific, but generally, you have a few options:
- use flock(), or equivilent (if your os supports it). This is advisory locking, unless you check for the lock, its ignored.
- Use a lock-copy-move-unlock methodology, where you copy the file, write the new data, then move it (move, not copy - move is an atomic operation), and you check for the existence of the lock file.
- Use a directory as a "lock". This is necessary if you're writing to NFS, since NFS doesn't support flock().
- There's also the possibility of using shared memory between the processes, but I've never tried that; its very os-specific.
For all these methods, you'll have to use a spin-lock (retry-after-failure) technique for acquiring and testing the lock. This does leave a small window for mis-synchronization, but its generally small enough to not be an major issue.
If you're looking for a solution that is cross platform, then you're better off logging to another system via some other mechanism (the next best thing is the NFS technique above).
Note that sqlite is subject to the same constraints over NFS that normal files are, so you can't write to an sqlite database on a network share and get synchronization for free.