Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Fork exec and not wait?

by eyepopslikeamosquito (Canon)
on Jan 24, 2013 at 19:30 UTC ( #1015216=note: print w/ replies, xml ) Need Help??


in reply to Fork exec and not wait?

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.


Comment on Re: Fork exec and not wait?
Re^2: Fork exec and not wait?
by packetstormer (Monk) on Jan 24, 2013 at 19:39 UTC
    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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2014-08-22 21:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (167 votes), past polls