由于要写个多进程同时访问一个文件的功能函数,结果用到这个,记录笔记
<?php function write_file($file,$msg,$usecheck=5){ static $check=1;//这里用到了~ $f=@fopen($file,"a+b"); if($f){ if(flock($f,LOCK_EX)){ fwrite($f,$msg); flock($f,LOCK_UN); fclose($f); }else{ fclose($f); $check++; if($check<=$usecheck){ sleep(1);//暂停下程序,等其他进程释放该资源 write_file($file,$msg); } } }else{ $check++; if($check<=$usecheck){ sleep(1);//暂停下程序,等其他进程释放该资源 write_file($file,$msg); } } }