: a technique for avoiding conflict
s if a resource
is accessed concurrent
ly. Can be used to enforce mutual exclusion
An example: Say you have a file saved somewhere on a computer network. Two users A and B both open the file for editing at (roughly) the same time. After some time, A saves the modified file; some time later, B does
In this case, whoever saves their work last will overwrite the previously saved version, wiping out those changes, so here A's changes will be lost. This is because B's editor program does not realize the file changed after it was read in initially.
One way to avoid problems like this is to have some mechanism to indicate that a resource is in use, that it is "locked"; this is called locking. Often a lock file is used to indicate this, but it can be anything which
all those doing access respect.
The problem of avoiding or controlling concurrent access arises in all situations where concurrent access can happen, such as multiprocessor systems, multi-threaded programs or networks with several machines. The implementation of
the lock (file, memory area, spinlock, semaphore...) differs, but the principle is the same.
A good example for the use of locking is the Unix editor vim: Open any file with vim, then go to a different terminal, start another vim and open the same file. You will get a warning dialogue. Vim uses lock files, by the way.