Open4::popen4

Discussion in 'Ruby' started by Ara.T.Howard, May 13, 2004.

  1. Ara.T.Howard

    Ara.T.Howard Guest

    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
    ===============================================================================
     
    Ara.T.Howard, May 13, 2004
    #1
    1. Advertising

  2. Ara.T.Howard

    Guest

    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.

    --
    Nobu Nakada
     
    , May 14, 2004
    #2
    1. Advertising

  3. Ara.T.Howard

    Ara.T.Howard Guest

    On Fri, 14 May 2004 wrote:

    > 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
    ===============================================================================
     
    Ara.T.Howard, May 14, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roman Yakovenko
    Replies:
    0
    Views:
    1,627
    Roman Yakovenko
    Sep 7, 2004
  2. Jeff Epler
    Replies:
    0
    Views:
    414
    Jeff Epler
    Sep 8, 2004
  3. [ANN] open4-0.2.0

    , Feb 14, 2006, in forum: Ruby
    Replies:
    0
    Views:
    99
  4. [ANN] open4-0.5.0

    , Aug 8, 2006, in forum: Ruby
    Replies:
    0
    Views:
    91
  5. [ANN] open4-0.5.1

    , Aug 22, 2006, in forum: Ruby
    Replies:
    0
    Views:
    122
Loading...

Share This Page