-1

Thanks to Mark's comment, pointing me to https://en.wikipedia.org/wiki/File_locking#Lock_files which says

Shell scripts and other programs often use a strategy similar to the use of file locking: creation of lock files, which are files whose contents are irrelevant (although often one will find the process identifier of the holder of the lock in the file) and whose sole purpose is to signal by their presence that some resource is locked. A lock file is often the best approach if the resource to be controlled is not a regular file at all, so using methods for locking files does not apply. For example, a lock file might govern access to a set of related resources, such as several different files, directories, a group of disk partitions, or selected access to higher level protocols like servers or database connections.

In "A lock file is often the best approach if the resource to be controlled is not a regular file at all, using methods for locking files does not apply",

  • why is a lock file the best if the resource is not a regular file? I can't see a lock file would be different whether the protected resource is a regular file or not.

  • What does "using methods for locking files does not apply" mean, and why? Isn't that opposite to "A lock file is often the best approach"? Or is the method for locking files different from the lock file approach ?

  • Some inode doesn't support locking, for devices nodes, even it supports locking, the device may not be locked, You can always create another inode represent the same device. – 炸鱼薯条德里克 Oct 18 '18 at 14:52
  • Because file locking can only lock a normal file, other objects like pipes or devices can't be locked, at least not by the same locking method. – 炸鱼薯条德里克 Oct 18 '18 at 14:54
2

If the resource to be controlled is not a regular file at all, then you could not use the existence of that regular file as its own lock file; then you'd need to create a separate file to use for locking.

  • Thanks, "If the resource to be controlled is not a regular file at all, then you could not use the existence of that regular file as its own lock file". but does file locking really use the shared file itself as a lock file on its own? file locking uses a file/record lock (not a lock file) on the shared file. – Tim Oct 18 '18 at 16:22
  • locking is implemented in various ways; it's up to the controlling resource to decide if and how it is going to lock access to some resource. The simpler case, which you started with is exactly the creation of a single file where a process wants to ensure that it was the creator, not something else. – Jeff Schaller Oct 18 '18 at 16:58
  • Can you ever use an existing file as a lock file on itself? (even If the resource to be controlled is a regular file) – Tim Oct 18 '18 at 17:03
  • Why would you lock a lock file? Are you trying for a recursive infinite loop? – Jeff Schaller Oct 18 '18 at 17:12
  • I am not sure what you mean by "If the resource to be controlled is not a regular file at all, then you could not use the existence of that regular file as its own lock file;" – Tim Oct 18 '18 at 17:26

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.