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

📄 ch14_06.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 14.5. Locking DBM Files (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:42:49Z"><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="ch14_01.htm"TITLE="14. Database Access"><LINKREL="prev"HREF="ch14_05.htm"TITLE="14.4. Merging DBM Files"><LINKREL="next"HREF="ch14_07.htm"TITLE="14.6. Sorting Large DBM Files"></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="ch14_05.htm"TITLE="14.4. Merging DBM Files"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 14.4. Merging DBM Files"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch14_01.htm"TITLE="14. Database Access"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch14_07.htm"TITLE="14.6. Sorting Large DBM Files"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 14.6. Sorting Large DBM Files"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch14-chap14_locking_0">14.5. Locking DBM Files</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch14-pgfId-460">Problem<ACLASS="indexterm"NAME="ch14-idx-1000004957-0"></A><ACLASS="indexterm"NAME="ch14-idx-1000004957-1"></A><ACLASS="indexterm"NAME="ch14-idx-1000004957-2"></A></A></H3><PCLASS="para">You need several concurrently running programs to have simultaneous access to a DBM file.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch14-pgfId-466">Solution</A></H3><PCLASS="para">Either use the DBM implementation's locking mechanism if it has one, lock the file with <CODECLASS="literal">flock</CODE>, or use an auxiliary locking scheme as in <ACLASS="xref"HREF="ch07_22.htm"TITLE="Program: netlock">Recipe 7.21</A>.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch14-pgfId-472">Discussion</A></H3><PCLASS="para">With SDBM or NDBM, you can't do much to lock the database itself. You must devise an auxiliary locking scheme using an extra lockfile.</P><PCLASS="para"><ACLASS="indexterm"NAME="ch14-idx-1000004958-0"></A>GDBM uses the concept of readers and writers: either many readers or one solitary writer may have a GDBM file open at any given time. You specify whether you're a reader or a writer when you open it. This can be annoying.</P><PCLASS="para">Version 1 of Berkeley DB gives you access to the file descriptor of the open database, allowing you to <CODECLASS="literal">flock</CODE> it. The lock applies to the database as a whole, not to individual records. Version 2 implements its own full transaction system with locking.</P><PCLASS="para"><ACLASS="xref"HREF="ch14_06.htm#ch14-25344"TITLE="dblockdemo">Example 14.3</A> shows an example of locking a database using Berkeley DB. Run this repeatedly in the background to see locks granted in proper order.</P><DIVCLASS="example"><H4CLASS="example"><ACLASS="title"NAME="ch14-25344">Example 14.3: dblockdemo</A></H4><PRECLASS="programlisting">#!/usr/bin/perl# <ACLASS="indexterm"NAME="ch14-idx-1000005185-0"></A>dblockdemo - demo locking dbm databasesuse DB_File;use strict;sub LOCK_SH { 1 }                   # In case you don't havesub LOCK_EX { 2 }                   # the standard Fcntl module.  Yousub LOCK_NB { 4 }                   # should, but who can tellsub LOCK_UN { 8 }                   # how those chips fall?my($oldval, $fd, $db, %db, $value, $key);$key    = shift || 'default';$value  = shift || 'magic';$value .= &quot; $$&quot;;$db = tie(%db, 'DB_File', '/tmp/foo.db', O_CREAT|O_RDWR, 0666)    or die &quot;dbcreat /tmp/foo.db $!&quot;;$fd = $db-&gt;fd;                      # need this for lockingprint &quot;$$: db fd is $fd\n&quot;;open(DB_FH, &quot;+&lt;&amp;=$fd&quot;)    or die &quot;dup $!&quot;;unless (flock (DB_FH, LOCK_SH | LOCK_NB)) {    print &quot;$$: CONTENTION; can't read during write update!                Waiting for read lock ($!) ....&quot;;    unless (flock (DB_FH, LOCK_SH)) { die &quot;flock: $!&quot; }}print &quot;$$: Read lock granted\n&quot;;$oldval = $db{$key};print &quot;$$: Old value was $oldval\n&quot;;flock(DB_FH, LOCK_UN);unless (flock (DB_FH, LOCK_EX | LOCK_NB)) {    print &quot;$$: CONTENTION; must have exclusive lock!                Waiting for write lock ($!) ....&quot;;    unless (flock (DB_FH, LOCK_EX)) { die &quot;flock: $!&quot; }}print &quot;$$: Write lock granted\n&quot;;$db{$key} = $value;$db-&gt;sync;  # to flushsleep 10;flock(DB_FH, LOCK_UN);undef $db;untie %db;close(DB_FH);print &quot;$$: Updated db to $key=$value\n&quot;;<ACLASS="indexterm"NAME="ch14-idx-1000005287-0"></A><ACLASS="indexterm"NAME="ch14-idx-1000005287-1"></A><ACLASS="indexterm"NAME="ch14-idx-1000005287-2"></A></PRE></DIV></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch14-pgfId-594">See Also</A></H3><PCLASS="para">The documentation for the standard DB_File module, 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_12.htm"TITLE="Locking a File">Recipe 7.11</A>; <ACLASS="xref"HREF="ch16_13.htm"TITLE="Sharing Variables in Different Processes">Recipe 16.12</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="ch14_05.htm"TITLE="14.4. Merging DBM Files"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 14.4. Merging DBM Files"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="ch14_07.htm"TITLE="14.6. Sorting Large DBM Files"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 14.6. Sorting Large DBM Files"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">14.4. Merging DBM Files</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">14.6. Sorting Large DBM Files</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 + -