Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Fork and exec

by mr_mischief (Monsignor)
on Apr 30, 2008 at 06:16 UTC ( #683632=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://683632]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2018-08-20 23:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:









    Results (197 votes). Check out past polls.

    Notices?