⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch07_12.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 7.11. Locking a File (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:36:48Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch07_01.htm"TITLE="7. File Access"><LINKREL="prev"HREF="ch07_11.htm"TITLE="7.10. Modifying a File in Place Without a Temporary File"><LINKREL="next"HREF="ch07_13.htm"TITLE="7.12. Flushing Output"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch07_11.htm"TITLE="7.10. Modifying a File in Place Without a Temporary File"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 7.10. Modifying a File in Place Without a Temporary File"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch07_01.htm"TITLE="7. File Access"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch07_13.htm"TITLE="7.12. Flushing Output"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 7.12. Flushing Output"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch07-37375">7.11. Locking a File</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-1060">Problem<ACLASS="indexterm"NAME="ch07-idx-1000009689-0"></A><ACLASS="indexterm"NAME="ch07-idx-1000009689-1"></A><ACLASS="indexterm"NAME="ch07-idx-1000009689-2"></A><ACLASS="indexterm"NAME="ch07-idx-1000009689-3"></A><ACLASS="indexterm"NAME="ch07-idx-1000009689-4"></A></A></H3><PCLASS="para">Many processes need to update the same file simultaneously.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-1066">Solution</A></H3><PCLASS="para">Have all processes honor advisory locking by using <CODECLASS="literal">flock</CODE>:</P><PRECLASS="programlisting">open(FH, &quot;+&lt; $path&quot;)                or die &quot;can't open $path: $!&quot;;flock(FH, 2)                        or die &quot;can't flock $path: $!&quot;;# update file, then...close(FH)                           or die &quot;can't close $path: $!&quot;;</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-1080">Discussion</A></H3><PCLASS="para">Operating systems vary greatly in the type and reliability of locking techniques available. Perl tries hard to give you something that works, even if your operating system uses its own underlying technique. The <CODECLASS="literal">flock</CODE> function takes two arguments: a filehandle and a number representing what to do with the lock on that filehandle. The numbers are normally represented by names like LOCK_EX, which you can get from the Fcntl or IO::File modules.</P><PCLASS="para">The LOCK_SH, LOCK_EX, LOCK_UN, and LOCK_NB symbolic values were not available in the Fcntl module before the 5.004 release, and even now they are available only if you ask for them specifically using the <CODECLASS="literal">:flock</CODE> tag. Their values are 1, 2, 4, and 8 respectively, which you may supply yourself instead of using the symbolic constants. You'll therefore often see people write:</P><PRECLASS="programlisting">sub LOCK_SH()  { 1 }     #  Shared lock (for reading)sub LOCK_EX()  { 2 }     #  Exclusive lock (for writing)sub LOCK_NB()  { 4 }     #  Non-blocking request (don't stall)sub LOCK_UN()  { 8 }     #  Free the lock (careful!)</PRE><PCLASS="para"><ACLASS="indexterm"NAME="ch07-idx-1000009695-0"></A><ACLASS="indexterm"NAME="ch07-idx-1000009695-1"></A>Locks come in two varieties: shared and exclusive. Despite what you might infer by "exclusive," processes aren't required to obey locks on files. Another way of saying this is that <CODECLASS="literal">flock</CODE> implements <EMCLASS="emphasis">advisory locking</EM>. It allows processes to let the operating system suspend would-be writers of a file until any readers are finished with it.</P><PCLASS="para">Flocking files is like putting up a stoplight at an intersection. It works only if people pay attention to whether the light is red or green &nbsp;-  or yellow in the case of a shared lock. The red light doesn't stop traffic; it merely signals that traffic should stop. A desperate, ignorant, or rude person will still go flying through the intersection no matter what the light says. Likewise, <CODECLASS="literal">flock</CODE> only blocks out other flockers &nbsp;-  not processes trying to do I/O. Unless everyone is polite, accidents can (and will) happen.</P><PCLASS="para">The polite process customarily indicates its intent to read from the file by requesting a LOCK_SH. Many processes can have simultaneous shared locks on the file because they (presumably) won't be changing the data. If a process intends to write to the file, it should request an exclusive lock via LOCK_EX. The operating system then suspends the requesting process until all other processes have released their locks, at which point it grants the lock to the suspended process and unblocks it. You are guaranteed that no other process will be able to run <CODECLASS="literal">flock(FH,</CODE> <CODECLASS="literal">LOCK_EX)</CODE> on the same file while you hold the lock. This is almost but not quite like saying that there can be only one exclusive lock on the file. Forked children inherit not only their parents' open files, but, on some systems, also any locks held. That means if you hold an exclusive lock and <CODECLASS="literal">fork</CODE> without <CODECLASS="literal">exec</CODE>ing, your child may also have that same exclusive lock on the file.</P><PCLASS="para">The <CODECLASS="literal">flock</CODE> function is therefore by default a blocking operation. You can also acquire a lock without wedging your process by using the LOCK_NB flag when you request a lock. This lets you warn the user that there may be a wait until other processes with locks are done:</P><PRECLASS="programlisting">unless (flock(FH, LOCK_EX|LOCK_NB)) {    warn &quot;can't immediately write-lock the file ($!), blocking ...&quot;;    unless (flock(FH, LOCK_EX)) {        die &quot;can't get write-lock on numfile: $!&quot;;    }}</PRE><PCLASS="para">If you use LOCK_NB and are refused a LOCK_SH, then you know that someone else has a LOCK_EX and is updating the file. If you are refused a LOCK_EX, then someone holds either a LOCK_SH or a LOCK_EX, so you shouldn't try to update the file.</P><PCLASS="para"><ACLASS="indexterm"NAME="ch07-idx-1000009696-0"></A>Locks dissolve when the file is closed, which may not be until your process exits. Manually unlocking the file without closing can be perilous due to buffering. The buffer might not have been flushed, leading to a time between unlocking and closing when another process could read data that the contents of your buffer were supposed to replace. A safer way to deal with this is:</P><PRECLASS="programlisting">if ($] &lt; 5.004) {                   # test Perl version number     my $old_fh = select(FH);     local $| = 1;                  # enable command buffering     local $\ = '';                 # clear output record separator     print &quot;&quot;;                      # trigger output flush     select($old_fh);               # restore previous filehandle}flock(FH, LOCK_UN);</PRE><PCLASS="para">Before version 5.004 of Perl, you had to force a flush. Because folks would often forget to do that, 5.004 changed LOCK_UN so that any pending unwritten buffers are automatically flushed right before the lock is released.</P><PCLASS="para">Here's how you increment a number in a file, using <CODECLASS="literal">flock</CODE>:</P><PRECLASS="programlisting">use Fcntl qw(:DEFAULT :flock);sysopen(FH, &quot;numfile&quot;, O_RDWR|O_CREAT)                                    or die &quot;can't open numfile: $!&quot;;flock(FH, LOCK_EX)                  or die &quot;can't write-lock numfile: $!&quot;;# Now we have acquired the lock, it's safe for I/O$num = &lt;FH&gt; || 0;                   # DO NOT USE &quot;or&quot; THERE!!seek(FH, 0, 0)                      or die &quot;can't rewind numfile : $!&quot;;truncate(FH, 0)                     or die &quot;can't truncate numfile: $!&quot;;print FH $num+1, &quot;\n&quot;               or die &quot;can't write numfile: $!&quot;;close(FH)                           or die &quot;can't close numfile: $!&quot;;</PRE><PCLASS="para">Closing the filehandle flushes the buffers and unlocks the file. The <CODECLASS="literal">truncate</CODE> function is discussed in <ACLASS="xref"HREF="ch08_01.htm"TITLE="File Contents">Chapter 8</A>.</P><PCLASS="para">File locking is not as easy as you might think &nbsp;-  or wish. Because locks are advisory, if one process uses locking and another doesn't, all bets are off. Never use the existence of a file as a locking indication because there exists a race condition between the test for the existence of the file and its creation. Furthermore, because file locking is an intrinsically stateful concept, it doesn't get along well with the stateless model embraced by network filesystems such as NFS. Although some vendors claim that <CODECLASS="literal">fcntl</CODE> addresses such matters, practical experience suggests otherwise.</P><PCLASS="para">NFS locking intimately involves both server and client. Consequently, we know of no general mechanism for guaranteed reliable locking over NFS. You can do it if you know certain operations are atomic in the server or client implementation. You can do it if you know that both server and client support <CODECLASS="literal">flock</CODE> or <CODECLASS="literal">fcntl </CODE>; most don't. But in general you can't hope to write code that works everywhere.</P><PCLASS="para">Don't confuse Perl's <CODECLASS="literal">flock</CODE> with the SysV function <CODECLASS="literal">lockf</CODE>. Unlike <CODECLASS="literal">lockf</CODE>, <CODECLASS="literal">flock</CODE> locks entire files at once. Perl doesn't support <CODECLASS="literal">lockf</CODE> directly. The only way to lock a portion of a file is to use the <CODECLASS="literal">fnctl</CODE> function, as demonstrated in the <EMCLASS="emphasis">lockarea</EM> program at the end of this chapter.<ACLASS="indexterm"NAME="ch07-idx-1000009691-0"></A><ACLASS="indexterm"NAME="ch07-idx-1000009691-1"></A><ACLASS="indexterm"NAME="ch07-idx-1000009691-2"></A><ACLASS="indexterm"NAME="ch07-idx-1000009691-3"></A><ACLASS="indexterm"NAME="ch07-idx-1000009691-4"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch07-pgfId-1170">See Also</A></H3><PCLASS="para">The <CODECLASS="literal">flock</CODE> and <CODECLASS="literal">fcntl</CODE> functions in <ICLASS="filename">perlfunc </I>(1) and in <ACLASS="olink"HREF="../prog/ch03_01.htm">Chapter 3</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>; the documentation for the standard Fcntl and DB_File modules (also in <ACLASS="olink"HREF="../prog/ch07_01.htm">Chapter 7</A> of <ACLASS="citetitle"HREF="../prog/index.htm"TITLE="Programming Perl"><CITECLASS="citetitle">Programming Perl</CITE></A>); <ACLASS="xref"HREF="ch07_22.htm"TITLE="Program: netlock">Recipe 7.21</A>; <ACLASS="xref"HREF="ch07_23.htm"TITLE="Program: lockarea">Recipe 7.22</A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch07_11.htm"TITLE="7.10. Modifying a File in Place Without a Temporary File"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 7.10. Modifying a File in Place Without a Temporary File"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch07_13.htm"TITLE="7.12. Flushing Output"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 7.12. Flushing Output"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">7.10. Modifying a File in Place Without a Temporary File</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">7.12. Flushing Output</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -