Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Independent processes

Reply
Thread Tools

Independent processes

 
 
Tasos Laskos
Guest
Posts: n/a
 
      12-18-2010
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/.

 
Reply With Quote
 
 
 
 
James Edward Gray II
Guest
Posts: n/a
 
      12-18-2010
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?


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

 
Reply With Quote
 
 
 
 
Tasos Laskos
Guest
Posts: n/a
 
      12-18-2010
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/.

 
Reply With Quote
 
James Edward Gray II
Guest
Posts: n/a
 
      12-18-2010
On Dec 18, 2010, at 12:34 PM, Tasos Laskos wrote:

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


 
Reply With Quote
 
Tasos Laskos
Guest
Posts: n/a
 
      12-18-2010
Thanks James, re-declaring the trap interrupt in the children worked.

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

 
Reply With Quote
 
Matt Lawrence
Guest
Posts: n/a
 
      12-18-2010
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.


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.

 
Reply With Quote
 
Tasos Laskos
Guest
Posts: n/a
 
      12-18-2010
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/.

 
Reply With Quote
 
James Edward Gray II
Guest
Posts: n/a
 
      12-18-2010
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?


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

James Edward Gray II

 
Reply With Quote
 
Tasos Laskos
Guest
Posts: n/a
 
      12-18-2010
Cool, I should be set now. Thanks guys.

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Controlling processes and what to "feed" other processes Marc Heiler Ruby 1 05-24-2009 05:37 PM
Independent processes john VHDL 3 01-10-2006 04:40 PM
Independent processes john VHDL 0 01-09-2006 07:43 PM
Route 4 T1 lines with independent VLANs mcarroll76@gmail.com Cisco 6 04-03-2005 10:01 PM
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM



Advertisments