Analysis of process lock problem _ PHP Tutorial

Source: Internet
Author: User
Tags flock usleep
Analysis and Research on process lock issues .? Php *** write lock test * open thread 1 * require (file_lock.php); $ locknewFile_Lock (dirname (_ FILE __)). fileLock. lock); ** the lock speed of a single thread is 1. /**
* Write lock test
* Open thread 1
*/
Require ("file_lock.php ");
$ Lock = new File_Lock (dirname (_ FILE _). "/FileLock. lock ");
/** The lock speed of a single thread is 30 thousand times per second. **/
/** Two threads write, 20 thousand of the data is about 7 s */
/** Write data in one thread. the data size of 10 thousand is about 3.9 s. the two files are written at the same time, which is faster */
/** If the lock is not performed, it takes about 2.8 seconds for a process to write data. locking is costly. */
/** Do not lock, the two processes are not evenly distributed, and most of them conflict */
$ Lock-> writeLock ();
$ Lock-> increment ();
$ Lock-> unlock ();
While ($ lock-> get () <2 ){
Usleep (1000 );
}
Sleep (1 );
Echo "begin to runing n ";
$ T1 = microtime (true );
For ($ I = 0; I I <10000; $ I ++)
{
$ Lock-> writeLock ();
$ Lock-> increment (1 );
$ Lock-> unlock ();
}
$ T2 = microtime (true)-$ t1;
Echo $ t2;
?>


Class File_Lock
{
Private $ name;
Private $ handle;
Private $ mode;
Function _ construct ($ filename, $ mode = 'A + B ')
{
Global $ php_errormsg;
$ This-> name = $ filename;
$ Path = dirname ($ this-> name );
If ($ path = '.' |! Is_dir ($ path )){
Global $ config_file_lock_path;
$ This-> name = str_replace (array ("/", ""), array ("_", "_"), $ this-> name );
If ($ config_file_lock_path = null ){
$ This-> name = dirname (_ FILE _). "/lock/". $ this-> name;
} Else {
$ This-> name = $ config_file_lock_path. "/". $ this-> name;
}
}
$ This-> mode = $ mode;
$ This-> handle = @ fopen ($ this-> name, $ mode );
If ($ this-> handle = false ){
Throw new Exception ($ php_errormsg );
}
}
Public function close ()
{
If ($ this-> handle! = Null ){
@ Fclose ($ this-> handle );
$ This-> handle = null;
}
}
Public function _ destruct ()
{
$ This-> close ();
}
Public function lock ($ lockType, $ nonBlockingLock = false)
{
If ($ nonBlockingLock ){
Return flock ($ this-> handle, $ lockType | LOCK_NB );
} Else {
Return flock ($ this-> handle, $ lockType );
}
}
Public function readLock ()
{
Return $ this-> lock (LOCK_SH );
}
Public function writeLock ($ wait= 0.1)
{
$ StartTime = microtime (true );
$ CanWrite = false;
Do {
$ CanWrite = flock ($ this-> handle, LOCK_EX );
If (! $ CanWrite ){
Usleep (rand (10,100 0 ));
}
} While ((! $ CanWrite) & (microtime (true)-$ startTime) <$ wait ));
}
/**
* If you want to log the number under multi-thread system,
* Please open the lock, use a + mod. then fopen the file will not
* Destroy the data.
*
* This function increment a delt value, and save to the file.
*
* @ Param int $ delt
* @ Return int
*/
Public function increment ($ delt = 1)
{
$ N = $ this-> get ();
$ N + = $ delt;
$ This-> set ($ n );
Return $ n;
}
Public function get ()
{
Fseek ($ this-> handle, 0 );
Return (int) fgets ($ this-> handle );
}
Public function set ($ value)
{
Ftruncate ($ this-> handle, 0 );
Return fwrite ($ this-> handle, (string) $ value );
}
Public function unlock ()
{
If ($ this-> handle! = Null ){
Return flock ($ this-> handle, LOCK_UN );
} Else {
Return true;
}
}
}
?>


Http://www.bkjia.com/PHPjc/445007.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445007.htmlTechArticle? Php/*** test of write lock * open thread 1 */require (file_lock.php); $ lock = new File_Lock (dirname (_ FILE __)). /FileLock. lock);/** the lock speed of a single thread...

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.