Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From <a href="http://faq.perl.org/perlfaq8" rel="nofollow noreferrer">perlfaq8</a>'s answer to <a href="http://learn.perl.org/faq/perlfaq8.html#How-do-I-start-a-process-in-the-background" rel="nofollow noreferrer">How do I start a process in the background?</a></p> <hr> <p>Several modules can start other processes that do not block your Perl program. You can use IPC::Open3, Parallel::Jobs, IPC::Run, and some of the POE modules. See CPAN for more details.</p> <p>You could also use</p> <pre><code>system("cmd &amp;") </code></pre> <p>or you could use fork as documented in "fork" in perlfunc, with further examples in perlipc. Some things to be aware of, if you're on a Unix- like system:</p> <p><strong>STDIN, STDOUT, and STDERR are shared</strong></p> <p>Both the main process and the backgrounded one (the "child" process) share the same STDIN, STDOUT and STDERR filehandles. If both try to access them at once, strange things can happen. You may want to close or reopen these for the child. You can get around this with "open"ing a pipe (see "open" in perlfunc) but on some systems this means that the child process cannot outlive the parent.</p> <p><strong>Signals</strong></p> <p>You'll have to catch the SIGCHLD signal, and possibly SIGPIPE too. SIGCHLD is sent when the backgrounded process finishes. SIGPIPE is sent when you write to a filehandle whose child process has closed (an untrapped SIGPIPE can cause your program to silently die). This is not an issue with "system("cmd&amp;")".</p> <p><strong>Zombies</strong></p> <p>You have to be prepared to "reap" the child process when it finishes.</p> <pre><code> $SIG{CHLD} = sub { wait }; $SIG{CHLD} = 'IGNORE'; </code></pre> <p>You can also use a double fork. You immediately wait() for your first child, and the init daemon will wait() for your grandchild once it exits.</p> <pre><code> unless ($pid = fork) { unless (fork) { exec "what you really wanna do"; die "exec failed!"; } exit 0; } waitpid($pid, 0); </code></pre> <p>See "Signals" in perlipc for other examples of code to do this. Zombies are not an issue with "system("prog &amp;")".</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload