Work gap a class of file locks is written to solve some of the concurrency problems.
BOOL Flock (int handle, int operation [, int &wouldblock]);
The handle of the flock () operation must be a file pointer that is already open. Operation can be one of the following values:
To obtain a shared lock (reader), set operation to Lock_sh (PHP 4.0.1 Previous version to 1)
To obtain an exclusive lock (writer), set operation to LOCK_EX (2 in the previous version of PHP 4.0.1)
To release the lock (either shared or exclusive), set operation to Lock_un (3 in the previous version of PHP 4.0.1)
If you do not want flock () to jam when locked, give operation plus LOCK_NB (set to 4 in previous versions of PHP 4.0.1)
Build 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, you can see the output:
Abc
After a.php run b.php, you can see the output:
Abc
123
Obviously, when the a.php write the file is too large, resulting in a long time, when the b.php read data is incomplete
Modify b.php to:
$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 the a.php, run b.php immediately, you can find that b.php will wait until a.php run complete (that is, 10 seconds later) to display:
Abc
123
Read the data intact, but the time is too long and he waits for the write lock to be released.
Modify b.php to:
$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, you can see the output:
Lock file failed ...
Proof can return a lock file failure status, rather than to wait a long time.
Conclusion:
When recommending a file cache, select the associated lock, or it may result in incomplete read data, or write data repeatedly.
File_get_contents seems to choose not to lock, do not know what he uses the default lock, anyway, and do not lock the output, is incomplete data.
I want to do file caching, so only need to know whether there is a write lock exists, and some words to check the database on it.
Test environment: Linux (Ubuntu 6), PHP 5.1.2, Apache 2
Turn again:
There are two types of file locks: shared and exclusive, read-lock (LOCK_SH) and write-Lock (LOCK_EX)
File locks are generally used in this way:
$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 the fwrite, the file is updated immediately, instead of waiting for fwrite then fclose the file will be updated, which can be checked by reading the file before fclose after fwrite
But when do you use LOCK_EX when using Lock_sh?
Read the time:
If you do not want to appear dirty data, it is best to use lock_sh shared locks. The following three scenarios can be considered:
1. If there is no shared lock on the reading, then the other program to write (whether this write is lock or unlocked) will immediately write success. If you just read half of it and then write it to another program, then the second half of the reading is probably not right (the first half is modified and the last half is modified)
2. If the read time added a shared lock (because only read, there is no need to use exclusive locks), this time, other programs began to write, the program does not use the lock, then write the program will directly modify the file, will lead to the same problem as before
3. Ideally, the time to read lock (LOCK_SH), the time to write a lock (LOCK_EX), so that the writing program will wait until the completion of the program to do the operation, and will not appear rashly operation of the situation
When it is written:
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 B program written
What would he read if it was written with a lock and other programs to read at this time?
1. If the reader does not apply for a shared lock, he will read the dirty data. For example, write a program to write a,b,c three parts, finish A, this time read read is a, continue to write B, this time read is AB, then write C, this time read the ABC.
2. If the read program has previously applied for a shared lock, then the reading program will write the ABC and release the lock before it is read.
<?php
/**
* Used to solve PHP in the concurrency of the lock control, different locks in parallel between the execution, similar to the MySQL InnoDB row-level locks
*/
Class filelock{
File Lock Store Path
Private $path = ';
File handle
Private $fp = ';
Lock file
Private $lockFile = ';
/**
* Constructor
* @param storage directory for string $path locks
* @param string $name lock KEY
*/
Public function __construct ($name, $path = ') {
if (empty ($path)) $this->path=dirname (__file__). ' /';
else $this->path= $path;
$this->lockfile= $this->path.md5 ($name). Lock ';
}
/**
* Add lock
*/
Public Function Lock () {
$this->fp=fopen ($this->lockfile, ' A + ');
if ($this->fp===false) {
return false;
}
Return flock ($this->fp,lock_ex);//Acquire exclusive lock
}
/**
* Unlock
*/
Public function unlock () {
if ($this->fp!==false) {
@flock ($this->fp,lock_un);
Clearstatcache ();
}
@fclose ($this->FP);
@unlink ($this->lockfile);
}
}