Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

how to create independent process?

by swaroop (Beadle)
on Feb 07, 2006 at 07:43 UTC ( #528440=perlquestion: print w/replies, xml ) Need Help??
swaroop has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I would like to create independent process (I mean PPID to 1) using fork and exec.

I will run sub command from main and would like to keep the sub command as independent of the main.

I had tried setsid , but no help !!!

* using setsid() : setsid creates a session to the process. But still the parent process will ( PPID ) be the main process. IF we give the TERM signal to the sub process it creates zombiee process if the main process is active.

* We can use SIG{CHLD} ignore or TERM ..but in linux its throwing some debug message in /var/log/messages. So , we need to use waitpid(). BUt I Don't want to work on that. I just want to run another command independent of the main ( ie .making init process as parent ).

Replies are listed 'Best First'.
Re: how to create independent process?
by izut (Chaplain) on Feb 07, 2006 at 09:04 UTC
    Check perlipc documentation. setsid() is part of the POSIX module.

    Igor 'izut' Sutton
    your code, your rules.

Re: how to create independent process?
by l.frankline (Hermit) on Feb 07, 2006 at 09:10 UTC

    If you are seeking for this example, then please go through with this code.

    use Win32::Process; Win32::Process::Create($ProcessObj, "C:\winnt\notepad.exe", "c:\sample.txt", 0, DETACHED_PROCESS, ".")|| die "cant find the application" +; }


    Don't put off till tomorrow, what you can do today.

Re: how to create independent process?
by svenXY (Deacon) on Feb 07, 2006 at 08:55 UTC
    can you please let us see some example code and what you have tried so far? And also, what you want to do from the new process that requires it to be so "independent" from the other process?
Re: how to create independent process?
by sgifford (Prior) on Feb 07, 2006 at 17:21 UTC
    You should fork twice. Have the new process run in the grandchild process, and have the middle process exit.

    You'll also want to call setsid, as you did, and probably close open file descriptors, especially if they're to a Web client or a terminal. There are some other steps that may also be useful, depending on what the process will be doing and how long it's going to run.

    Proc::Daemon wraps these all up into one easy function, and I'd be inclined to use that.

Re: how to create independent process?
by andThenThereWasPERL (Monk) on Feb 07, 2006 at 16:12 UTC
    my $pid; if (!defined ($pid = fork)) { die "Unable to fork: $!"; } elsif (! $pid) { # this is the branch for the child process # do stuff in the child process exit; # terminate the child process }

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2017-07-22 17:45 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (340 votes). Check out past polls.