Php file lock)

Source: Internet
Author: User
Tags flock
Php file lock (convert) 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:

To obtain a shared Lock (read program), set operation to LOCK_SH (PHP 4.0.1 earlier versions to 1) to obtain an exclusive lock (write program ), set operation to LOCK_EX (set to 2 in PHP versions earlier than 4.0.1) to release the lock (whether shared or exclusive ), set operation to LOCK_UN (set to 3 in PHP versions earlier than 4.0.1). If you do not want flock () to be blocked during lock, add LOCK_NB to operation (set 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:

$ Fp = fopen ("filename", "a"); flock ($ fp, LOCK_SH) or die ("lock error") $ str = fread ($ fp, 1024 ); flock ($ fp, LOCK_UN); 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

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.