View errata for Files, Part 1: I/O and Permissions
Page 213
For most file system types, using open (O_CREAT | O_EXCL) works ok for locking a file. This does not work for NFS volumes since you could find yourself in a race condition. For doing atomic locking for a lockfile is to make a unique file (such as using the hostname and pid of the process) and use link() to make a link to the lockfile. If link() succeeds, you have the lock. Otherwise use stat() on your unique file to see if its link count has increased to two, in which case the lock is successful. Otherwise you were not able to get the lock.
borkware home |
products |
miniblog |
rants |
quickies |
cocoaheads
Advanced Mac OS X Programming book
webmonster@borkware.com