Home  >  Article  >  Backend Development  >  PHP advisory file locking: flock_PHP tutorial

PHP advisory file locking: flock_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:10:021255browse

Recently I was studying PHP for work and encountered a problem. I used the following code to lock a file handle


[php]
$filename = "/tmp/lock.txt";

$fp = fopen($filename, "r+");
if (!$fp) {
Die("open failed.");
}

if (flock($fp, LOCK_EX)) { // perform exclusive locking
sleep(20);
$count = (int)fgets($fp);
$count += 1;
fseek($fp, 0);
fwrite($fp, (string)$count);
fflush($fp); // flush output before releasing the lock
flock($fp, LOCK_UN); // Release lock
} else {
echo "Couldn't get the lock!";
}

fclose($fp);
?>

$filename = "/tmp/lock.txt";

$fp = fopen($filename, "r+");
if (!$fp) {
Die("open failed.");
}

if (flock($fp, LOCK_EX)) { // Perform exclusive lock
Sleep(20);
$count = (int)fgets($fp);
$count += 1;
fseek($fp, 0);
fwrite($fp, (string)$count);
fflush($fp); // flush output before releasing the lock
flock($fp, LOCK_UN); // Release lock
} else {
echo "Couldn't get the lock!";
}

fclose($fp);
?>
Access, and then try to use vi to edit /tmp/lock.txt within 20 seconds of sleep, and find that the file content can be successfully modified without waiting for the end of the first script. After poring over the documentation, I found that there is a concept here called "consultative file locking", which means that all accessing programs must use the same lock method to take effect, otherwise it will not work.

Try to use the following code to access within 20 seconds:


[php]
$filename = "/tmp/lock.txt";

$fp = fopen($filename, "r+");
if (!$fp) {
Die("open failed.");
}

if (flock($fp, LOCK_EX)) { // perform exclusive locking
$count = (int)fgets($fp);
echo $count;
$count += 1;
flock($fp, LOCK_UN); // Release lock
} else {
echo "Couldn't get the lock!";
}

fclose($fp);
?>

$filename = "/tmp/lock.txt";

$fp = fopen($filename, "r+");
if (!$fp) {
Die("open failed.");
}

if (flock($fp, LOCK_EX)) { // Perform exclusive lock
$count = (int)fgets($fp);
echo $count;
$count += 1;
flock($fp, LOCK_UN); // Release lock
} else {
echo "Couldn't get the lock!";
}

fclose($fp);
?>

The blocking was found to be successful (the second script needs to wait for the first script to end before it can continue running).

So, what is locking in the same way?

Group A:


1. Try to change the flock parameter of script 1 to LOCK_SH, and the script 2 remains unchanged. After testing, it is found that the blocking is still successful;

2. Try changing the flock parameters of both script 1 and script 2 to LOCK_SH, and find that script 2 can return results without waiting for script 1 to finish executing;

Group B:


1. Try to change the fopen parameter of script 2 to "r", the phenomenon is the same as that of group A;

Group C:

1. Try to change the flock parameter of script 2 to LOCK_SH, while script 1 remains unchanged.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477596.htmlTechArticleRecently I was studying PHP for work and encountered a problem. I used the following code to lock a file handle [php ] ?php $filename = /tmp/lock.txt; $fp = fopen($filename, r+); if...
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