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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Spawning parallel processes

Status
Not open for further replies.

mwhamilton

Technical User
Jan 28, 2002
11
US
I am essentially trying to create a job queing system. We use a simulator to simulate all of our new hardware designs, and the vhdl simulator is quite slow. The simulator is called with a perl script. I have created a script that checks for machine availability, and also inputs the different test names that should be simulated. I am having one problem when using fork() to spawn processes. Each time I fork, I need to rsh into a different machine on the network. I am not sure when to use system or exec. The simulation takes a long time, so I need multiple simulations running on different machines at the same time. Also, I need to send all of the output back to the original xterm window. Please help! Thanks so much!

# START A NEW CHILD
if ( ( $pid = fork()) == 0 )
{
# CHILD
#Here is where I need to rsh into a new machine
#here is where I need to change directories on the remote machine
#Here is where I need to run the simulation on the remote machine
exit(1);
}
else
{
# PARENT
$children{$pid} = $command_to_execute; #hash links command line to the pid
$count++; #increment number of processes running
}
}



 
I think using 'open' with '-|' will do what you are looking
for. The open(HANDLE,"-|") syntax forks a new child
process that is identical to the parent except that in the
parent $pid is set by that 'open' statement.
- Immediately after the 'open' statement, you have two
process, a parent and a child.
- The parent has $pid set and the child does not. Control your
flow base on $pid. If $pid is set, do parent stufff. If
$pid is null, do child stuff.
- The child picks up executing where the the fork occurs. It
does not start over at the beginning of the code.
- STDOUT from the child goes back to the handle in the parent.
- Make sure you turn on autoflush or you may loose output from
you child processes. They can exit and leave stuff in their
buffers if autoflush is not on.

Code:
# Set autoflush on to make sure the children 
# get all their output back to the parent.
$| = 1; 
# In the parent, fire off a child for each rsh
foreach $req (@list_of_requests) {
  last if (!($pid = open($req,"-|"))); 
  # the 'last if' bails out if child process
}
if ($pid)  { 
  # $pid is populated - this must be the parent
  # - listen to each handle previously forked
  # - This is not fully parallel.  Note that in
  #   this example the listening is done in serial
  # 
  foreach $i (@list_of_requests) {
    while (<$i>) { print &quot;$i&quot;; }
  }
}
if (!($pid)) {
  # No pid set, therefore this is a child
  # Do child stuff, print output to parent via STDOUT
  # and exit when finished.
  print &quot;Something back to the parent process
         via STDOUT.\n&quot;;
  exit;   # the child does its work and bails out.
  }
$| = 0; # set AutoFlush back to its default.

See 'Programming Perl' published by OReilly by Wall, Christiansen,
and Schwartz for lots more detail on playing with this stuff.
This example leaves a lot of detail out in favor or brevity, but
hopefully it illustrates an approach. 'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top