PHP file lock)

Source: Internet
Author: User
Tags flock

Bool flock (INT handle, int operation [, Int & wouldblock]);
The handle of the flock () operation must be an opened file pointer. Operation can be one of the following values:

  1. To obtain the shared lock (read Program), set operation to lock_sh (PhP 4.0.1 and earlier versions to 1)
  2. To obtain an exclusive lock (Write Program), set operation to lock_ex (set to 2 in versions earlier than PhP 4.0.1)
  3. To release the lock (whether shared or exclusive), set operation to lock_un (set it to 3 in versions earlier than PhP 4.0.1)
  4. If you do not want flock () to be blocked during the lock, add lock_nb to Operation (set it to 4 in PHP versions earlier than 4.0.1)

Create two files
(1) A. php

$file = "temp.txt";    $fp = fopen($file , 'w');    if(flock($fp , LOCK_EX)){         fwrite($fp , "abc\n");         sleep(10);         fwrite($fp , "123\n");        flock($fp , LOCK_UN);    }    fclose($fp);   

(2) B. php

$file = "temp.txt";    $fp = fopen($file , 'r');    echo fread($fp , 100);    fclose($fp);   

After running a. php, run B. php immediately. The output is displayed:
ABC
After a. php is run, run B. php. The output is as follows:
ABC
123
Apparently, when a. php writes a file with a large amount of data, resulting in a long time, B. php reads incomplete data.

Modify B. php:

$file = "temp.txt";    $fp = fopen($file , 'r');    if(flock($fp , LOCK_EX)){        echo fread($fp , 100);        flock($fp , LOCK_UN);    } else{        echo "Lock file failed...\n";    }    fclose($fp);   

After running a. php, run B. php immediately. You can find that B. php will not be displayed until a. php is completed (10 seconds later:
ABC
123
The read data is complete, but it takes too long to wait for the write lock to be released.

Modify B. php:

$file = "temp.txt";    $fp = fopen($file , 'r');    if(flock($fp , LOCK_SH | LOCK_NB)){        echo fread($fp , 100);        flock($fp , LOCK_UN);    } else{        echo "Lock file failed...\n";    }    fclose($fp);   

After running a. php, run B. php immediately. The output is displayed:
Lock file failed...
It proves that the lock file failure status can be returned, rather than waiting for a long time as it goes up.

Conclusion:
We recommend that you select related locks when caching files. Otherwise, the read data may be incomplete or the data may be written repeatedly.
File_get_contents does not seem to be able to select a lock and does not know what locks it uses by default. The reverse is the same as the output obtained by the unlock, which is incomplete data.
I want to cache files, so I only need to know whether a write lock exists. If yes, I can check the database.
Test environment: Linux (Ubuntu 6), PHP 5.1.2, Apache 2

Re-convert:

There are two types of File locks: Shared locks and exclusive locks, that is, lock_sh and write lock_ex)
File locks are generally used as follows:
  1. $ Fp = fopen ("FILENAME", "");
  2. Flock ($ FP, lock_sh) or die ("lock error ")
  3. $ STR = fread ($ FP, 1024 );
  4. Flock ($ FP, lock_un );
  5. Fclose ($ FP );


Note that after fwrite, the file will be updated immediately, instead of waiting for fwrite and then fclose before the file will be updated. This can be checked by reading the file before fclose after fwrite. 

But when will lock_ex be used?

When reading: 
If you do not want dirty data to appear, it is best to use the lock_sh shared lock. You can consider the following three situations:
1. If the shared lock is not applied during read, other programs will write the lock immediately (no matter whether the write is locked or not. If half of the data is read and written by other programs, the last half of the data read may be less than the first half (the first half is before the modification, the last half is modified)
2. if a shared lock is added during read (because it is only read, there is no need to use the exclusive lock), at this time, other programs start to write, this Write Program does not use the lock, then, writing a program will directly modify this file, which will lead to the same problem as above.
3. the ideal situation is that lock_sh is used during read and lock_ex is used during write. In this way, the Write Program will wait until the read program is complete, there will be no hasty operations

When writing: 
If multiple write programs operate on files without locking, some of the final data may be written by program a and some by program B.
If the lock is applied during the write process and other programs are used to read the lock, what will the program read?
1. If the read program does not apply for a shared lock, it will read the data of dirty. For example, to write a program, Write A, B, and C, and write a. At this time, read a and continue to write B. At this time, read AB and then write C, at this time, I read ABC.
2. If the read program has applied for a shared lock before, the read program will read it only after the write program finishes writing ABC and releases the lock.

There is also a well-written blog:

Http://hxsdit.com/1110
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.