Linux process locking file

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.

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.

Читайте также:  Linux user directory access

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.

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:

Читайте также:  Linux get disk type

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.

Источник

How to Lock a Text File in Linux Using flock Command

Before we explore the techniques/approaches of locking a text file under a Linux operating system environment, we should first understand the logic behind Linux’s file locking mechanism.

Linux’s file locking mechanism restricts/controls file access among multiple processes. When a text file is successfully locked, only one process can access it on a specific time schedule.

Before proceeding with this article, please understand that file locking is very much different from file encryption or file access control where a passphrase or password is needed to control user access to your files.

When a file is locked under a Linux operating system environment, a mutual exclusion event is created where only a single process can access it at a time.

Problem Statement

The Linux operating system will automatically block any write action attempts directed to an open file being written on by another process. However, what if you want to revoke the read and/or write permissions already invoked by the first process that has your file open and in write mode? Is there a workaround to this problem?

Moreover, we might also want to lock our file so that no other process interferes or attempt to disrupt the write mode status already initiated. This article will walk us through a viable solution to lock a text file in Linux.

Sample Reference File

For this tutorial to be more engaging and worthwhile, we should have some reference text files. We will concentrate on the text files inside the following directory:

Sample Text Files

For instance, let us open the file output.txt and start writing on it.

Write Content to File

While this file is still open, let us try to open it from another command line tab.

You will get the following response:

File is being edited by root

The above screen capture relays the PID (133110) of the process of working on the text file with the option of opening the file while it is still in that process’s write mode.

By keying in Y from our keyboard, we will have opened this text file and handed it over to a different process with exclusive write mode access.

Читайте также:  Полезные команды линукс терминала

Open Text Files

Therefore, a file modification by user 1 will lead to the following prompt on user 2 while attempting to save the file.

File was modified

This instance is a perfect representation of two different users on the network working on a single file.

Lock a Text File Using Linux’s flock Utility

To solve this issue, we need the aid of Linux’s flock utility. Since the util-linux package hosts flock command, it should be available in almost all Linux OS distributions. This command manages specific file/directory locks via the Linux command line environment.

To lock a text file in Linux, we will reference the following syntax:

$ flock -x PATH_TO_FILE_TO_LOCK -c COMMAND

The -x option is for obtaining a write lock to the targeted file. Let us attempt to lock the sample output.txt text file. The -c option will enable us to pass a single Linux supported command e.g. cat command.

$ flock -x /home/dnyce/LinuxShellTips_Files/output.txt -c cat

The terminal instance above will remain active to signify that the text file has been locked.

If we open another terminal (while this terminal instance is still running) and execute another flock command on this same file, we should be denied access to it.

$ flock -w .007 /home/dnyce/LinuxShellTips_Files/output.txt -c echo; /bin/echo $? 1 

The -w option is used to relay the wait time of .007 seconds before a lock is placed on the text file. We then execute echo $? to output the exit status of this command.

An exit status of 0 implies that the command was executed successfully and an exit status of 1 implies the command could not be executed due to an error. In this case, the text file is under lock by another process.

To further confirm that the text file is under lock, we can use the lslocks commands to list all active local system locks.

List File Locks in Linux

As you can see, our file is present.

Unlocking a Text File in Linux

Canceling the initial flock command (Ctrl+c) or closing the text file should release the lock making it possible to successfully run the following command on the secondary terminal.

$ flock -w .007 /home/dnyce/LinuxShellTips_Files/output.txt -c echo; /bin/echo $?

Unlocking File in Linux

The exit status of 0 implies that the lock is no longer applicable to the text file.

We have learned how to lock a text file in Linux so that only one process can use it (write mode) at a time. The implementation of this article is particularly useful when different users on a network are accessing a single file.

More on flock command can be found on its man page.

Источник

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