1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
<HTML><HEAD>
<TITLE>General Tcl Information -- Sharing Files Between Interpreters</TITLE>
<LINK rel=Previous href="tgen-ch2.htm">
<LINK rel=ToC href="toc.htm">
<LINK rel=Index href="master.htm">
<LINK rel=Next href="tgen-ch4.htm">
</HEAD><BODY BGCOLOR="#ffffff"><A NAME="topofpage"></A>
<TABLE WIDTH=100%>
<TR>
<TD ALIGN=LEFT>
<A NAME="topofpage"></A> <IMG SRC="as-c-sm.gif">
</TD>
<TD ALIGN=RIGHT>
<A href="tgen-ch2.htm"><IMG BORDER="0" src=navbprev.gif alt="[ Previous ]"></A>
<A href=toc.htm> <IMG BORDER="0" src=navbhome.gif alt="[ Contents ]"></A>
<A href=master.htm> <IMG BORDER="0" src=navbhelp.gif alt="[ Index ]"></A>
<A href="tgen-ch4.htm"> <IMG BORDER="0" src=navbnext.gif alt="[ Next ]"></A>
<A name="7983"> </A>
</TD>
</TR>
</TABLE>
<a name="15579">
</a><h3>Sharing Files Between Interpreters</h3>
<p><a name="15581">
</a>To share a file between the interpreters in a group, use the Tcl <code>detach</code> command. A shared file is left open after an interpreter is deallocated when the AutoClose parameter is on (it is on by default). For example:</p>
<pre> <a name="15582"></a>#init time:
<a name="15583"></a>set sharedFile [open myfile.dat]
<a name="15584"></a>detach $sharedFile
</pre><p><p><a name="15585">
</a>You must manually close the shared file (by performing a <code>close</code>) when it is no longer needed or when the server is shut down.</p>
<p><a name="15586">
</a>Note that there is no implicit locking between interpreters. This means that one interpreter can be performing a <code>gets</code> command on the shared file at the same time another interpreter performs a <code>close</code> on the same file. The results are unpredictable, and could potentially cause a core dump. To avoid this situation, use a mutex to protect access to the file whenever it is accessed. </p>
<p><a name="15588">
</a>For example:, at initialization time (for example, in your init.tcl script), open a shared file and create a mutex:</p>
<pre> <a name="15589"></a>set sharedLock [ns_mutex create]
<a name="15590"></a>set sharedFile [open myfile.dat]
<a name="15591"></a>detach $sharedFile
</pre><p><p><a name="15592">
</a>At run time, use the mutex to protect any actions you perform on the shared file:</p>
<pre> <a name="15593"></a>ns_share sharedLock
<a name="15594"></a>ns_share sharedFile
<a name="15595"></a>ns_mutex lock $sharedLock
<a name="15596"></a>puts $sharedFile ...
<a name="15597"></a>gets $sharedFile ...
<a name="15598"></a>ns_mutex unlock $sharedLock
</pre><p><p><a name="15599">
</a>At shutdown (for example, in your shutdown procedure registered with ns_atshutdown), close the file and destroy the lock:</p>
<pre> <a name="15600"></a>ns_share sharedLock
<a name="15601"></a>ns_share sharedFile
<a name="15602"></a>close $SharedFile
<a name="15603"></a>ns_mutex destroy $SharedLock
</pre><p>
<TABLE BORDER="2" CELLPADDING="1" width="100%">
<TR><TD COLSPAN=3><P ALIGN=Center>
<IMG SRC="bluebult.gif">
<A HREF="#topofpage">
<FONT SIZE=-1>Top of Page</FONT></A>
<IMG SRC="bluebult.gif">
</TD></TR>
<TR><TD COLSPAN=3><P ALIGN=Center>
<A href="tgen-ch2.htm">
<IMG BORDER="0" src=navbprev.gif alt="[ Previous ]"></A>
<A href=toc.htm>
<IMG BORDER="0" src=navbhome.gif alt="[ Contents ]"></A>
<A href=master.htm>
<IMG BORDER="0" src=navbhelp.gif alt="[ Index ]"></A>
<A href="tgen-ch4.htm">
<IMG BORDER="0" src=navbnext.gif alt="[ Next ]"></A>
<BR align=center>
<FONT size=-1>Copyright © 1998-99 America Online,
Inc.</FONT>
</TD></TR></TABLE></BODY></HTML>
|