Open4::popen4

A

Ara.T.Howard

blatant rip-off of Open3::popen3 - does anyone think something like this might
be useful (to have a handle on the spawned processes' pid?)

-a


# open4.rb: Spawn a program like popen, but with stderr, and pid, too. You
# might also want to use this if you want to bypass the shell. (By passing
# multiple args, which IO#popen does not allow)
#
# Usage:
# require "open4"
#
# stdin, stdout, stderr, pid = Open4.popen4('nroff -man')
# or
# include Open4
# stdin, stdout, stderr, pid = popen4('nroff -man')

module Open4
#[stdin, stdout, stderr, pid] = popen4(command);
def popen4(*cmd)
pw = IO::pipe # pipe[0] for read, pipe[1] for write
pr = IO::pipe
pe = IO::pipe
pc = IO::pipe

pid = fork{
# child
gcid =
fork{
# grandchild
pw[1].close
STDIN.reopen(pw[0])
pw[0].close

pr[0].close
STDOUT.reopen(pr[1])
pr[1].close

pe[0].close
STDERR.reopen(pe[1])
pe[1].close

pc[1].close

exec(*cmd)
}
pc[0].close
pc[1].puts gcid
pc[1].close
exit!
}

pc[1].close
gcid = pc[0].read.to_i

pw[0].close
pr[1].close
pe[1].close
Process.waitpid(pid)
pi = [pw[1], pr[0], pe[0]]
pw[1].sync = true
if defined? yield
begin
return yield(*pi)
ensure
pi.each{|p| p.close unless p.closed?}
end
end
[pw[1], pr[0], pe[0], gcid]
end
module_function :popen4
end

if $0 == __FILE__
a = Open4.popen4("nroff -man")
Thread.start do
while line = gets
a[0].print line
end
a[0].close
end
while line = a[1].gets
print ":", line
end
end


--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| URL :: http://www.ngdc.noaa.gov/stp/
| TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done
===============================================================================
 
N

nobu.nokada

Hi,

At Fri, 14 May 2004 03:33:56 +0900,
Ara.T.Howard wrote in [ruby-talk:100167]:
blatant rip-off of Open3::popen3 - does anyone think something like this might
be useful (to have a handle on the spawned processes' pid?)

wait(2) can't wait a grand child process. It doesn't seem so
useful.
 
A

Ara.T.Howard

Hi,

At Fri, 14 May 2004 03:33:56 +0900,
Ara.T.Howard wrote in [ruby-talk:100167]:
blatant rip-off of Open3::popen3 - does anyone think something like this might
be useful (to have a handle on the spawned processes' pid?)

wait(2) can't wait a grand child process. It doesn't seem so
useful.

what i had wanted was

Process.kill -9, gcid

i didn't know that about wait(2)...

-a
--
===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| URL :: http://www.ngdc.noaa.gov/stp/
| TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done
===============================================================================
 

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

Similar Threads

open4 stdin.close required? 0
[ANN] open4-0.8.0 2
[ANN] open4-0.9.2 0
[ANN] open4-0.9.0 0
[ANN] open4-0.5.1 0
[ANN] open4-0.6.0 0
[ANN] open4-0.5.0 0
Communicating between processes 0

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top