Test A
$begin= Time(); for($i= 0;$i<10000;$i++) { $fp=fopen("tmp", ' r+ '); fseek($fp, 0,seek_end); fwrite($fp,str_repeat($argv[1],1024*32].Php_eol); fclose($fp);}$end= Time();Echo"Time use:". ($end-$begin).Php_eol;
PHP write.php b
PHP write.php A
WC-L tmp
10450 tmp
Test results:
Does not match the expected 2W line
Error Analysis:
Seek navigates to the end of the file, possibly because the other process has already written to the file, overwriting the current process
Test Two
$begin= Time(); for($i= 0;$i<10000;$i++) { $fp=fopen("tmp", ' A + '); fwrite($fp,str_repeat($argv[1],1024*32].Php_eol); fclose($fp);}$end= Time();Echo"Time use:". ($end-$begin).Php_eol;
PHP write.php b
PHP write.php A
WC-L tmp
20000 tmp
Test results:
Matches the expected 2W line, but checks the file contents
Check the script to check whether a row contains both A and b
<?PHP$fp=fopen("tmp", ' r+ '); while(!feof($fp)) { $line=fgets($fp, 1024*1024); if(strstr($line, ' a ') &&strstr($line, ' B ')) { Echo' Not pass '.Php_eol; for($i= 0;$i<strlen($line);$i++){ Echo Ord($line[$i]).Php_eol; } die; }}Echo' Pass '.Php_eol;
PHP check.php >ts
TS Content
Not pass9797...989810
Error Analysis:
PHP's fwrite is buffer, the content of writing a row is larger than the length of buffer, process A and process B are calling write to the same line in turn, resulting in this result
Test Three
Sequential Write
$begin= Time();$fp=fopen("tmp", ' w+ '); for($i= 0;$i<200000;$i++) { fwrite($fp,str_repeat($argv[1],1024*32].Php_eol);}fclose($fp);$end= Time();Echo"Time use:". ($end-$begin).Php_eol;
PHP write.php A
Time Use:13
$begin= Time(); for($i= 0;$i<200000;$i++) { $fp=fopen("tmp", ' A + '); fwrite($fp,str_repeat($argv[1],1024*32].Php_eol); fclose($fp);}$end= Time();Echo"Time use:". ($end-$begin).Php_eol;
PHP write.php A
Time Use:16
PHP multi-process Write file