I am experimenting with using the fork and exec functions to spawn a child process and execute a different script. My ultimate goal is to spawn multiple child processes from the parent without waiting for any of the children to complete. My first attempt is to merely spawn a single child. The parent code is as follows:

#!/usr/bin/perl use strict; use warnings; my $pid; if( $pid = fork ){ print "In test1 the parent pid is $$; the child pid is $pid\n"; } else { die "could not fork a process: $!" unless defined $pid; exec ( "/liwidata/dev/tmp/jmg/dev/library/test2.pl" ) or print STD +ERR "couldn't exec test program: $!"; } print "All done!\n";

The child code is as follows:

#!/usr/bin/perl use strict; use warnings; print "In test2.pl the pid is $$\n";

When I execute the parent script I receive the following output:

/liwidata/dev/tmp/jmg/dev/library> ./test1.pl In test1 the parent pid is 21348; the child pid is 21349 All done! /liwidata/dev/tmp/jmg/dev/library> In test2.pl the pid is 21349

As you can see the command prompt does not appear at the conclusion of the child script. I must enter a Ctrl-C in order to get it back. What am I doing wrong?


In reply to exec function does not return command prompt by casman46

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.