英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论