Wednesday 1 April 2015

tutorial on file lock in php

Hi,
Ever wondered how file locks works? Stuck somewhere in implementing file locks? Don't worry, today I am going to share tutorial which remove all of your worries!


Here I will try to share my knowledge in this area so it may happen I may be wrong. So, Please read discreetly.

What is file locks?

Use:
Suppose you are writing same file using two scripts or running two instance of script. Now there can be case content of one instance get mixed with content of second instance. which is commonly known as dirty write.  What if you wanted to write content in sequential manner say script1 will write first then only other instance/script will write to that file. So, you need some kind of mechanism to check which all function are currently using your file/resource. for this purpose, locks come into the picture. It's the same thing like you go to toilet and you latch the door, so that no one else come and share the same toilet you are using! :P

Types of locks:
Mainly there are two types of locks here.

  1. blocking:
    blocking means if one process got lock on resource and another resource is trying to get lock on the same resource, second process will be blocked(wait) till first process release the lock on the resource
  2. non-blocking:
    same analogy goes with non-blocking with difference is second process will not wait(block) and it will just return false that it could not able to obtain lock over the file.
so it's design choice if you want to wait until you get lock on resource or will just go ahead without getting lock and retry after some time.


How to implement file lock in PHP:
PHP have dedicated function which will lock file. which is flock. You don't have to worry about  implementation as PHP is higher level language.

I will try to incorporate examples to how to use locks in php.

<?php
// Let's create /tmp/ninja-lock1.txt ...
$fp0 = fopen('/tmp/ninja-lock1.txt', 'c');
// ... and close it imiedietly
fclose($fp0);

// File handler $fp0 was closed so flock()
// is unable to use it to gain lock.
// It will fail with wouldblock set to 0
// as it doesn't make sense to wait on unusable file handle.
//
// BTW flock() throws in such case warning "x is not a valid stream resource".
// Just for the purpose of clear output from this example
// I've suppressed it with @ - don't use @ in production
$flockResult = @flock($fp0, LOCK_EX | LOCK_NB, $wouldblock);
printf("flock=%b; wouldblock=%d (Acquire lock: %s)\n", $flockResult, $wouldblock, "failure, broken file handle");

// Two handlers for /tmp/ninja-lock2.txt
// to show flock() blocking result.
$fp1 = fopen('/tmp/ninja-lock2.txt', 'c');
$fp2 = fopen('/tmp/ninja-lock2.txt', 'c'); 

// Nobody is locking on /tmp/ninja-lock2.txt,
// so it will acquire lock and wouldblock will be 0
$flockResult = flock($fp1, LOCK_EX | LOCK_NB, $wouldblock);
printf("flock=%b; wouldblock=%d (Acquire lock: %s)\n", $flockResult, $wouldblock, "success");

// File is locked on $fp1 handle so flock won't acquire lock
// and wouldblock will be 1
$flockResult = flock($fp2, LOCK_EX | LOCK_NB, $wouldblock);
printf("flock=%b; wouldblock=%d (Acquire lock: %s)\n", $flockResult, $wouldblock, "failure, already acquired somewhere else");

// Result:
// $ php flock.php 
// flock=0; wouldblock=0 (Acquire lock: failure, broken file handle)
// flock=1; wouldblock=0 (Acquire lock: success)
// flock=0; wouldblock=1 (Acquire lock: failure, already acquired somewhere else)
?>


so some brief explanation about flock(). Here, LOCK_NB flag is used to let php know that script wants non-blocking lock. so as explained above it will not wait for lock to be removed. parameter $wouldblock contains was the lock got block, then it will have value 1.



2 comments:

  1. Thanks for taking the time to discuss that, I really feel strongly about it and love learning more on that topic. If achievable, as you gain competence, would you mind updating your blog with more information? It is highly helpful for me. ring gears and pinions manufacturers

    ReplyDelete
    Replies
    1. @parminder thanks for your compliment. Just wanted to know what more information you want to update? for this file lock or continuing writing other topic.

      Delete