如何从子进程中的`exec.Cmd` extraFiles fd中读取?

时间:2015-04-09 02:34:16

标签: go

我从golang.org读到了解释,如下所示。

// ExtraFiles specifies additional open files to be inherited by the
// new process. It does not include standard input, standard output, or
// standard error. If non-nil, entry i becomes file descriptor 3+i.
//
// BUG: on OS X 10.6, child processes may sometimes inherit unwanted fds.
// http://golang.org/issue/2603
ExtraFiles []*os.File

我不太了解它吗?例如,我在下面有这样的代码。

cmd := &exec.Cmd{
    Path: init,
    Args: initArgs,
}
cmd.Stdin = Stdin
cmd.Stdout = Stdout
cmd.Stderr = Stderr
cmd.Dir = Rootfs
cmd.ExtraFiles = []*os.File{childPipe}

这是否意味着,因为我在cmd.ExtraFiles = []*os.File{childPipe}写了一个小便管,我可以直接写fd 3来使用它。

pipe = os.NewFile(uintptr(3), "pipe")
json.NewEncoder(pipe).Encode(newThing)

谢谢,如果有人可以提供帮助!

1 个答案:

答案 0 :(得分:4)

正确;您可以通过创建一个新的*File来读取管道,其文件描述符是子管道的描述符。下面是从子进程到父进程的管道数据示例:

<强>父:

package main

import (
    "fmt"
    "os/exec"
    "os"
    "encoding/json"
)

func main() {
    init := "child"
    initArgs := []string{"hello world"}

    r, w, err := os.Pipe()
    if err != nil {
        panic(err)
    }

    cmd := exec.Command(init, initArgs...)
    cmd.Stdin = os.Stdin
    cmd.Stdout = os.Stdout
    cmd.Stderr = os.Stderr
    cmd.ExtraFiles = []*os.File{w}

    if err := cmd.Start(); err != nil {
        panic(err)
    }
    var data interface{}
    decoder := json.NewDecoder(r)
    if err := decoder.Decode(&data); err != nil {
        panic(err)
    }
    fmt.Printf("Data received from child pipe: %v\n", data)
}

儿童:

package main

import (
    "os"
    "encoding/json"
    "strings"
    "fmt"
)

func main() {
    if len(os.Args) < 2 {
        os.Exit(1)
    }
    arg := strings.ToUpper(os.Args[1])

    pipe := os.NewFile(uintptr(3), "pipe")
    err := json.NewEncoder(pipe).Encode(arg)
    if err != nil {
        panic(err)
    }
    fmt.Println("This message printed to standard output, not to the pipe")
}