Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Fork and exec

by perlmonkdr (Beadle)
on Apr 30, 2008 at 05:51 UTC ( #683628=perlquestion: print w/replies, xml ) Need Help??
perlmonkdr has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys!!!

I working with fork and exec, basicaly i would like to execute 10 times (at the same moment) a program but i don't want wait that those programs finish, so, i think in exec, but when i use fork, it still wait the status of the program.

Exists a way or method to do this?

Let me give you an example:

my a=0; while (++$a <= 10) { unless (fork()) { # Here I want execute and continue without # wait the status exec( 'perl'); #This exit isn't necessary, isn't it? exit(0); } }

Any comment are welcome, thk U so much.

Replies are listed 'Best First'.
Re: Fork and exec
by mr_mischief (Monsignor) on Apr 30, 2008 at 06:16 UTC
    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 ); } }
      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 :)


      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

Re: Fork and exec
by Anonymous Monk on Apr 30, 2008 at 06:19 UTC

      Thk, i was checked but this's not that i have in main

      Anyway i take this like a posible solution. thk

Re: Fork and exec
by pc88mxer (Vicar) on Apr 30, 2008 at 14:54 UTC
    but when i use fork, it still wait the status of the program.
    I gather that your parent is hanging someplace where you don't want it to hang. Are you calling wait or waitpid in the parent?

    If you don't need the exit status of a child process, you never need to call wait or waitpid or handle SIGCHLD. A 'zombie' process will be created when the child exits, but this is simply the child's entry in the system's process table and doesn't consume any appreciable memory. Zombie entries will be released when the parent exits.

    If you want to check if a child has exited without hanging, then call waitpid with the WNOHANG parameter - see perldoc -f waitpid for an example.

      Ok, but i'm not using wait, waitpid or signal in this example and the parent process wait to all childs finish, please not that i use this in windows, perhaps this the problem, isn't it?


        Windows has no fork but it can be emulated. See perlfork. Over on the perldoc for perlport I see that system(1, @args) will spawn an external process without waiting for the child to terminate.

        Disclaimer: I don't do windows.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://683628]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2017-05-23 23:17 GMT
Find Nodes?
    Voting Booth?