Independent processes

T

Tasos Laskos

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

James Edward Gray II

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

Tasos Laskos

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?
 
J

James Edward Gray II

*Lightbulb*
The children are listening on stdin too so they probably catch the=20
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=20
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
 
M

Matt Lawrence

That's correct. By default they share STDIN.


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: http://www.ruby-forum.com/topic/185221

-- Matt
It's not what I know that counts.
It's what I can remember in time to use.
 
T

Tasos Laskos

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

James Edward Gray II

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top