Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re^2: Fork exec and not wait?

by packetstormer (Monk)
on Jan 24, 2013 at 19:39 UTC ( #1015218=note: print w/replies, xml ) Need Help??

in reply to Re: Fork exec and not wait?
in thread Fork exec and not wait?

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.

Replies are listed 'Best First'.
Re^3: Fork exec and not wait?
by eyepopslikeamosquito (Chancellor) on Jan 24, 2013 at 19:54 UTC

    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).

Re^3: Fork exec and not wait?
by kennethk (Abbot) on Jan 24, 2013 at 20:06 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?

    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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1015218]
[ambrus]: hallo, cbstream still working?
[ambrus]: nice, it rarely runs for so long without having to restart it

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2018-05-24 22:24 GMT
Find Nodes?
    Voting Booth?