PHP file Lock

Source: Internet
Author: User
Tags echo date flock

Before the interview a lot of time remember to mention a file lock such a concept. I didn't know what it was. Work today, encountered a database lock, MC Lock, today and occasionally found in PHP's own file lock. Flock

http://php.net/manual/zh/function.flock.php

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

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

Before PHP 5.3.2, locks are also released (automatically called after the script ends) by fclose ().

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). By default, this function blocks to acquire locks, which can be controlled (on non-Windows platforms) by the LOCK_NB option in the following document.

Handle
A file system pointer, which is a resource (resource) typically created by fopen ().

Operation
Operation can be one of the following values:

Lock_sh Get a shared lock (read program).
LOCK_EX obtains an exclusive lock (written program.
Lock_un release Lock (whether shared or exclusive).
If you do not want flock () to block when locked, it is LOCK_NB (not yet supported on Windows).

Wouldblock
If the lock is blocked (ewouldblock error code case), the optional third parameter is set to TRUE. (Not supported on Windows)

return value?

Returns TRUE on success, or FALSE on failure.

<?php
$fp = fopen ('./a.lock ', ' R ');
if (Flock ($FP, lock_ex)) {
echo Date ("Y-m-d h:i:s", Time ());
Sleep (10);
Flock ($FP, lock_un);
}else {
The echo "LOCKing ...";
}
Fclose ($FP);

This is the exclusive lock, who grabbed it, don't just block the wait to end.

<?php
$fp = fopen ('./a.lock ', ' R ');
if (Flock ($fp, LOCK_EX | LOCK_NB)) {
echo Date ("Y-m-d h:i:s", Time ());
Sleep (10);
Flock ($FP, lock_un);
}else {
The echo "LOCKing ...";
}
Fclose ($FP);

If you do not want to block the wait, you can return the contents of else directly with LOCK_NB

<?php
$fp = fopen ('./a.lock ', ' R ');
if (Flock ($FP, lock_sh)) {
echo Date ("Y-m-d h:i:s", Time ());
Sleep (10);
Flock ($FP, lock_un);
}else {
The echo "LOCKing ...";
}
Fclose ($FP);

Here is the shared lock, can read and write together ...

PHP file Lock

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.