http://www.perlmonks.org?node_id=683632


in reply to Fork and exec

You can set $SIG{CHLD} to 'IGNORE' if you don't want to wait for status of your children. There are other ways, too, but that's the easy way. See perlipc and waitpid for more info.

The exit is there to make sure your child exits cleanly in case the exec() fails. You don't want each child going back and forking ten times because they can't exec the proper process for whatever reason, right? If that exec fails and you don't exit, you've unleashed a forkbomb on your machine. Your sysadmin might not be happy. See exec for more info.

BTW, your declaration of $a is missing a sigil, a for loop is more idiomatic when using a fixed count, and you're missing two important lines at the top:

use strict; use warnings; $SIG{CHLD} = 'IGNORE'; # my $a; # no longer necessary -- was a loop var where the loop # number range idiom will suffice for ( 1..10 ) { unless ( fork() ) { # Here I want execute and continue without # wait the status exec( 'echo', 'foo!' ); exit( 0 ); } }

Replies are listed 'Best First'.
Re^2: Fork and exec
by MidLifeXis (Monsignor) on Apr 30, 2008 at 17:16 UTC
    Your sysadmin might not be happy.

    That is putting it mildly :). I remember calling a professor as his machine started to alarm due to a self-inflicted (read: prof did it to self) fork bomb. Getting onto the machine and rebooting without losing data was, umm, fun.

    Thankfully, the prof happened to be one of the reasonable ones, and it was "his" machine, so other than the two of us, there was not a lot of impact :)

    --MidLifeXis

Re^2: Fork and exec
by perlmonkdr (Beadle) on Apr 30, 2008 at 23:10 UTC

    Thk 4 your reply

    I'm afraid not work, if i do something like this exec "perl -e sleep(20)"; the parent process wait for finish all childs, one little thing, i tested it on win, remember that in this OS Perl emulate fork, it works fine 4 U on *nix?

    Thk a lot