Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Introduction to File Locking in Linux
File locking is a mechanism used to restrict access to a file to only one process or user at a time. It is essential in multi-user systems to avoid conflicts when multiple processes try to access the same file simultaneously. In Linux, file locking prevents other processes from accessing a file until the lock is released.
This article explores file locking in Linux and how to implement it using C code. We will discuss the different types of file locks, how to create and release locks, and how to handle file lock monitoring.
Types of File Locks
In Linux, there are two types of file locks
| Lock Type | Description | Enforcement | Use Case |
|---|---|---|---|
| Advisory Locks | Cooperative locks that processes voluntarily respect | Application-level | Multiple processes coordinating access |
| Mandatory Locks | Kernel-enforced locks that block access automatically | Kernel-level | Critical files requiring strict protection |
Implementing Advisory Locks Using fcntl()
Advisory locks allow processes to request a lock on a file, but they do not prevent other processes from accessing or modifying the file unless those processes also check for locks. Advisory locks are helpful when multiple processes need coordinated access to a file.
The fcntl() function is used to set advisory locks on files. It can acquire a lock on a particular section of a file or on the entire file.
Example Advisory Lock Implementation
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
int fd = open(argv[1], O_RDWR);
struct flock fl;
fl.l_type = F_WRLCK; // Set a write lock
fl.l_whence = SEEK_SET; // Start at beginning of file
fl.l_start = 0; // Offset from beginning of file
fl.l_len = 0; // Lock entire file
fl.l_pid = getpid(); // Set process ID
fcntl(fd, F_SETLKW, &fl); // Set the lock
printf("File locked<br>");
sleep(10); // Sleep for 10 seconds
fl.l_type = F_UNLCK; // Release the lock
fcntl(fd, F_SETLK, &fl); // Unlock the file
printf("File unlocked<br>");
close(fd);
return 0;
}
The above code sets a write lock on a file specified by the command-line argument. The fcntl() function is called with the F_SETLKW flag to acquire the lock and wait if necessary. The process then sleeps for 10 seconds to simulate work being done on the file. Finally, the lock is released using F_UNLCK.
File locked File unlocked
Implementing Mandatory Locks
Mandatory locks are kernel-enforced locks that prevent other processes from accessing or modifying a file until the lock is released. These locks are set by the kernel and cannot be overridden by processes. They require special filesystem mounting options and file permissions.
Example Mandatory Lock Implementation
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
int fd = open(argv[1], O_RDWR);
struct flock fl;
fl.l_type = F_WRLCK; // Set a write lock
fl.l_whence = SEEK_SET; // Start at beginning of file
fl.l_start = 0; // Offset from beginning of file
fl.l_len = 0; // Lock entire file
fl.l_pid = getpid(); // Set process ID
fcntl(fd, F_SETLK, &fl); // Set the lock
printf("File locked<br>");
sleep(10); // Sleep for 10 seconds
fl.l_type = F_UNLCK; // Release the lock
fcntl(fd, F_SETLK, &fl); // Unlock the file
printf("File unlocked<br>");
close(fd);
return 0;
}
File locked File unlocked
Monitoring File Locks in Linux
There are two primary ways to view all locks in a Linux system
lslockscommandcat /proc/locks
Using lslocks Command
The lslocks command displays all current file locks in your Linux system in a human-readable format.
$ lslocks
COMMAND PID TYPE SIZE MODE M START END PATH cron 752 FLOCK WRITE 0 0 0 /run... nautilus 2164 POSIX 3.9M READ 0 1073741826 1073742335 /home/user/.cache/tracker/meta.db whoopsie 955 FLOCK WRITE 0 0 0 /run/lock... snapd 787 FLOCK WRITE 0 0 0 /... tracker-miner-f 1480 POSIX 3.9M READ 0 1073741826 1073742335 /home/user/.cache/tracker/meta.db update-notifier 2049 FLOCK WRITE 0 0 0 /run/user/1000/update-notifier.pid
The output displays information on currently locked files within the system, including the lock type, file path, and the process holding the lock.
Using /proc/locks
The /proc/locks file is a virtual filesystem that provides detailed information about the locks held on files in the system. This method shows more technical details about each lock.
$ cat /proc/locks
1: FLOCK ADVISORY WRITE 2049 00:37:94 0 EOF 2: POSIX ADVISORY READ 1480 08:05:280388 128 128 3: POSIX ADVISORY READ 1480 08:05:280384 1073741826 1073742335 4: FLOCK ADVISORY WRITE 787 08:05:703530 0 EOF 5: FLOCK ADVISORY WRITE 955 00:1b:5 0 EOF 6: FLOCK ADVISORY WRITE 752 00:19:1123 0 EOF
Key Features
F_SETLK Attempts to acquire lock immediately, fails if unavailable
F_SETLKW Waits for lock to become available (blocking call)
F_GETLK Tests for existing locks without acquiring
F_UNLCK Releases an existing lock
Conclusion
File locking is a critical mechanism in Linux that prevents data corruption and ensures data consistency in multi-process environments. Advisory locks provide cooperative coordination between processes, while mandatory locks offer kernel-level enforcement. Understanding both types and their monitoring tools is essential for robust system programming.
