Flock-lightweight consultation file locking. Boolflock (resource $ handle, int $ operation [, int $ wouldblock]) handle file system pointer, is a typical resource created by fopen ). Operation can be bool flock (resource $ handle, int $ operation [, int & $ wouldblock]) of the following values.
Handle
A file system pointer is a resource typically created by fopen ).
Operation can be one of the following values:
1. LOCK_SH gets the shared Lock (read program ).
2. LOCK_EX gets an exclusive lock (written program.
3. LOCK_UN release lock (whether shared or exclusive ).
4. if you do not want flock () to be blocked during the lock, it is LOCK_NB (not supported on Windows ).
Wouldblock
If the lock is blocked (in case of EWOULDBLOCK error code), the optional third parameter is set to TRUE. (Not supported on Windows)
Return value
Returns TRUE if the call succeeds, or FALSE if the call fails.
Before PHP 5.3.2, the lock will also be released by fclose () (automatically called after the script is completed). to unlock the lock, you must manually perform it (flock ($ fp, LOCK_UN ); // release the lock ).
When a multi-threaded server API (such as ISAPI) is used, flock () may not be used to protect files, because PHP scripts running on other parallel threads in the same server instance can process the file. (?)
Eg: 1
File_get_contents and file sometimes return null when reading a non-empty file, as shown in the following example:
Code2 outputs null during code1 lock. code3 will wait for code1 to release the lock and return the obtained content.
Code1
Code2
Code3
Eg: 2
The running result of the local machine is inconsistent with the following. it can be written in LOCK_SH (???)
The following is an example in the manual.
I just spend a long time to understand why write function returns me "0", on a basic file opening and then writing.
I discovered that if you use LOCK_SH and then you write something, that will not work:
Eg: 3
Write code to solve the problem of multi-process threads reading and writing a file at the same time:
PHP does not have the concept of multithreading. However, we can still simulate multithreading in an "imperfect" way.
In short, it is queue processing. You can lock and unlock files. When a file is operated by a user,
The file is locked, and other users can only wait. it is not perfect, but it can also meet the requirements of some applications.
Http://www.bkjia.com/PHPjc/769340.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/769340.htmlTechArticlebool flock (resource $ handle, int $ operation [, int $ wouldblock]) handle file system pointer, is a resource typically created by fopen ). Operation can be of the following values...