    <rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:admin="http://webns.net/mvcb/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:content="http://purl.org/rss/1.0/modules/content/">
     <channel>
        <title>Linux.com :: Perl: Running two processes at once.</title>
        <link>http://www.linux.com/archive/forums/topic/3894</link>
        <description>Everything Linux and Open Source</description>
        <dc:language>en-us</dc:language> 
        <dc:creator>Linux.com Admin</dc:creator> 
        <admin:generatorAgent rdf:resource="http://www.linux.com" /> 
        <admin:errorReportsTo rdf:resource="mailto:noreply@linux.com" />
       <sy:updatePeriod>hourly</sy:updatePeriod>
       <sy:updateFrequency>1</sy:updateFrequency>
       <docs>http://backend.userland.com/rss</docs>



<div class="xarbb-noteswrapper">
</div>

   <div class="xarbb-noteswrapper">
       <div class="xar-sub rightnote">
           <div>
                    <a href="http://www.linux.com/archive/forums/topic/3648">
Previous Topic</a>
|
                    <a href="http://www.linux.com/archive/forums/topic/3884">
Next Topic</a>
</div>

           <div>
                    <a href="http://www.linux.com/archive/forums/printtopic/3894?theme=print">
Print this topic</a> |
You are not subscribed            </div>
        </div>
        <div style="clear: both;"></div>
    </div>

    <h1>
        <a class="xar-title" href="http://www.linux.com/archive/forums/topic/3894">Perl: Running two processes at once.</a>
   </h1>


   <div class="xarbb-topiccontrols">
<form action="http://www.linux.com/archive/forums?func=jump" method="post">
   <label for="xarbb_jump_menu">Forum Jump</label>
    <select name="f" id="xarbb_jump_menu" onchange="this.form.submit();">
        <optgroup label="Options">
            <option value="5" selected="selected">
Please select forum            </option>
            <option value="5">--------------------</option>
        </optgroup>
        <optgroup label="Forums">
                <option value="1">New to Linux</option>
                <option value="2">Hardware</option>
                <option value="3">Linux Distributions</option>
                <option value="4">Applications</option>
                <option value="5">Programming and Development</option>
                <option value="6">Miscellaneous Discussion</option>
                <option value="7">Linux.com Writers Forum</option>
                <option value="8">Jobs</option>
        </optgroup>
    </select>
    <input type="submit" class="button" value="Jump" />
</form>
<div style="clear: both;"></div>
   </div>

   <div class="xarbb-topiccontrols">

<span class="xar-sub">
    <a href="http://www.linux.com/archive/forums">
Forum Index    </a>
    <span style="font-size: larger;">&#187;</span>
    <a href="http://www.linux.com/archive/forums/category/4181">
        Forums 
    </a>
    <span style="font-size: larger;">&#187;</span>
    <a href="http://www.linux.com/archive/forums/forum/5">
        Programming and Development   </a>
</span>
<div style="clear: both;"></div>
   </div>

<div class="xarbb-tablewrapper">



   <table>
       <tr>
           <th scope="col">
Author</th>
           <th scope="col">
Message</th>
       </tr>



            <tr>
                <td class="xar-norm author">
                    <div>
                        <a href="http://www.linux.com/archive/roles/416311">|300LE4|\|</a>
                   </div>
                   <div class="xar-sub">
Joined: Jan 05, 2009<br/>Posts: 2                        <br />
                        <a href="http://www.linux.com/archive/forums?by=416311&amp;func=searchtopics">
Other Topics</a>
                   </div>
               </td>

               <td class="xar-norm message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted Jan 06, 2009 at 8:09:25 PM</span>
                       </div>



<div style="float: left;">
                           <strong class="xar-sub">
Subject: Perl: Running two processes at once.                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div>
                        I am writing a perl script and at one point I would like to run two executables at once.
If I do two system calls then I have to wait until the first process finishes to start the second.
Is there a way around this?</div>

                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/3894#top">
Back to top</a>
</td>
               <td class="xar-alt">

                        <a href="http://www.linux.com/archive/roles/416311">
                            <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                        </a>
                        <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=416311">
                            <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                       </a>
                            <a href="http://">
                                <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                           </a>
</td>
           </tr>





           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/411583">
                                thobbs                           </a>
</strong>

                   <span class="xar-sub">
<br />
Joined Oct 12, 2008<br/>Posts: 238<br />
Location:Texas!<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=411583&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-accent message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                Jan 06, 2009 10:05:42 PM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1205818" href="http://www.linux.com/archive/forums/topic/3894#1205818">
                                    Re:  Perl: Running two processes at once.                                </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        I would use fork().

If you've never forked a program before, you should read up on the basics, but this is basically how to do it:[code=xml]$pid = fork();
if($pid == 0) {
    # Fork returns 0 to the child, and the child's pid to the parent
    execute child's code
    exit 0;
    # The child will exit at this point
}
execute parent's code
exit 0;[/code]Anyways, like I said, definitely read up on forking.                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/3894#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/411583">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=411583">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
                                <a href="http://www.cs.utexas.edu/~thobbs/">
                                    <img src="modules/xarbb/xarimages/new/icon_www.gif" alt="Website" />
                               </a>
</td>
           </tr>

           <tr>
               <td class="xar-norm author">
                   <strong>

                            <a href="http://www.linux.com/archive/roles/383588">
                                Mr. Shawn H. Corey                           </a>
</strong>

                   <span class="xar-sub">
<br />
Joined Jun 21, 2007<br/>Posts: 2<br />
                        <br />
                        <a href="http://www.linux.com/archive/forums?by=383588&amp;func=searchtopics">
Other Topics                        </a>
                    </span>
                </td>

                <td class="xar-norm message">
                   <div class="xarbb-messagecontrols">
                       <div class="leftnote">
                           <span class="xar-sub">
Posted:
                                Jan 07, 2009 2:36:43 PM                           </span>
                       </div>
                       <div class="rightnote">
</div>
                       <div style="float:left;">
                           <strong class="xar-sub">
Subject:
                                <a name="1205859" href="http://www.linux.com/archive/forums/topic/3894#1205859">
                                    Re:  Perl: Running two processes at once.                                </a>
                            </strong>
                        </div>
                        <div style="clear: both;"></div>
                    </div>

                    <div style="clear: left;">
                        Here's the sub I use:
[code=xml]# --------------------------------------
#      Usage: $child_pid = fork_exec( \%control, $command; @arguments );
#    Purpose: Fork and exec the command
# Parameters:  \%control -- optional control hash ref
#                             -nohup -- non-zero means parent won't wait for child completion
#                            -system -- non-zero means use system if cannot fork
#                             -quiet -- non-zero means close STD... file handles
#               $command -- system command
#             @arguments -- optional list of arguments for command
#    Returns: $child_pid -- or undef if failure
#
sub fork_exec {
  my %control = ();
  if( my $ref = ref( $_[0] ) ){
    if( $ref eq 'HASH' ){
      %control = %{ shift @_ };
    }
  }

  my $command   = shift @_;
  my @arguments = @_;

  my $child_pid = fork();
  unless( defined $child_pid ){
    if( $control{-system} ){
      system( &quot;nohup $command @arguments &amp;&quot; );
    }
    return;
  }

  unless( $child_pid ){
    # child
    if( $control{-quiet} ){
      close STDIN;
      close STDOUT;
      close STDERR;
      open STDERR, '&gt;', '/dev/null';
    }
    $SIG{HUP} = 'IGNORE' if $control{-nohup};
    exec { $command } $command, @arguments or do {
      if( $control{-system} ){
        system( &quot;nohup $command @arguments &amp;&quot; );
      }
      return;
    }
  }

  # parent
  return $child_pid;
}
[/code]                    </div>
                </td>
            </tr>

            <tr>
                <td class="xar-norm author">
                    <a href="http://www.linux.com/archive/forums/topic/3894#top">
Back to top</a>
</td>

               <td class="xar-alt">

                            <a href="http://www.linux.com/archive/roles/383588">
                                <img src="modules/xarbb/xarimages/new/icon_profile.gif" alt="Profile" />
                            </a>
                            <a href="http://www.linux.com/archive/?module=roles&amp;func=email&amp;uid=383588">
                                <img src="modules/xarbb/xarimages/new/icon_email.gif" alt="Email" />
                           </a>
</td>
           </tr>
</table>
</div>

<div class="xarbb-topiccontrols">

<span class="xar-sub">
    <a href="http://www.linux.com/archive/forums">
Forum Index    </a>
    <span style="font-size: larger;">&#187;</span>
    <a href="http://www.linux.com/archive/forums/category/4181">
        Forums 
    </a>
    <span style="font-size: larger;">&#187;</span>
    <a href="http://www.linux.com/archive/forums/forum/5">
        Programming and Development   </a>
</span>

</div>



            </channel>
</rss>
