How to use PHP file lock? PHP file lock Getting Started tutorial

Source: Internet
Author: User
Tags flock fread php write
    1. $file = "Temp.txt";
    2. $fp = fopen ($file, ' w ');
    3. if (Flock ($FP, lock_ex)) {
    4. Fwrite ($fp, "abc\n");
    5. Sleep (10);
    6. Fwrite ($fp, "123\n");
    7. Flock ($FP, lock_un);
    8. }
    9. Fclose ($FP);
Copy Code

2) b.php

    1. $file = "Temp.txt";
    2. $fp = fopen ($file, ' R ');
    3. Echo fread ($fp, 100);
    4. Fclose ($FP);
Copy Code

After running the a.php, run b.php immediately, you can see the output: ABC and other a.php run after running b.php, you can see the output: abc123 obviously, when the a.php write files when the data is too large, resulting in a long time, then b.php read data is not complete above methods in the PHP text Piece Lock Write instance tutorial has the related introduction, does not understand the friend, may refer to the article in the example deep study.

Modify the b.php to:

    1. $file = "Temp.txt";
    2. $fp = fopen ($file, ' R ');
    3. if (Flock ($FP, lock_ex)) {
    4. Echo fread ($fp, 100);
    5. Flock ($FP, lock_un);
    6. } else{
    7. echo "Lock file failed...\n";
    8. }
    9. Fclose ($FP);
Copy Code

After running a.php, run b.php immediately, you can find b.php will wait until the completion of the a.php run (that is, 10 seconds) before the display: abc123 read the data intact, but the time is too long, he will wait for the write lock release.

Modify the b.php to:

    1. $file = "Temp.txt";
    2. $fp = fopen ($file, ' R ');
    3. if (Flock ($fp, Lock_sh | LOCK_NB)) {
    4. Echo fread ($fp, 100);
    5. Flock ($FP, lock_un);
    6. } else{
    7. echo "Lock file failed...\n";
    8. }
    9. Fclose ($FP);
Copy Code

After running a.php, run b.php immediately, you can see the output: Lock file failed ... Prove that you can return the lock file failure status, not as long as you want to.

Conclusion: It is suggested that when the file is cached, the relevant lock will be selected, otherwise the reading data may be incomplete or the data will be written repeatedly. File_get_contents seems to choose not to lock, do not know what his default lock, anyway, and the output is not locked, is incomplete data. I want to do the file cache, so just need to know if there is a write lock exists, and some of the database can be checked. Test environment: Linux (Ubuntu 6), PHP 5.1.2, Apache 2

Re-rotating: There are two types of file locks: shared and exclusive, that is, read-lock (LOCK_SH) and write-lock (lock_ex) file locks are generally used:

    1. $fp = fopen ("filename", "a");
    2. Flock ($FP, lock_sh) or Die ("LOCK error")
    3. $str = Fread ($fp, 1024);
    4. Flock ($FP, lock_un);
    5. Fclose ($FP);
Copy Code

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

But when to use LOCK_EX when to use Lock_sh?

One, read: If you do not want to appear dirty data, it is best to use lock_sh shared lock. There are three scenarios to consider: 1, if you do not add a shared lock when reading, then other programs to write (whether this write is locking or not lock) will immediately write success. If just half of the reading, and then by other programs to write, then half of the reading can be half right (the first half is modified, the latter half is modified) 2, if the read time with a shared lock (because just read, there is no need to use exclusive lock), this time, other programs began to write, This write program does not use the lock, then write the program will directly modify the file, it will also lead to the same problem 3, the most ideal situation is, read time locking (LOCK_SH), write the time also to lock (LOCK_EX), so that the program will wait to read the program after the completion of the operation, Without any hasty operation.

Second, when writing: If multiple write programs do not lock the file at the same time, then the final data may be part of a program written, part of the B program written if the time of the write lock, this time there are other programs to read, then he will read something? 1, if the reader does not apply for a shared lock, then he will read the dirty data. For example, write a program to write a,b,c three parts, write a, this time read a, continue to write B, at this time read the AB, and then write C, this time read ABC. 2, if the reader program before the application of a shared lock, then read the program will wait until the write program will write the ABC and release the lock before reading.

The above is about the PHP file lock introductory tutorial, through a comparative analysis of the use of PHP file lock, I hope to be helpful to everyone.

  • 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.