Test the exclusive operation for PHP flock File Locking

Source: Internet
Author: User
Tags flock

In php, most of the operations on files will use File Locking to avoid conflicts when multiple users operate at the same time. The following small editor will test some instance analysis of File Locking exclusive operations with everyone.

Flock-lightweight consultation file locking

Flock () function prototype

Bool flock (int handle, int operation [, int & wouldblock])

PHP supports a lightweight method of locking all files in consultation (that is, all access programs must be locked in the same way, otherwise it will not work)


Operation can be one of the following values:

To obtain the shared lock (read Program), set operation to LOCK_SH (PHP 4.0.1 and earlier versions to 1 ).
To obtain an exclusive lock (Write Program), set operation to LOCK_EX (set to 2 in versions earlier than PHP 4.0.1 ).
To release the lock (whether shared or exclusive), set operation to LOCK_UN (3 in PHP versions earlier than 4.0.1 ).
If you do not want flock () to be blocked during the lock, add LOCK_NB to operation (set to 4 in versions earlier than PHP 4.0.1 ).
Flock () allows you to execute a simple read/write model (including most Unix-derived versions and even Windows) that can be used on any platform ). If the lock is blocked (in case of EWOULDBLOCK error code), the optional third parameter is set to TRUE. The lock operation can also be released by fclose () (automatically called when the code is executed ).

If the call succeeds, TRUE is returned. If the call fails, FALSE is returned.

Note:
In Windows, flock () will be executed forcibly. The handle of the flock () operation must be an opened file pointer.
Because flock () requires a file pointer, it may have to use a special lock file to protect access to the file to be opened in write mode (in fopen () add "w" or "w +" to the function ").
Flock () cannot be used in NFS or other network file systems. Flock () does not support old file systems, such as FAT and its derived systems. Therefore, in this environment, FALSE (especially for Windows) is always returned to view the documents of your operating system.
In some operating systems, flock () is implemented at the process level. 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.

Exclusive test:

The two files below are similar. The difference is that the writing is different. Run a first. PHP file, keep it not closed, and then run B. PHP file, and then view the content of the written file, you will find B. the PHP file is not written successfully!

The Code is as follows: Copy code

<? Php
// A. php
If (! ($ F = @ fopen ("flock. log", "AB") exit;
Flock ($ f, LOCK_EX );
While (TRUE)
{
Fwrite ($ f, "");
}
?>

<? Php
// B. php
If (! ($ F = @ fopen ("flock. log", "AB") exit;
Flock ($ f, LOCK_EX );
While (TRUE)
{
Fwrite ($ f, "bn ");
}
?>

For example, we have two files.

Flocka. php

 

The Code is as follows: Copy code
$ File = 'temp.txt ';
$ Fp = fopen ($ file, 'A ');
 
For ($ I = 0; $ I <5; $ I ++)
{
Fwrite ($ fp, "111111n ");
Sleep (1 );
}
 
Fclose ($ fp );

Flockb. php

The Code is as follows: Copy code
$ File = 'temp.txt ';
$ Fp = fopen ($ file, 'A ');
 
For ($ I = 0; $ I <5; $ I ++)
{
Fwrite ($ fp, "22222222n ");
}
 
Fclose ($ fp );

Run flocka. php first, and then run flockb. php immediately.
Result:
11111111
22222222
22222222
22222222
22222222
22222222
11111111
11111111
11111111
11111111
This indicates that when no filelock is applied, the two files write the txt files at the same time.
Next, modify the code of the two PHP files.
Flocka. php

 

The Code is as follows: Copy code
$ File = 'temp.txt ';
$ Fp = fopen ($ file, 'A ');
 
If (flock ($ fp, LOCK_EX ))
{
For ($ I = 0; $ I <5; $ I ++)
{
Fwrite ($ fp, "111111n ");
Sleep (1 );
}
Flock ($ fp, LOCK_UN );
}
Fclose ($ fp );


Flockb. php

The Code is as follows: Copy code
$ File = 'temp.txt ';
$ Fp = fopen ($ file, 'A ');
 
If (flock ($ fp, LOCK_EX ))
{
For ($ I = 0; $ I <5; $ I ++)
{
Fwrite ($ fp, "22222222n ");
}
Flock ($ fp, LOCK_UN );
}
 
Fclose ($ fp );

Run flocka. php and then run flockb. php immediately.
We will find that flockb. php remains in the waiting state until the flocka. php operation is complete. flockb. php will continue to be executed only after the flocka. php operation is complete.
Output result:
11111111
11111111
11111111
11111111
11111111
22222222
22222222
22222222
22222222
22222222
In addition, when flock is executed, the file lock is automatically released.

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.