linux,

File locking with flock 101

Nov 25, 2021 · 2 mins read · Post a comment

As a concept, file locking in Linux purpose is to control access to a shared resource between multiple processes, in a way to indicate which process is next in line, so it could use the shared resource (file), also known as serialization. To ensure the serialization, a cooperation between processes is required, and before I give an example, keep in mind that multiple processes could hold a read lock simultaneously, where only one can have the write lock at a time. A cooperation means processes respects each other turns, so if process_1 is writing to a shared file (acquires write lock), and process_2 decides to write in the same file as well without trying to acquire write lock too, process_2 is not a cooperating one.

Since there are two types of Linux locks, advisory and mandatory, and the second type is unreliable, by default advisory locks are used. These locks are also called “unenforced”.

Now, flock is a utility which manages locks and could be used with shell scripts or from the Terminal.

Prerequisites

  • Linux bash environment

List file locks

List all held file locks at the moment.

lslocks

Output:

COMMAND           PID  TYPE SIZE MODE  M START END PATH
systemd-timesyn   780 FLOCK      WRITE 0     0   0 /run...
lvmetad           492 POSIX   4B WRITE 0     0   0 /run/lvmetad.pid
cron             1173 FLOCK   5B WRITE 0     0   0 /run/crond.pid

This information is pulled from the /proc/locks file.

flock example

Example backing up and uploading a Postgres database to S3 (as part of a shell script):

flock -xn /tmp/cron.lock -c "pg_dump -Fc devcoops_db > /db_backups/devcoops_db.sql"
flock -xn /tmp/cron.lock -c "aws s3 cp /db_backup/devcoops_db s3://devcoops/devcoops_db.sql"

So, in this case, the pg_dump command will finish first before aws s3 cp starts, as we want the flow to be.

  • -x: Stands for exclusive (write) lock.
  • -n: Fails immediately, rather than waiting for lock acquiring.
  • -c: Pass a command.

Conclusion

To get more details regarding flock run man flock. Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.