Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

detached process in windows

by Random_Walk (Prior)
on Jun 19, 2006 at 09:16 UTC ( #556209=perlquestion: print w/replies, xml ) Need Help??
Random_Walk has asked for the wisdom of the Perl Monks concerning the following question:

Great and wise monks,

I am fighting the dragon of windows and fear to say the great wyrm is winning. I need to have a parent process launch a child which will run detached from the parent. The parent should be able to carry on running once the child is launched. When the parent exits the child should carry on regardless.

I have a small bit of test code but I am getting odd behaviour...

#!/usr/bin/perl use strict; use warnings; $SIG{CHLD} = 'IGNORE'; $|++; if (@ARGV) { payload(@ARGV) } else { for (1..3) { my $pid = fork; die "failed to forking fork\n" unless defined $pid; if ($pid) { # parent, loop on ... } else { # child, run the payload print "I am a child\n"; `wperl $0 little_forker $_`; print "wperl launched\n"; } } } exit; sub payload { my ($type, $id) = @_; sleep 30; # ZZZzzzz..... open my $fh, '>>', "C:/tmp/$type.$id.log"; print $fh "number $id of type $type ran OK\n"; close $fh; exit; }
the three children are spawned and run on OK (the output files are written) even if CTRL-C out of the parent. The odd bit is that unless I CTRL-C the parent never exits, not even after the children are done. Also it only ever prints one line of output though the correct running of three children leads me to expect something more. Here I hit CTRL-C in the end about 30 secs after the children had finnished.
C:\Random>perl I am a child Terminating on signal SIGINT(2)
I am not getting a warm fuzzy feeling from this. How can I do it better ?


Pereant, qui ante nos nostra dixerunt!

Replies are listed 'Best First'.
Re: detached process in windows
by BrowserUk (Pope) on Jun 19, 2006 at 10:53 UTC

    The simplest way is to use the barely documented system-with-a-first-argument-of-1 asynchronous system. It's not portable outside of Win32, but it is roughy analogous to appending '&' to the end of a unix command.

    #! perl -slw use strict; if( @ARGV ) { print 'I am the child. I got args: ', join', ', @ARGV; sleep 10; print 'Child ended'; } else { print 'I am the parent'; for ( 1 .. 3 ) { print "Starting child $_"; system 1, $0, 'An arg', $_; } print 'Parent continuing'; sleep 10; print 'parent exiting'; } __END__ c:\test>junk I am the parent Starting child 1 Starting child 2 Starting child 3 Parent continuing I am the child. I got args: An arg, 2 I am the child. I got args: An arg, 3 I am the child. I got args: An arg, 1 parent exiting c:\test>Child ended Child ended Child ended

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: detached process in windows
by bart (Canon) on Jun 19, 2006 at 10:15 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://556209]
Approved by Joost
and God said, "Let Newton be!"...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2017-03-29 05:40 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (343 votes). Check out past polls.