# RCR: IO.popen4

**URL:** <https://www.ruby-forum.com/t/rcr-io-popen4/190731>\
**Category:** Ruby\
**Created:** [June 25, 2010, 11:35pm UTC](https://www.ruby-forum.com/t/rcr-io-popen4/190731 "2010-06-25T23:35:44Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [June 25, 2010, 11:35pm UTC](https://www.ruby-forum.com/t/rcr-io-popen4/190731/1 "2010-06-25T23:35:44Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [June 26, 2010, 3:00am UTC](https://www.ruby-forum.com/t/rcr-io-popen4/190731/2 "2010-06-26T03:00:12Z")

</div>

2010/6/26 Roger P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> IO.popen(“ls”) # what’s the PID?

Use IO#pid.

> r,w,e,thread = Open3.popen3(“ls”) # what’s the PID?

Use thread.pid.

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [June 29, 2010, 4:41pm UTC](https://www.ruby-forum.com/t/rcr-io-popen4/190731/3 "2010-06-29T16:41:28Z")

</div>

Tanaka A. wrote:

> 2010/6/26 Roger P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > 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

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [June 30, 2010, 12:35pm UTC](https://www.ruby-forum.com/t/rcr-io-popen4/190731/4 "2010-06-30T12:35:08Z")

</div>

(2010/06/30 18:30), Charles Oliver N. 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.

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [July 1, 2010, 9:49pm UTC](https://www.ruby-forum.com/t/rcr-io-popen4/190731/5 "2010-07-01T21:49:38Z")

</div>

On Wed, Jun 30, 2010 at 5:34 AM, Urabe S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> (2010/06/30 18:30), Charles Oliver N. 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

---

<div class="post-metadata">

**Author:** ![rogerdpack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/45deac/32.png) [@rogerdpack](https://www.ruby-forum.com/u/rogerdpack)\
**Post date:** [June 30, 2010, 11:31am UTC](https://www.ruby-forum.com/t/rcr-io-popen4/190731/6 "2010-06-30T11:31:18Z")

</div>

On Fri, Jun 25, 2010 at 4:36 PM, Roger P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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
