I feel like I end up reading this once a year, usually when I'm trying to write a [python] script that needs to run from a cron script without clobbering its own files if one instance is started before the previous one is finished.
See also my shelfcache Python3 package which provides a thread- and multiprocess-safe key-value caching store on top of the standard library's shelve module: https://github.com/cristoper/shelfcache
On the futility of Unix file locks (especially on networked file systems).
Note that Linux 3.15 introduced "open file description locks" to fcntl, which combines the semantics of BSD flock (lock associated with open file) with the record-locking and NFS-compatibility of fcntl. That solves everything except for portability.