https://unix.stackexchange.com/a/12818/674 says

Lock files are used by programs to ensure two (well-behaved) separate instances of a program, which may be running concurrently on one system, don't access something else at the same time. The idea is before the program accesses its resource, it checks for presence of a lock file, and if the lock file exists, either error out or wait for it to go away. When it doesn't exist, the program wanting to "acquire" the resource creates the file, and then other instances that might come across later will wait for this process to be done with it. Of course, this assumes the program "acquiring" the lock does in fact release it and doesn't forget to delete the lock file.

This works because the filesystem under all UNIX-like operating systems enforces serialization, which means only one change to the filesystem actually happens at any given time. Sort of like locks with databases and such.

What does the last paragraph mean?

Is it correct that in Linux, a file can be written simultaneously by two processes, and the OS doesn't provide implicit synchronization but require explicit synchronization from programmers?

Is lock file an explicit way to synchronize the access to the same file by multiple processes?


  • 2
    Two processes cannot create a file at the same time. If the calls happen simultaneously, one will succeed and the other will say file exists. The content of lock files is typically less important than the presence of them.
    – LawrenceC
    Oct 18, 2018 at 13:54
  • @LawrenceC you could write that up as an answer (perhaps with a little more detail, since this only works if all processes involved “agree” to open the lock file with O_CREAT | O_EXCL), especially since you’re the best-placed person to explain what you meant by the last paragraph of the answer you wrote ;-). Oct 18, 2018 at 13:57
  • @StephenKitt Make sense. Thanks. Appreciate if you also take a look at unix.stackexchange.com/questions/476329/…
    – Tim
    Oct 18, 2018 at 16:29


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.