PHPflock file lock-exclusive operation test_php tutorial

Source: Internet
Author: User
Tags flock
Test the exclusive operation of PHPflock file locking. In php, most of the operations on files will use file locking to avoid conflict during simultaneous operations by multiple users, the following small series will test the exclusive file lock operation with everyone. some operations on files in php are most likely to use file lock to avoid conflict during simultaneous operations by multiple users, the following is a small Editor to test some examples of exclusive file lock operations.

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:

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

// 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:
$ 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:
$ 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:
$ 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:
$ 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.