概述
$file = fopen("test.txt", $fileOpenMode); flock($file, $lockMode) or die("Can't lock"); //... flock($file, LOCK_UN); fclose($file);
$fileOpenMode:"r"(读);"r+"(写)(不能用"w",否则由于先打开再加锁,打开后文件内容已经清空了)
$lockMode:LOCK_SH(读);LOCK_EX(写)
<?php sleep(3); return; $mode = $_GET['mode']; switch($mode){ case "r": $fileOpenMode = "r"; $lockMode = LOCK_SH; $sleepTime = 3; break; case "w": $fileOpenMode = "r+"; $lockMode = LOCK_EX; $sleepTime = 5; break; } echo "QueryTime:" . time() . "<br>"; $startTime = time(); echo "Mode:" . $mode . "<br>"; $file = fopen("test.txt", $fileOpenMode); flock($file, $lockMode) or die("Can't lock"); sleep($sleepTime); switch($mode){ case "r": echo "Data:" . fgets($file) . "<br>"; break; case "w": echo "Data:" . "(Written)" . "<br>"; fputs($file, "Write data info file at " . time()); break; } flock($file, LOCK_UN); fclose($file); echo "Time:" . (time()-$startTime) . "<br>"; echo "Now:" . time() . "<br>"; ?>
转载于:https://www.cnblogs.com/turtlegood/p/4355721.html
最后
以上就是深情楼房为你收集整理的php文件读写锁的全部内容,希望文章能够帮你解决php文件读写锁所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复