This requirement is a common locking in the software design process.
Computer students should know that this is in the "Computer operating system" course has this knowledge point.
The main consideration is the process synchronization, that is, the process mutually exclusive access to the resource.
OK, speak with the program!
<?php
function WriteFile ($filename, $data) {
$fp = fopen ($filepath, ' a '); Opens the file as an append, and returns a pointer
do{
Usleep (100); Suspends execution of the program with parameters in microseconds
}while (!flock ($fp, LOCK_EX)); Locks the file in exclusive write mode, returns true if successful, otherwise false
}
$res = fwrite ($fp, $data. " /n "); Writes data to an open file in an append way
Flock ($FP, lock_un); Unlock to allow other processes to lock
Fcloce ($FP); Close the Open file pointer
return $res; Return write results
?>
OK, the code is not too much! This is not a code, it is important to understand the meaning of each function, as well as the meaning of each statement.