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.

···

--
Posted via http://www.ruby-forum.com/.

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 Gray II

···

On Dec 18, 2010, at 12:10 PM, Tasos Laskos 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?

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?

···

--
Posted via http://www.ruby-forum.com/.

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.

···

--
Posted via http://www.ruby-forum.com/.

Cool, I should be set now. Thanks guys.

···

--
Posted via http://www.ruby-forum.com/.

*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 Gray II

···

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

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

James Edward Gray II

···

On Dec 18, 2010, at 12:54 PM, Tasos Laskos 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?

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

···

--
Posted via http://www.ruby-forum.com/.

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.

···

On Sun, 19 Dec 2010, James Edward Gray II wrote:

On Dec 18, 2010, at 12:34 PM, Tasos Laskos 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.