Fork + exec + caller must not wait for a child

I have two scripts script_a and script_b. script_a calls script_b. script_b creates two processes. As shown below.

script_a waits for the completion of the parent and child elements of script_b. I want script_a to continue without waiting for the script_b child process.

What I did for this in script_b, I added the following code.

if (! $f_id) {
    close STDOUT;
    close STDERR;
    exec("sleep 10; echo 'i am child'");
}

This works for me. script_a is no longer waiting for a child process.

My question here is 1. Is this done correctly? 2. Do the parent and child processes share the same STDOUT and STDERR, and I get into trouble if there is a race condition? 3. Are there any better ways to do this?

Thanks in advance for your help.

script_a.pl

#! /usr/local/bin/perl
print `script_b.pl`;

script_b.pl

#! /usr/local/bin/perl

$f_id = fork();

if (! $f_id) {
    exec("sleep 10; echo 'i am child'");
}

if ($f_id) {
    print "i am parent\n";
}
+3
source share
2 answers

`` script_a. STDOUT STDERR script_a. -

script_a

system("script_b.pl > /var/tmp/out_file 2>&1");

script_b

#! /usr/local/bin/perl

$f_id = fork();

if (! $f_id) {
    exec("sleep 10; echo 'i am child'");
}

if ($f_id) {
    print "i am parent\n";
}

, exec.

.

+1

`` backticks perl . , , , sytem (command &) .

0

All Articles