mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-29 21:12:42 +08:00
229 lines
12 KiB
HTML
229 lines
12 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||
<html>
|
||
<head>
|
||
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
||
<title>轻便的咨询文件锁定</title>
|
||
</head>
|
||
<body class="docs"><div id="layout">
|
||
<div id="layout-content"><div id="function.flock" class="refentry">
|
||
<div class="refnamediv">
|
||
<h1 class="refname">flock</h1>
|
||
<p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">flock</span> — <span class="dc-title">轻便的咨询文件锁定</span></p>
|
||
|
||
</div>
|
||
<div class="refsect1 description" id="refsect1-function.flock-description">
|
||
<h3 class="title">说明</h3>
|
||
<div class="methodsynopsis dc-description">
|
||
<span class="methodname"><strong>flock</strong></span>
|
||
( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$handle</code></span>
|
||
, <span class="methodparam"><span class="type">int</span> <code class="parameter">$operation</code></span>
|
||
[, <span class="methodparam"><span class="type">int</span> <code class="parameter reference">&$wouldblock</code></span>
|
||
] ) : <span class="type">bool</span></div>
|
||
|
||
<p class="para rdfs-comment">
|
||
<span class="function"><strong>flock()</strong></span> 允许执行一个简单的可以在任何平台中使用的读取/写入模型(包括大部分的
|
||
Unix 派生版和甚至是 Windows)。
|
||
</p>
|
||
<p class="para">
|
||
在 PHP 5.3.2版本之前,锁也会被 <span class="function"><a href="fclose.html" class="function">fclose()</a></span> 释放(在脚本结束后会自动调用)。
|
||
</p>
|
||
<p class="para">
|
||
PHP 支持以咨询方式(也就是说所有访问程序必须使用同一方式锁定, 否则它不会工作)锁定全部文件的一种轻便方法。
|
||
默认情况下,这个函数会阻塞到获取锁;这可以通过下面文档中 <strong><code>LOCK_NB</code></strong> 选项来控制(在非 Windows 平台上)。
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 parameters" id="refsect1-function.flock-parameters">
|
||
<h3 class="title">参数</h3>
|
||
<p class="para">
|
||
<dl>
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">handle</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">文件系统指针,是典型地由
|
||
<span class="function"><a href="fopen.html" class="function">fopen()</a></span> 创建的 <span class="type"><a href="language.types.resource.html" class="type resource">resource</a></span>(资源)。</p>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">operation</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
<code class="parameter">operation</code>
|
||
可以是以下值之一:
|
||
<ul class="itemizedlist">
|
||
<li class="listitem">
|
||
<span class="simpara">
|
||
<strong><code>LOCK_SH</code></strong>取得共享锁定(读取的程序)。
|
||
</span>
|
||
</li>
|
||
<li class="listitem">
|
||
<span class="simpara">
|
||
<strong><code>LOCK_EX</code></strong> 取得独占锁定(写入的程序。
|
||
</span>
|
||
</li>
|
||
<li class="listitem">
|
||
<span class="simpara">
|
||
<strong><code>LOCK_UN</code></strong> 释放锁定(无论共享或独占)。
|
||
</span>
|
||
</li>
|
||
</ul>
|
||
</p>
|
||
<p class="para">
|
||
如果不希望 <span class="function"><strong>flock()</strong></span> 在锁定时堵塞,则是
|
||
<strong><code>LOCK_NB</code></strong>(Windows 上还不支持)。
|
||
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
|
||
<dt>
|
||
<code class="parameter">wouldblock</code></dt>
|
||
|
||
<dd>
|
||
|
||
<p class="para">
|
||
如果锁定会堵塞的话(EWOULDBLOCK
|
||
错误码情况下),可选的第三个参数会被设置为 <strong><code>TRUE</code></strong>。(Windows 上不支持)
|
||
</p>
|
||
</dd>
|
||
|
||
|
||
</dl>
|
||
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 returnvalues" id="refsect1-function.flock-returnvalues">
|
||
<h3 class="title">返回值</h3>
|
||
<p class="para">
|
||
成功时返回 <strong><code>TRUE</code></strong>, 或者在失败时返回 <strong><code>FALSE</code></strong>。
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 changelog" id="refsect1-function.flock-changelog">
|
||
<h3 class="title">更新日志</h3>
|
||
<p class="para">
|
||
<table class="doctable informaltable">
|
||
|
||
<thead>
|
||
<tr>
|
||
<th>版本</th>
|
||
<th>说明</th>
|
||
</tr>
|
||
|
||
</thead>
|
||
|
||
<tbody class="tbody">
|
||
<tr>
|
||
<td>5.3.2</td>
|
||
<td>
|
||
在文件资源句柄关闭时不再自动解锁。现在要解锁必须手动进行。
|
||
</td>
|
||
</tr>
|
||
|
||
<tr>
|
||
<td>4.0.1</td>
|
||
<td>
|
||
增加了常量 <em>LOCK_XXX</em>。
|
||
之前你必须使用 1 代表 <strong><code>LOCK_SH</code></strong>,2 代表
|
||
<strong><code>LOCK_EX</code></strong>,3 代表<strong><code>LOCK_UN</code></strong>,4 代表
|
||
<strong><code>LOCK_NB</code></strong>。
|
||
</td>
|
||
</tr>
|
||
|
||
</tbody>
|
||
|
||
</table>
|
||
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 examples" id="refsect1-function.flock-examples">
|
||
<h3 class="title">范例</h3>
|
||
<p class="para">
|
||
<div class="example" id="example-2795">
|
||
<p><strong>Example #1 <span class="function"><strong>flock()</strong></span> 例子</strong></p>
|
||
<div class="example-contents">
|
||
<div class="phpcode"><pre><span style="color: #000000">
|
||
<span style="color: #0000BB"><?php<br /><br />$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">"/tmp/lock.txt"</span><span style="color: #007700">, </span><span style="color: #DD0000">"r+"</span><span style="color: #007700">);<br /><br />if (</span><span style="color: #0000BB">flock</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">LOCK_EX</span><span style="color: #007700">)) { </span><span style="color: #FF8000">// 进行排它型锁定<br /> </span><span style="color: #0000BB">ftruncate</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">); </span><span style="color: #FF8000">// truncate file<br /> </span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #DD0000">"Write something here\n"</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">fflush</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">); </span><span style="color: #FF8000">// flush output before releasing the lock<br /> </span><span style="color: #0000BB">flock</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">LOCK_UN</span><span style="color: #007700">); </span><span style="color: #FF8000">// 释放锁定<br /></span><span style="color: #007700">} else {<br /> echo </span><span style="color: #DD0000">"Couldn't get the lock!"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span>
|
||
</span>
|
||
</pre></div>
|
||
</div>
|
||
|
||
</div>
|
||
</p>
|
||
<p class="para">
|
||
<div class="example" id="example-2796">
|
||
<p><strong>Example #2 <span class="function"><strong>flock()</strong></span> 使用 <strong><code>LOCK_NB</code></strong> 选项</strong></p>
|
||
<div class="example-contents">
|
||
<div class="phpcode"><pre><span style="color: #000000">
|
||
<span style="color: #0000BB"><?php<br />$fp </span><span style="color: #007700">= </span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #DD0000">'/tmp/lock.txt'</span><span style="color: #007700">, </span><span style="color: #DD0000">'r+'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/* Activate the LOCK_NB option on an LOCK_EX operation */<br /></span><span style="color: #007700">if(!</span><span style="color: #0000BB">flock</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">, </span><span style="color: #0000BB">LOCK_EX </span><span style="color: #007700">| </span><span style="color: #0000BB">LOCK_NB</span><span style="color: #007700">)) {<br /> echo </span><span style="color: #DD0000">'Unable to obtain lock'</span><span style="color: #007700">;<br /> exit(-</span><span style="color: #0000BB">1</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">/* ... */<br /><br /></span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$fp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
|
||
</span>
|
||
</pre></div>
|
||
</div>
|
||
|
||
</div>
|
||
</p>
|
||
</div>
|
||
|
||
|
||
<div class="refsect1 notes" id="refsect1-function.flock-notes">
|
||
<h3 class="title">注释</h3>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
<span class="function"><strong>flock()</strong></span> uses mandatory locking instead of advisory
|
||
locking on Windows. Mandatory locking is also supported on Linux and
|
||
System V based operating systems via the usual mechanism supported by the
|
||
fcntl() system call: that is, if the file in question has the setgid
|
||
permission bit set and the group execution bit cleared. On Linux, the file
|
||
system will also need to be mounted with the mand option for this to work.
|
||
</p>
|
||
</p></blockquote>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
由于 <span class="function"><strong>flock()</strong></span>
|
||
需要一个文件指针, 因此可能不得不用一个特殊的锁定文件来保护打算通过写模式打开的文件的访问(在
|
||
<span class="function"><a href="fopen.html" class="function">fopen()</a></span> 函数中加入 "w" 或 "w+")。
|
||
</p>
|
||
</p></blockquote>
|
||
<blockquote class="note"><p><strong class="note">Note</strong>:
|
||
<p class="para">
|
||
May only be used on file pointers returned by <span class="function"><a href="fopen.html" class="function">fopen()</a></span>
|
||
for local files, or file pointers pointing to userspace streams that
|
||
implement the <span class="function"><a href="streamwrapper.stream_lock.html" class="function">streamWrapper::stream_lock()</a></span> method.
|
||
</p>
|
||
</p></blockquote>
|
||
<div class="warning"><strong class="warning">Warning</strong>
|
||
<p class="para">
|
||
Assigning another value to <code class="parameter">handle</code> argument in
|
||
subsequent code will release the lock.
|
||
</p>
|
||
</div>
|
||
<div class="warning"><strong class="warning">Warning</strong>
|
||
<p class="para">
|
||
在部分操作系统中 <span class="function"><strong>flock()</strong></span> 以进程级实现。当用一个多线程服务器
|
||
API(比如 ISAPI)时,可能不可以依靠
|
||
<span class="function"><strong>flock()</strong></span> 来保护文件,因为运行于同一服务器实例中其它并行线程的
|
||
PHP 脚本可以对该文件进行处理。
|
||
</p>
|
||
<p class="para">
|
||
<span class="function"><strong>flock()</strong></span> 不支持旧的文件系统,如 <em>FAT</em>
|
||
以及它的派生系统。因此,此环境下总是返回 <strong><code>FALSE</code></strong>(尤其是对 Windows 98 用户来说)。
|
||
</p>
|
||
</div>
|
||
</div>
|
||
|
||
</div></div></div></body></html> |