Home >Backend Development >PHP Tutorial >Detailed explanation of PHP file lock concurrency operation

Detailed explanation of PHP file lock concurrency operation

小云云
小云云Original
2018-03-31 10:51:202460browse

This article mainly shares with you the detailed explanation of PHP file lock concurrent operation. The default: PHP's file operation function is not in a blocking state, but in a free operation state. I hope it can help everyone.

Conditions:

  • When a script operation is required and another script operation needs to be blocked, a file lock is required.

Lock operation process:

  • Add the lock first, check whether the lock is added successfully, and use it again if it is successful!

Lock type:

  • Read lock: s-Lock (share-lock) shared lock, the expected additional lock before the read operation. As a result, concurrent reads are allowed and additional write operations are blocked.

  • Write lock: x-lock (exclusive-lock) exclusive lock, exclusive lock, try to add the lock type before writing operation. As a result, other scripts cannot read or write.

  • Intention lock: All scripts that operate resources follow a convention to use file locks (convention).

Blocking: refers to that after the previous operation is unlocked, the next operation to lock the file can continue to be executed.

Function:

bool flock ( resource $handle , int $operation [, int &$wouldblock ] )

Type parameters ($operation): LOCK_SH read lock and LOCK_EX write lock

Example:
Use Read lock:
Detailed explanation of PHP file lock concurrency operation
Use write lock:
Detailed explanation of PHP file lock concurrency operation

You can use LOCK_NB to not block when the lock fails:

<?php
$fp = fopen(&#39;/tmp/lock.txt&#39;, &#39;r+&#39;);

/* Activate the LOCK_NB option on an LOCK_EX operation */if(!flock($fp, LOCK_EX | LOCK_NB)) {
    echo &#39;Unable to obtain lock&#39;;
    exit(-1);
}

/* ... */

fclose($fp);

Unlock: in Before PHP 5.3.2, the lock would also be released by fclose() (which will be automatically called after the script ends). Now it can only be unlocked manually through flock($fp,LOCK_UN)

Default: PHP's file operation function is not in a blocking state, but in a free operation state.

Conditions:

  • When a script operation is required and another script operation needs to be blocked, a file lock is required.

Lock operation process:

  • Add the lock first, check whether the lock is added successfully, and use it again if it is successful!

Lock type:

  • Read lock: s-Lock (share-lock) shared lock, the expected additional lock before the read operation. As a result, concurrent reads are allowed and additional write operations are blocked.

  • Write lock: x-lock (exclusive-lock) exclusive lock, exclusive lock, try to add the lock type before writing operation. As a result, other scripts cannot read or write.

  • Intention lock: All scripts that operate resources follow a convention to use file locks (convention).

Blocking: refers to that after the previous operation is unlocked, the next operation to lock the file can continue to be executed.

Function:

bool flock ( resource $handle , int $operation [, int &$wouldblock ] )

Type parameters ($operation): LOCK_SH read lock and LOCK_EX write lock

Example:
Use Read lock:
Detailed explanation of PHP file lock concurrency operation
Use write lock:
Detailed explanation of PHP file lock concurrency operation

You can use LOCK_NB to not block when the lock fails:

<?php
$fp = fopen(&#39;/tmp/lock.txt&#39;, &#39;r+&#39;);

/* Activate the LOCK_NB option on an LOCK_EX operation */if(!flock($fp, LOCK_EX | LOCK_NB)) {
    echo &#39;Unable to obtain lock&#39;;
    exit(-1);
}

/* ... */

fclose($fp);

Unlock: in Before PHP 5.3.2, the lock would also be released by fclose() (which would be automatically called after the script ends). Now it can only be unlocked manually through flock($fp,LOCK_UN).

Related recommendations:

How PHP uses file locks to solve high concurrency problems

php uses file locks to solve high concurrency problems

How to solve high concurrency in php

The above is the detailed content of Detailed explanation of PHP file lock concurrency operation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn