2 Types of Linux File Locking (Advisory, Mandatory Lock Examples)

by Lakshmanan Ganapathy on April 4, 2012

File locking is a mechanism which allows only one process to access a file at any specific time. By using file locking mechanism, many processes can read/write a single file in a safer way.

In this article we’ll explore the different types of Linux file locking and understand their differences using an example program.

We will take the following example to understand why file locking is required.

  1. Process “A” opens and reads a file which contains account related information.
  2. Process “B” also opens the file and reads the information in it.
  3. Now Process “A” changes the account balance of a record in its copy, and writes it back to the file.
  4. Process “B” which has no way of knowing that the file is changed since its last read, has the stale original value. It then changes the account balance of the same record, and writes back into the file.
  5. Now the file will have only the changes done by process “B”.

To avoid such issues locking is used to ensure “serialization”.

The following are the two types of Linux file locking:

  1. Advisory locking
  2. Mandatory locking

1. Advisory Locking

Advisory locking requires cooperation from the participating processes. Suppose process “A” acquires an WRITE lock, and it started writing into the file, and process “B”, without trying to acquire a lock, it can open the file and write into it. Here process “B” is the non-cooperating process. If process “B”, tries to acquire a lock, then it means this process is co-operating to ensure the “serialization”.

Advisory locking will work, only if the participating process are cooperative. Advisory locking sometimes also called as “unenforced” locking.

2. Mandatory Locking

Mandatory locking doesn’t require cooperation from the participating processes. Mandatory locking causes the kernel to check every open, read, and write to verify that the calling process isn’t violating a lock on the given file. More information about mandatory locking can be found at kernal.org

To enable mandatory locking in Linux, you need to enable it on a file system level, and also on the individual files. The steps to be followed are:

  1. Mount the file system with “-o mand” option
  2. For the lock_file, turn on the set-group-ID bit and turn off the group-execute bit, to enable mandatory locking on that particular file. (This way has been chosen because when you turn off the group-execute bit, set-group-ID has no real meaning to it )

Linux File Locking Examples

To understand how this works, crate the following file_lock.c program:

#include <stdio.h>
#include <fcntl.h>

int main(int argc, char **argv) {
  if (argc > 1) {
    int fd = open(argv[1], O_WRONLY);
    if(fd == -1) {
      printf("Unable to open the file\n");
      exit(1);
    }
    static struct flock lock;

    lock.l_type = F_WRLCK;
    lock.l_start = 0;
    lock.l_whence = SEEK_SET;
    lock.l_len = 0;
    lock.l_pid = getpid();

    int ret = fcntl(fd, F_SETLKW, &lock);
    printf("Return value of fcntl:%d\n",ret);
    if(ret==0) {
      while (1) {
        scanf("%c", NULL);
      }
    }
  }
}

Compile the program using gcc.

# cc -o file_lock file_lock.c

Remount the root filesystem with “mand” option using the mount command as shown below. This will enable mandatory locking at the file system level.

Note: You need to be root to execute the below command.

# mount -oremount,mand /

Create 2 files named “advisory.txt” and “mandatory.txt” in the directory where the executable (file_lock) is located. Enable the Set-Group-ID and disable the Group-Execute-Bit for “mandatory.txt” as follows

# touch advisory.txt
# touch mandatory.txt
# chmod g+s,g-x mandatory.txt

Test Advisory Locking: Now execute the sample program with ‘advisory.txt’ as the argument.

# ./file_lock advisory.txt

The program will wait to get input from the user. From another terminal, or console, try the following

# ls >>advisory.txt

In the above example, ls command will write its output to advisory.txt file. Even though we acquire a write lock, still some other process ( Non Cooperating ) can write into the file. This is termed as “Advisory” locking.

Test Mandatory Locking: Once again execute the sample program with ‘mandatory.txt’ as the argument.

# ./file_lock mandatory.txt

From another terminal or console, try the following:

# ls >>mandatory.txt

In the above example, ls command will wait for the lock to be removed before writing its output to the mandatory.txt file. It is still a non-cooperative process, but locking is achieved using mandatory locking.


Linux Sysadmin Course Linux provides several powerful administrative tools and utilities which will help you to manage your systems effectively. If you don’t know what these tools are and how to use them, you could be spending lot of time trying to perform even the basic administrative tasks. The focus of this course is to help you understand system administration tools, which will help you to become an effective Linux system administrator.
Get the Linux Sysadmin Course Now!

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 5 comments… read them below or add one }

1 bob April 4, 2012 at 7:32 am

thanks!!!
Great article. I learnt something new again today..
Well written.

2 dewey January 15, 2013 at 12:47 pm

Awesome article. Just thought I’d let you know that it’s still totally relevant.

FYI, I used your C code there to help debug something, and it worked like a charm.

3 Shankar July 3, 2013 at 7:40 pm

Nice article – Clear explanation. Thanks

4 shaj October 26, 2013 at 7:11 am

why it doesn’t work in ubuntu 13.04

5 shaj October 26, 2013 at 7:24 am

tried it out in ubuntu 13.04. But ls >>mandatory.txt aslo could win with out any block.

Leave a Comment

Previous post:

Next post: