[Trilug-ontopic] perl and threads

Brian Weaver cmdrclueless at gmail.com
Fri Mar 11 10:36:55 EST 2011


pipe(my $child_rd, my $parent_wr) or die "Failed to get pipe system resources: $!";
pipe(my $parent_rd, my $child_wr) or die "Failed to get pipe system resources: $!";

my $pid = fork();
die "System resource error, fork failed: $!" unless defined $pid;
if($pid == 0) { # child
    close($parent_wr);
    close($parent_rd);
    open(STDIN, '<&', $child_rd) or die "Failed to tie pipe to standard input; $!";
    open(STDOUT, '>&', $child_wr) or die "Failed to tie pipe to standard output: $!";

    exec $command;  # or launch your subprocess which reads/writes from standard input/output.
    die "Exec of '$command' failed: $!";
}

close($child_wr);
close($child_rd);

###

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!

-- Brian



On Mar 11, 2011, at 10:04 AM, Greg Brown wrote:

> Ok, that sounds logical.  I've never forked a perl script before so I've got some fun tonight when I get home.  I like this idea, though
> 
> Thanks!
> 
> Greg
> 
> On Fri, Mar 11, 2011 at 10:00 AM, Brian Weaver <cmdrclueless at gmail.com> wrote:
> Greg,
> 
> I don'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's done.
> 
> Instead of writing the status to a log file the child process could simply write the status back to the parent's communication channel. The master process can then write to the log file without any fear of multiple writers.
> 
> -- Brian
> 
> 
> On Mar 10, 2011, at 5:07 PM, Greg Brown wrote:
> 
> > Ok, I've GOT to get my scripts using threads.  Here's what I've got and what I'd like to do:
> >
> > source: a 3000 line file with hostnames, IP addrs, etc.
> >
> > what script does: logs into each device using Net::SSH::Expect that makes configuration changes to IOS devices (uses Net::SSH::Expect rather than expect.pm because one of our NMS servers is a windows box and the scripts have to run across all NMS servers.. sigh)
> >
> > 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 ("login failed", "change implemented", etc).
> >
> > what I'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'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't find it.
> >
> > Does anyone know of a perl book with good and clear examples that use threads?
> >
> > Greg
> > _______________________________________________
> > Trilug-ontopic mailing list
> > Trilug-ontopic at trilug.org
> > http://www.trilug.org/mailman/listinfo/trilug-ontopic
> 
> _______________________________________________
> Trilug-ontopic mailing list
> Trilug-ontopic at trilug.org
> http://www.trilug.org/mailman/listinfo/trilug-ontopic
> 
> _______________________________________________
> Trilug-ontopic mailing list
> Trilug-ontopic at trilug.org
> http://www.trilug.org/mailman/listinfo/trilug-ontopic



More information about the Trilug-ontopic mailing list