Before the interview a lot of time remember to mention a file lock such a concept. I didn't know what it was. Work today, encountered a database lock, MC Lock, today and occasionally found in PHP's own file lock. Flock
http://php.net/manual/zh/function.flock.php
BOOL Flock (resource $handle, int $operation [, int & $wouldblock])
Flock () allows the execution of a simple read/write model that can be used on any platform (including most Unix-derived versions and even Windows).
Before PHP 5.3.2, locks are also released (automatically called after the script ends) by fclose ().
PHP supports a lightweight method of locking all files in a consultative manner (that is, all access programs must be locked in the same way, otherwise it will not work). By default, this function blocks to acquire locks, which can be controlled (on non-Windows platforms) by the LOCK_NB option in the following document.
Handle
A file system pointer, which is a resource (resource) typically created by fopen ().
Operation
Operation can be one of the following values:
Lock_sh Get a shared lock (read program).
LOCK_EX obtains an exclusive lock (written program.
Lock_un release Lock (whether shared or exclusive).
If you do not want flock () to block when locked, it is LOCK_NB (not yet supported on Windows).
Wouldblock
If the lock is blocked (ewouldblock error code case), the optional third parameter is set to TRUE. (Not supported on Windows)
return value?
Returns TRUE on success, or FALSE on failure.
<?php
$fp = fopen ('./a.lock ', ' R ');
if (Flock ($FP, lock_ex)) {
echo Date ("Y-m-d h:i:s", Time ());
Sleep (10);
Flock ($FP, lock_un);
}else {
The echo "LOCKing ...";
}
Fclose ($FP);
This is the exclusive lock, who grabbed it, don't just block the wait to end.
<?php
$fp = fopen ('./a.lock ', ' R ');
if (Flock ($fp, LOCK_EX | LOCK_NB)) {
echo Date ("Y-m-d h:i:s", Time ());
Sleep (10);
Flock ($FP, lock_un);
}else {
The echo "LOCKing ...";
}
Fclose ($FP);
If you do not want to block the wait, you can return the contents of else directly with LOCK_NB
<?php
$fp = fopen ('./a.lock ', ' R ');
if (Flock ($FP, lock_sh)) {
echo Date ("Y-m-d h:i:s", Time ());
Sleep (10);
Flock ($FP, lock_un);
}else {
The echo "LOCKing ...";
}
Fclose ($FP);
Here is the shared lock, can read and write together ...
PHP file Lock