Most file systems (but not all) use locks to protect simultaneous access to a single file. The lock can be exclusive, so the first user gets access to the lock - subsequent users receive a "denied access" error. In your example script, user A will be able to read the file and obtain a file lock, but user B will not be able to write while user A is reading.
Some file systems (such as NTFS) allow you to specify a lock level to allow, for example, simultaneous readers, but no writers. Byte locks are also possible.
Unlike databases, file systems are usually not transactional, not atomic, and changes from different users are not isolated (if changes can even be seen, blocking can prevent this.)
Using whole file locks is a rough approach, but it will protect against inconsistent updates. Not all file systems support locking of entire files, so it is common practice to use a lock file β a typically empty file whose presence indicates that its associated file is being used. (Creating a file is an atomic operation on most file systems.)
mdma May 01 '10 at 22:22 2010-05-01 22:22
source share