How do filesystems handle concurrent read/write?

Most filesystems (but not all) use locking to guard concurrent access to the same file. The lock can be exclusive, so the first user to get the lock gets access – subsequent users get a “access denied” error. In your example scenario, user A will be able to read the file and gets the file lock, but user B will not be able to write while user A is reading.

Some filesystems (e.g. NTFS) allow the level of locking to be specified, to allow for example concurrent readers, but no writers. Byte-range locks are also possible.

Unlike databases, filesystems typically are not transactional, not atomic and changes from different users are not isolated (if changes can even be seen – locking may prohibit this.)

Using whole-file locks is a coarse grained approach, but it will guard against inconsistent updates. Not all filesystems support whole-file locks, and so it is common practice to use a lock file – a typically empty file whose presence indicates that its associated file is in use. (Creating a file is an atomic operation on most file systems.)

Leave a Comment