Advisory locking on files that Unix systems typically employs

filesfilesystemslock

I read that Unix like system typically use advisory locking for file IO vs mandatory locking used by Microsoft OS's. If I understood correctly in mandatory locking file lock is enforced by the OS itself. But I do not get the intuition of advisory locking. Take the example of two process,say "A" and "B" that are working on file "foo". Lets say "A" writes to "foo" and "B" reads from "foo". How would advisory locking work in this scenario? If the OS/file system is not enforcing a lock for "foo" then how is consistency maintained for reads and writes?

Best Answer

Advisory locking is for processes that cooperate "peacefully". The kernel keeps track of the locks but doesn't enforce them - it's up to the applications to obey them. This way the kernel doesn't need to deal with situations like dead-locks.

Mandatory locking was introduced in System V Unix, but it turns out that the design was not the brightest thing. (That is, there are ways around it.) If you need something like true mandatory locking under unixy systems, then follow a client-server design pattern where the server is the authority on the shared resource(s).

Related Question