You rock, Weave!  I&#39;ll see if I can make heads or tails of it when I get home and have some time to dedicate to making it work.  This must be the answer I&#39;m looking for.. I&#39;ve got to speed up my scripts.<div><br>
</div><div>Greg<br><br><div class="gmail_quote">On Fri, Mar 11, 2011 at 10:36 AM, Brian Weaver <span dir="ltr">&lt;<a href="mailto:cmdrclueless@gmail.com">cmdrclueless@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
pipe(my $child_rd, my $parent_wr) or die &quot;Failed to get pipe system resources: $!&quot;;<br>
pipe(my $parent_rd, my $child_wr) or die &quot;Failed to get pipe system resources: $!&quot;;<br>
<br>
my $pid = fork();<br>
die &quot;System resource error, fork failed: $!&quot; unless defined $pid;<br>
if($pid == 0) { # child<br>
    close($parent_wr);<br>
    close($parent_rd);<br>
    open(STDIN, &#39;&lt;&amp;&#39;, $child_rd) or die &quot;Failed to tie pipe to standard input; $!&quot;;<br>
    open(STDOUT, &#39;&gt;&amp;&#39;, $child_wr) or die &quot;Failed to tie pipe to standard output: $!&quot;;<br>
<br>
    exec $command;  # or launch your subprocess which reads/writes from standard input/output.<br>
    die &quot;Exec of &#39;$command&#39; failed: $!&quot;;<br>
}<br>
<br>
close($child_wr);<br>
close($child_rd);<br>
<br>
###<br>
<br>
Just some pseudo code to help you get a quick start. It might put you on the right path. I do not warranty that the above code is error free!<br>
<font color="#888888"><br>
-- Brian<br>
</font><div><div></div><div class="h5"><br>
<br>
<br>
On Mar 11, 2011, at 10:04 AM, Greg Brown wrote:<br>
<br>
&gt; Ok, that sounds logical.  I&#39;ve never forked a perl script before so I&#39;ve got some fun tonight when I get home.  I like this idea, though<br>
&gt;<br>
&gt; Thanks!<br>
&gt;<br>
&gt; Greg<br>
&gt;<br>
&gt; On Fri, Mar 11, 2011 at 10:00 AM, Brian Weaver &lt;<a href="mailto:cmdrclueless@gmail.com">cmdrclueless@gmail.com</a>&gt; wrote:<br>
&gt; Greg,<br>
&gt;<br>
&gt; I don&#39;t know about using threads, but if you want multiple instances with each working on a single device then why not use a master-child process model? Have the master fork off N children and maintain a link using anonymous pipes, unix sockets, or some other IPC mechanism. The master can inform each child on what system to query and then receive a status back from the child when it&#39;s done.<br>

&gt;<br>
&gt; Instead of writing the status to a log file the child process could simply write the status back to the parent&#39;s communication channel. The master process can then write to the log file without any fear of multiple writers.<br>

&gt;<br>
&gt; -- Brian<br>
&gt;<br>
&gt;<br>
&gt; On Mar 10, 2011, at 5:07 PM, Greg Brown wrote:<br>
&gt;<br>
&gt; &gt; Ok, I&#39;ve GOT to get my scripts using threads.  Here&#39;s what I&#39;ve got and what I&#39;d like to do:<br>
&gt; &gt;<br>
&gt; &gt; source: a 3000 line file with hostnames, IP addrs, etc.<br>
&gt; &gt;<br>
&gt; &gt; what script does: logs into each device using Net::SSH::Expect that makes configuration changes to IOS devices (uses Net::SSH::Expect rather than <a href="http://expect.pm" target="_blank">expect.pm</a> because one of our NMS servers is a windows box and the scripts have to run across all NMS servers.. sigh)<br>

&gt; &gt;<br>
&gt; &gt; how script does it: the guts of the script, the real work, is all done in a subscript.  The main portion of the script opens the file and creates a foreach loop to process each line (and device) one at at time.  As you might imagine this script takes a long time to complete.  The subscript returns a message to the main script that is written to a log file (&quot;login failed&quot;, &quot;change implemented&quot;, etc).<br>

&gt; &gt;<br>
&gt; &gt; what I&#39;d like to do: take a group of devices, say five at at time, and launch the subscript as a thread so I can process more than one device at a time.  I would imagine this would involve some kind of flock on the log file so two threads couldn&#39;t return at the same time.  John B. sent me some instructions how to thread a perl script a while back but now I can&#39;t find it.<br>

&gt; &gt;<br>
&gt; &gt; Does anyone know of a perl book with good and clear examples that use threads?<br>
&gt; &gt;<br>
&gt; &gt; Greg<br>
&gt; &gt; _______________________________________________<br>
&gt; &gt; Trilug-ontopic mailing list<br>
&gt; &gt; <a href="mailto:Trilug-ontopic@trilug.org">Trilug-ontopic@trilug.org</a><br>
&gt; &gt; <a href="http://www.trilug.org/mailman/listinfo/trilug-ontopic" target="_blank">http://www.trilug.org/mailman/listinfo/trilug-ontopic</a><br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; Trilug-ontopic mailing list<br>
&gt; <a href="mailto:Trilug-ontopic@trilug.org">Trilug-ontopic@trilug.org</a><br>
&gt; <a href="http://www.trilug.org/mailman/listinfo/trilug-ontopic" target="_blank">http://www.trilug.org/mailman/listinfo/trilug-ontopic</a><br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; Trilug-ontopic mailing list<br>
&gt; <a href="mailto:Trilug-ontopic@trilug.org">Trilug-ontopic@trilug.org</a><br>
&gt; <a href="http://www.trilug.org/mailman/listinfo/trilug-ontopic" target="_blank">http://www.trilug.org/mailman/listinfo/trilug-ontopic</a><br>
<br>
_______________________________________________<br>
Trilug-ontopic mailing list<br>
<a href="mailto:Trilug-ontopic@trilug.org">Trilug-ontopic@trilug.org</a><br>
<a href="http://www.trilug.org/mailman/listinfo/trilug-ontopic" target="_blank">http://www.trilug.org/mailman/listinfo/trilug-ontopic</a><br>
</div></div></blockquote></div><br></div>