How PHP uses flock to implement file locking
This article describes the example of how PHP uses flock to implement file locking. Share it with everyone for your reference. The specific analysis is as follows:
The explanation of flock in the official documentation is: flock() allows you to implement a simple read/write model that can be used on any platform (including most Unix derivatives and even Windows). If the lock will block (in case of EWOULDBLOCK error code), set the optional third parameter to TRUE. The lock operation can also be released by fclose() (also called automatically when the code completes execution).
Simply put, it is to lock a file so that multiple processes are restricted from accessing the file, thereby preventing conflicts. For example:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
|
$file = fopen("test.txt","w ");
if (flock($file,LOCK_EX))
{
fwrite($file,"Write something");
flock($file,LOCK_UN);
}
else
{
echo "Error locking file!";
}
fclose($file);
?>
|
1
2
3
4
5
6
7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
function writeData($path, $mode,$data)
{
$fp = fopen($path, $mode);
$retries = 0;
$max_retries = 100;
do{
if ($retries > 0)
{
usleep(rand(1, 10000));
}
$retries = 1;
}while (!flock($fp, LOCK_EX) and $retries<= $max_retries);
if ($retries == $max_retries)
{
return false;
}
fwrite($fp, "$datan");
flock($fp, LOCK_UN);
fclose($fp);
return true;
}
|
8
9
10
11
12
13
|
$file = fopen("test.txt","w ");
if (flock($file,LOCK_EX))
{
<🎜>flock($file,LOCK_UN);<🎜>
<🎜>}<🎜>
<🎜>else<🎜>
<🎜>{<🎜>
<🎜>echo "Error locking file!";<🎜>
<🎜>}<🎜>
<🎜>fclose($file);<🎜>
<🎜>?>
|
Description:
1. The meaning of this code is to open the file test.txt in read-write mode. When a user calls the php page, the test.txt file is operated, and flock($file,LOCK_EX is executed) ) code, that is, the test.txt file is exclusively locked (the file can only be read and written by this user), then if other new users want to access the file, they will be blocked until the former closes the file (releases the lock) .
2. If the code is changed to flock($file, LOCK_EX LOCK_NB), it means that an error is returned directly when locking. Then if a new user accesses the file, "Error locking file!" will be output.
3. The syntax of this function is flock(file, lock, block), where file is required. Specifies an open file to be locked or released. lock required. Specifies which lock type to use. block is optional. If set to 1 or true, blocks other processes while locking.
For example: Please write a piece of PHP code to ensure that multiple processes can write the same file successfully at the same time
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
function writeData($path, $mode,$data)
{
$fp = fopen($path, $mode);
$retries = 0;
$max_retries = 100;
do{
if ($retries > 0)
{
usleep(rand(1, 10000));
}
$retries = 1;
}while (!flock($fp, LOCK_EX) and $retries<= $max_retries);
if ($retries == $max_retries)
{
return false;
}
fwrite($fp, "$datan");
flock($fp, LOCK_UN);
fclose($fp);
return true;
}
|
I hope this article will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/1025313.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1025313.htmlTechArticleHow PHP uses flock to implement file locking. This article describes how PHP uses flock to implement file locking. Share it with everyone for your reference. The specific analysis is as follows: flock in the official document...