Answer to: How can I flock LOCK_EX if there is always a LOCK_SH?
Score: 1
You don't need to use any locking.
Use rename().
Assuming this is a POSIX system and you're running on a local filesystem that supports atomic rename() operations (and if you're trying to do file locking on any non-local, network file system, give up now), and every file access by a reader is done via open()/read()/close() operations, don't bother using locks.
Simply write a new file in the same filesystem as the working copy, then call rename( "/path/to/new/file", /path/to/working/copy" ). Any open file descriptor on the original working copy of the file will remain valid, allowing any reader to have an unmodified copy of the data, and any open() call made after the rename() will use the new copy. And once the last open file descriptor on the old file is closed, the old file will be deleted.
And you wondered why Unix allows things like deleting or renaming a file while it's open? Now you know.
View Question ↗
Question
Parent Entity
Score: 10 • Views: 200
Site: stackoverflow
Other Comments / Reviews
SaaS Metrics