Skip to content
Related Articles
Open in App
Not now

Related Articles

Perl | File Locking

Improve Article
Save Article
  • Difficulty Level : Hard
  • Last Updated : 26 Jan, 2019
Improve Article
Save Article

File locking, or locking in general, is just one of the various solutions proposed to deal with problems associated with resource sharing.
File locking is a method to preserving the security and integrity of any document. The main motive of file locking is allowing people to commit changes to the document without creating a mess of it. When a file is trying to be changed by two or more users, there could be conflicts that arise.

For example, let us consider a file containing the data of a project. The whole project team is allowed to modify the file. There will be a CGI script coded on the web to do the following.

$file = "project.docx";
$commit = $ENV{'QUERY_INFO'};
open(FILE, "$file"); #opening the document
while() {
  if (m/^$commit$/) {
    print "Change already made\n";
push(@newcommit, $commit);
open(FILE, ">$file");
print ...
print "Commit made!";

Locking of a file is done at the system level, which means that the actual details of applying the lock is not something a user has to worry about. File locks are introduced to set temporary restrictions on certain files to limit how they can be shared among different processes. Depending on the nature of an operation, we come up with two types of locks. The first type is a shared lock, and another one is an exclusive lock. With respect to files, read access can be shared by multiple processes because read access does not result in any changes to the state of the shared resource. Therefore, a consistent view of the shared resource can be maintained.
File locking can be done, in Perl, with the flock command.


flock() accepts two parameters. The first one is the filehandle. The second argument indicates the locking operation required.



Here, OPERATION is a numeric value, it can be either 1, 2, 4, or 8.
These numeric values have different meaning and are used to perform different operations such as:


Perl uses numbers to represent the values:

sub LOCK_SH { 1 } ## set as shared lock
sub LOCK_EX { 2 } ## set as exclusive lock
sub LOCK_NB { 4 } ## set lock without any blocks
sub LOCK_UN { 8 } ## unlock the FILEHANDLE

    A shared lock can be applied when you simply want to read the file, as well as allowing others to read the same file alongside. Shared Lock not only sets a lock, but it checks for the existence of other locks first. This Lock doesn’t cover the existence check for all the locks but only for ‘Exclusive Lock’. If an Exclusive Lock exists, it waits until the lock is removed. After removal, it will get executed as Shared Lock. There might exist multiple Shared Locks at the same time.


    flock(FILE, 1); #from the above code

    An exclusive lock is used when the file is to be used among a group of people, and everyone has got the permission to make a change. Only one exclusive lock will be on a file so that only one user/process at a time makes changes. In an exclusive lock, the rule is “I am the only one.” flock() looks for any other types of locks in the script. If found, it will hold on, till all of them have been removed from the script. At the right moment, it will lock the file.


    flock(FILE, 2); #from the above code

    A non-blocking lock informs the system not to wait for other locks to come off the file. It will return an error message if another lock has been found in the file.


    flock(FILE, 4); #from the above code

    Unblock any specified file- same as the close(FILE) function does.


    flock(FILE, 8); #from the above code

  • Below discussed is a problem which explains the use of flock() function:
    The major problem with the script explained in the above example is when it opens up the file – project.docx, clearing the first file and making it empty.
    Imagine Person A, B, C trying to make a commit at almost the same timing.

    Person A opens the file to check the data. Closes it, and starts to edit the data. (clearing the first file)
    Meanwhile, Person B opens the file to check/read and notices it is completely empty.

    That is a Clash! The whole system will be disrupted.

    This is where file locking is used. Person A, B, C are willing to make a commit to the document. Person A opens up the file, that moment(shared lock is activated), checks out all the data inside the file and closes it again(unlocking the file).
    Person A then wishes to commit changed to the file and re-opens the file to make edits( this is when the Exclusive lock comes into action). While person A is making a change to the file, Neither Person B or C can open up the file to read or write.

    Person A does his work, closes the file (unlocking it).

    Now if Person B had tried to open the file in the meantime, would have got an error -“File being accessed by some other candidate”.
    Thus creating no stepping of toes, and maintaining a good flow of work.

    flock() vs lockf()

    lockf() function is used to lock parts of a file unlike flock() which locks entire files at once. lockf() can not be used in the perl directly whereas Perl supports the flock system call natively but doesn’t applies on network locks.
    Perl also supports fcntl() system call which offers the most locking controls in Perl.

    My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!