os.Stdout和syscall.Stdout之间有什么区别?

huangapple go评论85阅读模式
英文:

What is the difference between os.Stdout and syscall.Stdout?

问题

我一直在尝试使用ForkExec()函数,但无法使其正常工作,syscall.Stdout和os.Stdout之间有什么区别吗?

以下是我尝试运行的代码示例:

command := "/usr/bin/echo"
args := []string{"Hello there."}
attr := new(syscall.ProcAttr)
attr.Env = os.Environ()
attr.Files = []uintptr{uintptr(syscall.Stdin), uintptr(syscall.Stdout), uintptr(syscall.Stderr)}
pid , err := syscall.ForkExec(command, args, attr)
if err != nil {
    log.Fatal(err)
}
fmt.Println(pid)

输出没有显示在屏幕上。

非常感谢您提前的帮助。

英文:

I have been trying to work ForkExec() and I am not able to get this one work, is there a difference between syscall.Stdout and os.Stdout?

Here is a small example of the code I am trying to run.

command := "/usr/bin/echo"
args := []string{"Hello there."}
attr := new(syscall.ProcAttr)
attr.Env = os.Environ()
attr.Files = []uintptr{uintptr(syscall.Stdin), uintptr(syscall.Stdout), uintptr(syscall.Stderr)}
pid , err := syscall.ForkExec(command, args, attr)
if err != nil {
    log.Fatal(err)
}
fmt.Println(pid)

The output is not showing up on the screen.

Thanks a lot for your help in advance.

答案1

得分: 5

os.Stdout 是一个 *os.File。它与需要 io.Writer 或类似接口的 Go 函数一起使用。syscall.Stdout 是一个整数常量。它是标准输出的文件描述符号,对于低级系统调用非常有用。

syscall.ForkExec 确实需要文件描述符号... 但不清楚为什么你要使用它,而不是更直接的 os/exec.Cmd

英文:

os.Stdout is a *os.File. It works with go functions that want an io.Writer or similar interfaces. syscall.Stdout is an integer constant. It's the file descriptor number of stdout, which is useful for low-level syscalls.

syscall.ForkExec does indeed want file descriptor numbers... but it's unclear why you're using that instead of os/exec.Cmd which is much more straightforward.

huangapple
  • 本文由 发表于 2021年7月31日 01:53:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/68595258.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定