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:
Use write lock:
You can use LOCK_NB to not block when the lock fails:
<?php $fp = fopen('/tmp/lock.txt', 'r+'); /* Activate the LOCK_NB option on an LOCK_EX operation */if(!flock($fp, LOCK_EX | LOCK_NB)) { echo 'Unable to obtain lock'; 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:
Use write lock:
You can use LOCK_NB to not block when the lock fails:
<?php $fp = fopen('/tmp/lock.txt', 'r+'); /* Activate the LOCK_NB option on an LOCK_EX operation */if(!flock($fp, LOCK_EX | LOCK_NB)) { echo 'Unable to obtain lock'; 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!