Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Fork exec and not wait?

by packetstormer (Monk)
on Jan 24, 2013 at 19:10 UTC ( #1015210=perlquestion: print w/ replies, xml ) Need Help??
packetstormer has asked for the wisdom of the Perl Monks concerning the following question:

Good evening
I was reading perldoc about fork and noticed this:

"Calling exec() within a pseudo-process actually spawns the requested executable in a separate process and waits for it to complete before exiting with the same exit status as that process"

I have noticed that if my forked process returns any output the parent process waits and in turn splats my application!
Is there anyway to either fork a process and force its output to /dev/null or simply make the parent ignore the child and continue?

I need to grab the PID of the fork to manage it later..

Comment on Fork exec and not wait?
Re: Fork exec and not wait?
by eyepopslikeamosquito (Canon) on Jan 24, 2013 at 19:30 UTC

    Your perldoc quote "about fork" namely:

    Calling exec() within a pseudo-process actually spawns the requested executable in a separate process and waits for it to complete before exiting with the same exit status as that process
    is not from fork but from perlfork. This pseudo-process business only applies to Windows, not to Unix. Which platform are you on?

    I have noticed that if my forked process returns any output the parent process waits and in turn splats my application!
    I have no idea what this means. Please clarify.

    Is there anyway to either fork a process and force its output to /dev/null or simply make the parent ignore the child and continue?
    Why would you want to do that? You are going to have supply more context and describe what you are trying to achieve. This is looking like an XY Problem.

      I am running Unix

      I have noticed that if my forked process returns any output the parent process waits and in turn splats my application!
      I notice that when I fork, in my script, and if the forked process has any form of output (that would usually go to STDOUT when run from a terminal) the fork never finished, and in turn halts my main program (or, to the user - splats it!)

      Is there anyway to either fork a process and force its output to /dev/null or simply make the parent ignore the child and continue?
      Why would you want to do that? You are going to have supply more context and describe what you are trying to achieve.

      I want to achieve this to avoid the problem I outlined above. I don't mind if I could catch the return code or output and then move on, but as I explain above and output from the forked process causes the script to wait, indefinitely.

        What you are saying is not making sense to me and does not agree with my experience of using fork on Unix. In my experience, Perl's fork and exec are rock-solid on Unix. Have you used them successfully before?

        You are going to have to post some code. If your code is too long and complicated to post, how about posting a short sample program that forks and then execs a simple Unix executable (the "ls" command say). Make sure to include error checking in this little sample program and be sure to capture the stdout and stderr of the "ls" command along with its exit code. If you do that, we may be able to spot what you are doing wrong. (An example of a more complicated sample Unix Perl program that uses fork and exec can be found here).

        Is there anyway to either fork a process and force its output to /dev/null or simply make the parent ignore the child and continue?

        You can literally accomplish this task by redirecting your STDOUT and STDERR to /dev/null using open in the child after your fork:

        open STDOUT, '>', '/dev/null' or die "Redirect failed: $!"; open STDERR, '>', '/dev/null' or die "Redirect failed: $!";

        You'd probably combine this in an instruction to not bother waiting to reap the child (see perlipc):

        $SIG{CHLD} = "IGNORE";

        However, you are treating a symptom here.As eyepopslikeamosquito says, this sounds like an XY Problem. I suspect your dissection of the system is incorrect, and what's actually happening is your child is hanging on STDIN that's not coming. Post code so we can actually diagnose this and fix it.


        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Fork exec and not wait?
by dasgar (Deacon) on Jan 24, 2013 at 19:53 UTC

    If I'm understanding you correctly, you're wanting to have your script spawn something that will go off and do some work. Then have the main script ignore what was spawned off and continue on without any delays. Is that correct?

    The thought that popped into my mind was to use threads instead of forking. Just create a new thread and then detach it (or have the thread detach itself).

      If I'm understanding you correctly, you're wanting to have your script spawn something that will go off and do some work. Then have the main script ignore what was spawned off and continue on without any delays. Is that correct?

      This is it exactly - yes. I maybe should have said that it I am looking for something comparable to "&" in Unix. Thanks for the suggestion, I will take a look at threads.

        If you want something comparable to "&" in Unix, I recommend fork and exec, not threads. If you want sound advice from us, rather than random guessing, you will need to provide more background and context of your application and what you are trying to achieve. And you will need to post some code.

Re: Fork exec and not wait?
by zentara (Archbishop) on Jan 25, 2013 at 09:20 UTC
    Is there anyway to either fork a process and force its output to /dev/null or simply make the parent ignore the child and continue?

    Possibly using

    $SIG{CHLD} = 'IGNORE';
    will work, I didn't see anyone mention it yet.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2014-09-20 02:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (151 votes), past polls