From: Roger Pack on
Currently with the std lib there is no (built in) way to get the PID of
a currently running child process (short of using fork, which is
unavailable on windows).

i.e.

IO.popen("ls") # what's the PID?
r,w,e,thread = Open3.popen3("ls") # what's the PID?



begin
Timeout.timeout(1) {
system("bash /c sleep")
}
rescue Timeout::Error
# what's the pid, so I can kill it?
end

Jruby has come up with a new method,

pid,r,e,w = IO.popen4("ls")

suggestion: this should be a part of std lib, for the reasons I
discussed above.
Any thoughts on this one?
Thanks.
-rp
--
Posted via http://www.ruby-forum.com/.

From: Tanaka Akira on
2010/6/26 Roger Pack <rogerpack2005(a)gmail.com>:

> IO.popen("ls") # what's the PID?

Use IO#pid.

> r,w,e,thread = Open3.popen3("ls") # what's the PID?

Use thread.pid.
--
Tanaka Akira

From: Roger Pack on
Tanaka Akira wrote:
> 2010/6/26 Roger Pack <rogerpack2005(a)gmail.com>:
>
>> IO.popen("ls") # what's the PID?
>
> Use IO#pid.
>
>> r,w,e,thread = Open3.popen3("ls") # what's the PID?
>
> Use thread.pid.

Oh cool thanks.
On 1.8 there appears to be no thread...

Open3.popen3("ls")
=> [#<IO:0xf7323c30>, #<IO:0xf7323bf4>, #<IO:0xf7323b90>]

Any option there?

Also wouldn't it be good to have a popen4 method so you can avoid
starting an extra thread (though I suppose that Process.spawn already
gives us that functionality...)

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

From: Charles Oliver Nutter on
On Fri, Jun 25, 2010 at 4:36 PM, Roger Pack <rogerpack2005(a)gmail.com> wrote:
> Jruby has come up with a new method,
>
> pid,r,e,w = IO.popen4("ls")
>
> suggestion: this should be a part of std lib, for the reasons I
> discussed above.

We added both open3 and open4 because the existing implementations
used fork. open3 is just all three streams and open4 also provides the
pid. open3 is of course normally from require 'open3', which in JRuby
now just uses the builtin open3. open4 is equivalent to the open4 gem,
which also uses fork and can't work on JRuby.

- Charlie

From: Charles Oliver Nutter on
On Wed, Jun 30, 2010 at 5:34 AM, Urabe Shyouhei <shyouhei(a)ruby-lang.org> wrote:
> (2010/06/30 18:30), Charles Oliver Nutter wrote:
>> We added both open3 and open4 because the existing implementations
>> used fork.
>
> You might already be aware of the fact, that 1.9's open3 no longer uses fork.

I did not, but this may help. I believe both spawn and detach should
be possible in JRuby, and the JDK's process-wrangling APIs work in a
similar way already.

We'll still need a custom version for 1.8 mode though.

- Charlie