# RCR: IO.popen4

**URL:** <https://rubytalk.org/t/rcr-io-popen4/59003>\
**Category:** ruby-talk\
**Created:** [25 June 2010 21:36 UTC](https://rubytalk.org/t/rcr-io-popen4/59003 "2010-06-25T21:36:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [25 June 2010 21:36 UTC](https://rubytalk.org/t/rcr-io-popen4/59003/1 "2010-06-25T21:36:12Z")

</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  
&nbsp;&nbsp;Timeout.timeout(1) {  
&nbsp;&nbsp;&nbsp;&nbsp;system("bash /c sleep")  
&nbsp;&nbsp;}  
rescue Timeout::Error  
&nbsp;&nbsp;# 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/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Tanaka\_Akira](https://avatars.discourse-cdn.com/v4/letter/t/439d5e/32.png) [@Tanaka\_Akira](https://rubytalk.org/u/Tanaka_Akira)\
**Post date:** [26 June 2010 00:59 UTC](https://rubytalk.org/t/rcr-io-popen4/59003/2 "2010-06-26T00:59:33Z")

</div>

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

Use IO#pid.

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

Use thread.pid.

> **···**
>
> 2010/6/26 Roger Pack \<rogerpack2005@gmail.com\>:  
> --  
> Tanaka Akira

---

<div class="post-metadata">

**Author:** ![Charles\_Nutter](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/charles_nutter/32/2567_2.png) [@Charles\_Nutter](https://rubytalk.org/u/Charles_Nutter)\
**Post date:** [30 June 2010 09:30 UTC](https://rubytalk.org/t/rcr-io-popen4/59003/3 "2010-06-30T09:30:53Z")

</div>

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

> **···**
>
> On Fri, Jun 25, 2010 at 4:36 PM, Roger Pack \<rogerpack2005@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.

---

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [29 June 2010 14:41 UTC](https://rubytalk.org/t/rcr-io-popen4/59003/4 "2010-06-29T14:41:31Z")

</div>

Tanaka Akira wrote:

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

> **···**
>
> > 2010/6/26 Roger Pack \<rogerpack2005@gmail.com\>:
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Urabe\_Shyouhei1](https://avatars.discourse-cdn.com/v4/letter/u/6a8cbe/32.png) [@Urabe\_Shyouhei1](https://rubytalk.org/u/Urabe_Shyouhei1)\
**Post date:** [30 June 2010 10:34 UTC](https://rubytalk.org/t/rcr-io-popen4/59003/5 "2010-06-30T10:34:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Charles\_Nutter](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/charles_nutter/32/2567_2.png) [@Charles\_Nutter](https://rubytalk.org/u/Charles_Nutter)\
**Post date:** [1 July 2010 19:48 UTC](https://rubytalk.org/t/rcr-io-popen4/59003/6 "2010-07-01T19:48:45Z")

</div>

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

> **···**
>
> On Wed, Jun 30, 2010 at 5:34 AM, Urabe Shyouhei \<shyouhei@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.
