PHP Flock file Lock exclusive operation Test _php tutorial

Source: Internet
Author: User
Tags flock
In PHP for the operation of the file we will probably use file locking to avoid the simultaneous operation of multi-user conflict, the following small series with you to test the file lock exclusive operation some examples of analysis.

flock– Lightweight consultation document lock-up

Flock () function prototype

BOOL Flock (int handle, int operation [, int &wouldblock])

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)


Operation can be one of the following values:

To get a shared lock (read program), set operation to Lock_sh (the previous version of PHP 4.0.1 is set to 1).
To obtain an exclusive lock (written program), set operation to LOCK_EX (2 in previous versions of PHP 4.0.1).
To release the lock (either share or exclusive), set operation to Lock_un (3 in previous versions of PHP 4.0.1).
If you do not want flock () to block when locked, Operation plus LOCK_NB (set to 4 in previous versions of PHP 4.0.1).
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). If the lock is blocked (ewouldblock error code case), the optional third parameter is set to TRUE. The lock operation can also be released by Fclose (), which is also automatically called when the code is finished executing.

Returns TRUE if successful, and FALSE if it fails.

Attention:
Under Windows flock () will be enforced. The handle of the flock () operation must be a file pointer that has already been opened.
Because Flock () requires a file pointer, you may have to use a special lock file to protect access to the files that you intend to open through write mode (add "w" or "w+" in the fopen () function).
Flock () cannot be used for NFS and some other network file systems. Flock () does not support legacy file systems, such as FAT and its derived systems. Therefore, this environment always returns FALSE (especially for Windows) details to view the documentation for your operating system.
Flock () is implemented at the process level in some operating systems. When using a multithreaded server API (such as ISAPI), you may not be able to rely on flock () to protect files, because PHP scripts that run on other parallel threads in the same server instance can process the file.

Exclusive testing:

The following two files are similar, the difference is that the writing is not the same, first run the a.php file, keep it closed, then run the b.php file, and then go to view the contents of the file written, you will find that the contents of the b.php file is not written successfully!

The code is as follows Copy Code

a.php
if (! ($f = @fopen ("Flock.log", "AB"))) Exit
Flock ($f, LOCK_EX);
while (TRUE)
{
Fwrite ($f, "an");
}
?>

b.php
if (! ($f = @fopen ("Flock.log", "AB"))) Exit
Flock ($f, LOCK_EX);
while (TRUE)
{
Fwrite ($f, "bn");
}
?>

For example, we have two files, as follows.

flocka.php

The code is as follows Copy Code
$file = ' temp.txt ';
$fp = fopen ($file, ' a ');

for ($i = 0; $i < 5; $i + +)
{
Fwrite ($fp, "11111111n");
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, then run flockb.php immediately.
Results:
11111111
22222222
22222222
22222222
22222222
22222222
11111111
11111111
11111111
11111111
Note When no file lock is added, two files write to the TXT file at the same time.
Modify the code for the two PHP files below.
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, "11111111n");
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);

Also run flocka.php first, and then run flockb.php immediately.
You will find that flockb.php is waiting until the end of the flocka.php run, and flockb.php will not continue until the flocka.php is finished.
Output Result:
11111111
11111111
11111111
11111111
11111111
22222222
22222222
22222222
22222222
22222222
In addition, the file lock is automatically released when the flock is executed

http://www.bkjia.com/PHPjc/632777.html www.bkjia.com true http://www.bkjia.com/PHPjc/632777.html techarticle in PHP for the operation of the file we will probably use file locking to avoid the simultaneous operation of multi-user conflict, the following small series with you to test the file lock exclusive operation some ...

  • 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.