What is file locking in linux

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

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

The following are the two types of Linux file 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 #include 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.

Читайте также:  1с сервер линукс 12 лицензий

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

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.

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

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.

From another terminal or console, try the following:

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.

Источник

File Locking in Linux

File locking in Linux is the solution by which you can ensure that the file for reading/writing is going to be handled safely.

Nope, this is not about password-protecting your files.

When multiple processes are working on the same file, you might not get intentional output as there was no order defined on how processes are going to handle the same file!

And file locking is the solution by which you can ensure that the file for reading/writing is going to be handled the safe way by defining the order.

It might sound too complex and that’s perfectly fine. So before approaching the solution, let me bring light to the problem.

Why File Locking is Needed

The problem is similar to the Race condition where multiple processes are modifying the same data simultaneously and the result strictly depends on the order of the process.

Читайте также:  Xerox versalink c7020 driver linux

Let’s say there are two processes A and B which are dealing with the file named account.txt (initial data is 100)and their behavior is like this:

  • A reads the current value and subtracts 20 from the initial balance
  • B reads the current value and adds 50 to the balance.

On paper, everything looks excellent and the final data should be 130 but it ends up being something else. Let me show you how:

  • Process A: Reads data (100) and prepares for the operation.
  • Process B: Reads data (100) and saves the current data to its buffer.
  • Process A: Subsctracts 20 from 100 making it 80.
  • Process B: It is unaware of the fact that the original data has been modified and uses 100 as the initial value so the final result ends up being 150 (100 + 50) rather than what you expected (130).

And there comes the implementation for file locking. So let’s have a look at the types of file locking in Linux.

Types of File Locking in Linux

There are two types of file locking in Linux:

1. Advisory Locking

The advisory locking system will not force the forces and will only work if both processes are participating in locking.

For example, process A acquires the lock and starts given tasks with a file.

And if process B starts without acquiring a lock, it can interrupt the ongoing task with process A.

So the condition for making an advisory lock is to ensure each process participates in locking.

2. Mandatory Locking

Mandatory locking can be understood as a forced locking system and it does not require any cooperation by the processes.

A mandatory locking causes the kernel to monitor every open, read, and write to verify that the process is not going to violate any process.

But making mandatory locking work requires comparatively more steps:

  • Mounting files with the “-o mand” option.
  • Also, users are required to turn off the group execute bit and turn on set-group-ID to enable mandatory locking as when you turn off the group-execute bit, set-group-ID will have no impact.
Читайте также:  Kali linux xfce gnome kde

How to Enable Advisory Locking

For this, I’ll be using the flock command which allows users to handle tasks related to advisory locking. And the syntax for the flock command is as follows:

flock file_to_lock command

First, let’s make a file to hold the default value:

Now, let’s make it hold 100 as the default value:

So let’s create a simple script to handle the logical part of the processes:

#!/bin/bash file="account.txt" value=$(cat $file) echo "Read current balance:$value" #sleep 10 seconds to simulate business calculation progress=10 while [[ $progress -lt 101 ]]; do echo -n -e "\033[77DCalculating new balance..$progress%" sleep 1 progress=$((10+progress)) done echo "" value=$((value+$1)) echo "Write new balance ($value) back to $file." echo $value > "$file" echo "Done."

Now, let’s create the process a.sh with advisory locking:

And make sure it contains the following:

#!/bin/bash flock --verbose balance.dat ./update_balance.sh '-20'

Now, let’s make it executable:

So let’s put it to the test:

Behavior with Non-cooperative process

For this example, I’ll be creating the script b.sh which will be non-cooperative to its nature.

And script should contain the following:

#---------------------------------------- # non-cooperative way #---------------------------------------- ./update_balance.sh '80'

Make it executable by following the given command:

So let’s put it to the test:

As you can clearly see, my desired value was supposed to be 160 but the process b messed up and gave 180.

Behavior with Cooperative Processes

In this section, I’ll be making process b a cooperative process with some minor changes as given:

#---------------------------------------- # A cooperative way #---------------------------------------- flock --verbose balance.dat ./update_balance.sh '80'

As you can clearly see, process B waited while the first process was being executed and I got the desired results!

How to Inspect Locks in Linux

You can inspect all locks using lslocks command. And the usage is quite easy. A single command and that’s it:

list every lock in linux

And it will show everything from the command, and type of lock to the path.

Final Words

This guide explained all the basics related to locks in Linux with examples of how you can use advisory Linux for suitable tasks.

And if you have any queries, let me know in the comments.

Источник

Оцените статью
Adblock
detector