Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to pass info from child process to parent 1

Status
Not open for further replies.

vvv

Programmer
Jan 11, 2001
7
0
0
KR
The question is :
how to pass info from child to parent not using files

is it possible?
 
ya, it can easily be done with pipes... i'm pretty sure that there is an easier way than that, also, but, i'm not coherent enough to remember at the moment...

i'll post back when i remember. adam@aauser.com
 
playing with pipes is easier than one thinks, once you understand the flow..... I use this to launch multiple database calls to run simultaneously. Then, listen for the return
of each one.

General Flow:
Set print Buffer to autoflush.
Foreach required child process, spawn a child using "-|"
This syntax returns the process ID of the child process to the parent
and spawns a copy of the code at that point as a child. STDOUT from the child pionts back through the pipe to the parent. The child does not
have $pid populated, so that is how you can tell a child from a parent. Except for
the process ID, the child is an exact copy of the parent. All variables are set or are
null in the child just as they were in the parent at the time the child was spawned.
If $pid exists, behave like the parent process - continue spawning children until finished and then listen to each one.
If $pid is null, behave like a child and print output to STDOUT.

###########################
$| = 1; # set autoflush on

# having built a list of chores I need to do....
foreach $line (@lines)
{
($siteName,$host,$port,$dbName) = split(/\|/,$line);
last if (!($pid = open($siteName,"-|"))); # bail out if child
$children{$pid} = $siteName; # keep a list of children to cycle through later
}
if ($pid) # process ID exists - must be the parent process
{
foreach $db (keys(%children))
{
# this is a little circuitous - could be more concise....
$db = $children{$db}; # get name of handle(s) opened above
while (<$db>) { print; } # the handle is the same thing used to open the pipe.
}
}
if (!($pid)) # child process
{
# do something and print output to parent
print &quot;$output&quot;;
exit; # Since this is a child, bail out. It is finished, and we don't want it to
# to continue into code that should be run only by the parent process.
}

$| = 0; # set autoflush back to default.
##########################

This code was clipped from a larger more complex piece, so some of the varibles (siteName) are aritifacts from the 'parent' piece of code. But, you can see the....
$pid = open(HANDLE,&quot;-|&quot;);
if ($pid) { I'm the parent process - listen to handles }
if (!($pid)) { I'm a child - get output and print to STDOUT through pipe to parent }

For more detail, see &quot;Programming Perl&quot;, by Wall, Christiansen, and Schwartz, from O'Reilly, second edition, page 343, section &quot;Talking to yourself&quot;.



'hope this helps.


keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top