Independent processes

Hi guys,

I’m having a bit of a problem…
Is there a way to completely detach child processes?

What I mean is, if I Kernel.fork() a piece of code and then the parent
gets killed I want the child to continue running.

Does anyone have an idea of how to do that?

Cheers,
Tasos L.

On Dec 18, 2010, at 12:10 PM, Tasos L. wrote:

I’m having a bit of a problem…
Is there a way to completely detach child processes?

What I mean is, if I Kernel.fork() a piece of code and then the parent
gets killed I want the child to continue running.

Does anyone have an idea of how to do that?

That’s how processes work, with no special action required:

$ cat parent_and_child.rb
fork do
5.times do
puts “Child still running…”
sleep 1
end
puts “Child exiting.”
end

2.times do
puts “Parent still running…”
sleep 1
end
puts “Parent exiting.”
$ ruby parent_and_child.rb
Parent still running…
Child still running…
Parent still running…
Child still running…
Parent exiting.
Child still running…
$ Child still running…
Child still running…
Child exiting.

James Edward G. II

Thanks for the response,

And that’s why I chose to work with Processes and not Threads.
I recalled that that’s their default behaviour but for some reason when
the parent XMLRPC server dies his children die too.
(The children are also XMLRPC servers serving a different purpose.)

That’s what’s bugging me…

Lightbulb
The children are listening on stdin too so they probably catch the
Ctrl+C interrupt and die as well.

But I don’t want that either…is there a way to separate them
completely or will I have to settle?

Thanks James, re-declaring the trap interrupt in the children worked. :slight_smile:

On Dec 18, 2010, at 12:34 PM, Tasos L. wrote:

Lightbulb
The children are listening on stdin too so they probably catch the
Ctrl+C interrupt and die as well.

That’s correct. By default they share STDIN.

But I don’t want that either…is there a way to separate them
completely or will I have to settle?

Sure. You could redirect STDIN (plus STDOUT and STDERR, if desired) in
the children:

$stdin.reopen("/dev/null", “r”)

or you could change the signal handling in the children:

trap(“INT”, “IGNORE”)

I hope that helps.

James Edward G. II

On Sun, 19 Dec 2010, James Edward G. II wrote:

Sure. You could redirect STDIN (plus STDOUT and STDERR, if desired) in the
children:

$stdin.reopen(“/dev/null”, “r”)

or you could change the signal handling in the children:

trap(“INT”, “IGNORE”)

I hope that helps.

Or maybe this is what you want: Proper way to daemonize - Ruby - Ruby-Forum

– Matt
It’s not what I know that counts.
It’s what I can remember in time to use.

No that wouldn’t work for me…

One last question, if I pass a singleton to each of the children do they
get a copy or will it be shared somehow?

During my tests there’s no overwriting of any values but I’d prefer not
to find out the hard way.

Any ideas?

Thanks for your help guys.

Cool, I should be set now. Thanks guys.

On Dec 18, 2010, at 12:54 PM, Tasos L. wrote:

One last question, if I pass a singleton to each of the children do they
get a copy or will it be shared somehow?

Memory isn’t shared between processes, unless you take steps to make it
so.

James Edward G. II