First, a file lock is also a mutual exclusion mechanism that ensures that multiple processes can read/write to the same file in a secure manner. The reason for controlling these multi-process services is that the scheduling of these processes is unpredictable, and this unpredictability will cause competing access to the same file resource, resulting in unexpected results.

We can look at an example to understand the problem better.

Suppose we have an account.dat file that stores account balances with an initial value of “200”. The concurrent system has two processes to update the balance value on this file:

  1. Process A: reads the current value, subtracts 20, and saves the result back to the file.
  2. Process B: reads the current value, adds 80, and writes the result back to the file.

Obviously, after executing both processes sequentially, we expect the file to have the following values: 200-20 + 80 = 260.

However, if the process does not execute with the expected sequential diameter, different results may occur in the following cases:

  1. Process A reads the current value of the file (200) and is ready to perform further calculations.
  2. At this point, process B reads the same file and gets the current balance (200).
  3. Process A computes 200-20 and saves the result 180 back to the file.
  4. Process B is not aware that the balance has been updated. Therefore, it will still calculate 200 + 80 using the outdated value 200 and write the result 280 to the file.

As a result, the balance in the account.dat file is 280 instead of the expected 260.

File locks in Linux

As mentioned earlier, a file lock is a mechanism that restricts concurrent access to files between multiple processes. It allows only one process to access a file for a specified period of time, thus avoiding update problems.

We all know that rm -rf/is a very dangerous command in Linux. If we execute this command as root, it can even delete all files on a running system. This is because Linux does not automatically lock open files, so even running files can still be deleted by the RM command. Linux supports two types of file locks: Advisory lock and Mandatory lock.

Advisory Lock

Co-locking is not a mandatory locking scheme and is only effective if participating processes collaborate by explicitly acquiring the lock. Otherwise, if a process does not know the lock at all, the co-lock is ignored (meaning that processes must negotiate and obey the co-lock mechanism in order for the lock to work).

The following example will help us understand the co-locking mechanism more easily. Let’s review the account file example we mentioned earlier.

First, we assume that the file account.dat still contains the initial value of “200”.

Process A acquires the exclusive lock on the account.dat file, then opens and reads the file to get the current value: 200.

It is important to understand that co-locking is not set by the operating system or file system. Therefore, even if process A locks the file, process B is still free to read, write, or delete the file through system calls.

If process B does not attempt to perform the file operation without obtaining the lock, then it can be said that process B and process A are not cooperating using the co-locking mechanism and still have unexpected results.

Now, let’s look at how locks work in a collaborative process:

  1. Process B attempts (in collaboration with process A) to acquire A lock on the account.dat file before reading the file.
  2. Because process A has acquired the file lock, process B must wait for process A to release the lock.
  3. Process A computes 100-20 and writes 80 back to the file.
  4. Process A releases the lock.
  5. Process B now acquires a lock and reads the file, and gets the updated value: 180.
  6. Process B starts its logic and writes the result 260 (180 + 80) back to the file.
  7. Process B releases the lock so that other collaborating processes can continue reading and writing the file.

Mandatory Lock

Unlike cooperative locking, mandatory locking does not require any cooperation between participating processes. Once a forced lock is enabled on a file, the operating system prevents other processes from reading or writing to the file.

To enable mandatory file locking in Linux, two requirements must be met:

  1. We must mount the FILESYSTEM using the mand option (mount -o mand FILESYSTEM MOUNT_POINT).

  2. We must turn on the set-group-id bit and turn off the group execution bit for the files to be locked (chmod g + S, g-x FILE).

With a mandatory lock, the lock is managed and controlled at the operating system level.

Check all locks in the system

Slocks command

The lslocks command is a component of the util-Linux package and is available in all Linux distributions to list all currently held file locks on the system.

In the following list, you can see all the currently locked files on the system, as well as details about each lock, such as the type of lock and which process holds it.

/ proc /locks

/proc/locks is not a command, it is a file in the procfs virtual file system. This file contains all the current file locks, and the lsLOCKS command also relies on this file to generate the list.

To get the /proc/locks information, we execute “cat /proc/locks” :

Let’s start with the first line to see how lock information is organized in the /proc/locks filesystem:

  1. The first column is the serial number.
  2. The second field indicates the lock class to use, such as FLOCK (from FLOCK system call) or POSIX (from lockf, FCNTL system call).
  3. This column is used to describe the lock type. It can have two values: ADVISORY or MANDATORY.
  4. The fourth field shows whether the lock is a WRITE or READ lock.
  5. The fifth field is the process ID of the lock.
  6. This field contains a colon-delimited string that displays the ID of the locked file in the format of major-device: minor-device: inode.
  7. This column and the last column show the start and end of the locked region for the locked file. In this example row, the entire file is locked (0-eof).

Distributed file systems are mainly used to share files among multiple clients. It is common for client applications to concurrently access files. In practical applications, file locks are required for rendering and HPC services. YRCloudFile supports co-locking and shared locking in POSIX semantics. You can use system calls such as flock or FCNTL to lock files.