Linux file hierarchy – what’s the best location to store lockfiles

directory-structurefhslinuxlocksynchronization

I want to synchronize processes based on lock files (/ socket files). These files should only be removable by their creator user.

There are plenty of choices:

/dev/shm

/var/lock

/run/lock

/run/user/<UID>

/tmp

What's the best location for this purpose? And what way are above locations meant to be used for?

Best Answer

  1. /dev/shm : It is nothing but implementation of traditional shared memory concept. It is an efficient means of passing data between programs. One program will create a memory portion, which other processes (if permitted) can access. This will result into speeding up things.

  2. /run/lock (formerly /var/lock) contains lock files, i.e. files indicating that a shared device or other system resource is in use and containing the identity of the process (PID) using it; this allows other processes to properly coordinate access to the shared device.

  3. /tmp : is the location for temporary files as defined in the Filesystem Hierarchy Standard, which is followed by almost all Unix and Linux distributions. Since RAM is significantly faster than disk storage, you can use /dev/shm instead of /tmp for the performance boost, if your process is I/O intensive and extensively uses temporary files.

  4. /run/user/$uid: is created by pam_systemd and used for storing files used by running processes for that user.

Coming to your question, you can definitely use /run/lock directory to store your lock file.

Related Question