Home  >  Article  >  Backend Development  >  flock — lightweight advisory file locking_PHP tutorial

flock — lightweight advisory file locking_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:29:45991browse

bool flock ( resource $handle , int $operation [, int &$wouldblock ] )

handle
The file system pointer is a resource typically created by fopen().

operation can be one of the following values:
1. LOCK_SH obtains the shared lock (reading program).
2. LOCK_EX Obtains an exclusive lock (writing program.
3. LOCK_UN Releases the lock (whether shared or exclusive).
4. If you do not want flock() to block when locking, it is LOCK_NB ( Not yet supported on Windows).

wouldblock
If the lock would block (in case of EWOULDBLOCK error code), the optional third parameter will be set to TRUE (not supported on Windows)<.>
Return value
Returns TRUE on success, or FALSE on failure.


Prior to PHP 5.3.2, the lock will also be released by fclose() (in the script). will be called automatically after completion), now unlocking must be done manually (flock ( $fp , LOCK_UN ); // Release the lock).
When using a multi-threaded server API (such as ISAPI), flock( may not be relied on. ) to protect the file because PHP scripts running in other parallel threads in the same server instance can process the file (?)


eg:1
file_get_contents and file sometimes read a. If the file is not empty, it will return empty. For example,
During the locking period of code1, the output of code2 is empty. Code3 will wait for code1 to release the lock and return the obtained content
code1

<?<span>php
</span><span>$fo</span> = <span>fopen</span>('abc.txt', 'r+'<span>);
</span><span>flock</span>(<span>$fo</span>,<span> LOCK_EX);
</span><span>sleep</span>(10<span>);
</span><span>flock</span>(<span>$fo</span>, LOCK_UN);

code2

<?<span>php
</span><span>var_dump</span>(<span>file_get_contents</span>('abc.txt'<span>));
</span><span>var_dump</span>(<span>file</span>('abc.txt'));
code3

<?<span>php
</span><span>$con</span> = getContents('abc.txt'<span>);
</span><span>print_r</span>(<span>$con</span><span>);

</span><span>function</span> getContents(<span>$path</span>, <span>$waitIfLocked</span> = <span>true</span><span>) {
    </span><span>if</span>(!<span>file_exists</span>(<span>$path</span><span>)) {
        </span><span>throw</span> <span>new</span> <span>Exception</span>('File "'.<span>$path</span>.'" does not exists'<span>);
    }
    </span><span>else</span><span> {
        </span><span>$fo</span> = <span>fopen</span>(<span>$path</span>, 'r'<span>);
        </span><span>$locked</span> = <span>flock</span>(<span>$fo</span>, LOCK_SH, <span>$waitIfLocked</span><span>);
         
        </span><span>if</span>(!<span>$locked</span><span>) {
            </span><span>return</span> <span>false</span><span>;
        }
        </span><span>else</span><span> {
            </span><span>$cts</span> = <span>file_get_contents</span>(<span>$path</span><span>);
             
            </span><span>flock</span>(<span>$fo</span>,<span> LOCK_UN);
            </span><span>fclose</span>(<span>$fo</span><span>);
             
            </span><span>return</span> <span>$cts</span><span>;
        }
    }
} </span>

eg:2
The running result of this machine is inconsistent with the following. It can be written during LOCK_SH (???)
The following is an example in the manual
I just spend a long time to understand why write function returns me "0", on a basic file opening and then writing.
I discovered that if you use LOCK_SH and then you write something, that will not work :

<?<span>php
</span><span>$fp</span> = <span>fopen</span>('file.txt', 'a'<span>);

</span><span>flock</span>(<span>$fp</span>,<span>LOCK_SH);

</span><span>$written</span> = <span>fputs</span>(<span>$fp</span>, 'data'<span>);

</span><span>var_dump</span>(<span>$written</span>); <span>//</span><span> 0 and file is not changed</span>

<span>fclose</span>(<span>$fp</span>);

eg:3

Write code to solve the problem of multiple processes and threads reading and writing a file at the same time ​Question:
PHP does not have the concept of multi-threading. However, we can still use "imperfect" methods to simulate multi-threading.
To put it simply, it is queue processing. This is achieved by locking and unlocking files. When a file is operated by a user,
the file is locked, and other users can only wait. It is indeed not perfect, but it can also meet some applications with low requirements

<?<span>php
</span><span>function</span> T_put(<span>$filename</span>,<span>$string</span><span>){  
</span><span>$fp</span> = <span>fopen</span>(<span>$filename</span>,&rsquo;a'<span>); //追加方式打开  
if (flock($fp, LOCK_EX)){ //加写锁  
fputs($fp,$string); //写文件  
flock($fp, LOCK_UN); //解锁  
}  
fclose($fp);  
}  

function T_get($filename,$length){  
$fp = fopen($filename,&rsquo;r</span>'); <span>//</span><span>追加方式打开  </span>
<span>if</span> (<span>flock</span>(<span>$fp</span>, LOCK_SH)){ <span>//</span><span>加读锁  </span>
<span>$result</span> = <span>fgets</span>(<span>$fp</span>,<span>$length</span>); <span>//</span><span>读取文件  </span>
<span>flock</span>(<span>$fp</span>, LOCK_UN); <span>//</span><span>解锁  </span>
<span>}  
</span><span>fclose</span>(<span>$fp</span><span>);  
</span><span>return</span> <span>$result</span><span>;  
}</span>

http://www.bkjia.com/PHPjc/769340.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/769340.htmlTechArticlebool flock ( resource $handle , int $operation [, int $wouldblock ] ) handle file system pointer, which is a typical A resource created by fopen(). operation can be one of the following values...
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