closeAfterStart在exec中的目的是什么?

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

What is the purpose of closeAfterStart in exec

问题

我正在阅读go exec源代码。https://cs.opensource.google/go/go/+/refs/tags/go1.17.3:src/os/exec/exec.go

当调用Stdinpipe时,读取器被添加到closeAfterStart数组中。当调用Start()时,读取器被关闭。我不确定为什么他们在启动进程后立即关闭读取器。

英文:

I'm reading go exec source code. https://cs.opensource.google/go/go/+/refs/tags/go1.17.3:src/os/exec/exec.go

When Stdinpipe is called, the reader is added to an array closeAfterStart. When Start() is called, the reader is closed. I'm not sure to understand why they close the reader just after starting the process.

答案1

得分: 0

为了反映Penélope Stevens所说的,os.Pipe映射到底层的os文件管道。当通过os.Pipe返回的*os.File关闭时,它已经被传递给新生成的进程。关闭操作将关闭该进程中的文件描述符,但生成的进程仍然可以从该管道读取/写入。

文件描述符在这里被获取:https://cs.opensource.google/go/go/+/refs/tags/go1.17.3:src/os/exec/exec.go;l=404-415;drc=refs%2Ftags%2Fgo1.17.3

然后通过ProcAttr传递给生成的进程:https://pkg.go.dev/os#ProcAttr

英文:

To mirror what Penélope Stevens is saying, os.Pipe maps to an underlying os File pipe. By the time the *os.File returned by os.Pipe is closed it has already been passed to the new spawned process. The close will close the file descriptor in this process, but the spawned process can still read/write from that pipe.

The file descriptor is grabbed here: https://cs.opensource.google/go/go/+/refs/tags/go1.17.3:src/os/exec/exec.go;l=404-415;drc=refs%2Ftags%2Fgo1.17.3

And then passed to the spawned process with ProcAttr: https://pkg.go.dev/os#ProcAttr

huangapple
  • 本文由 发表于 2021年12月1日 03:55:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/70175281.html
匿名

发表评论

匿名网友

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

确定