匿名管道

发布于 2024-09-15 06:39:25 字数 978 浏览 4 评论 0原文

我编写了两个使用匿名管道进行通信的简短程序。父进程通过为子进程设置标准 IO 句柄来共享管道句柄:

// -- Set STARTUPINFO for the spawned process -------------------------
ZeroMemory(&m_ChildSI, sizeof(STARTUPINFO));
GetStartupInfo(&m_ChildSI);

m_ChildSI.dwFlags       = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
m_ChildSI.wShowWindow   = SW_HIDE;
m_ChildSI.hStdError     = m_pipeChild.WritePipeHandle();
m_ChildSI.hStdOutput    = m_pipeChild.WritePipeHandle();
m_ChildSI.hStdInput     = m_pipeParent.ReadPipeHandle();

子进程通过调用 GetStdHandle

hReadPipe = GetStdHandle(STD_INPUT_HANDLE)

我的问题是: 管道句柄由调用 CloseHandle< 的父进程创建一旦父母和孩子完成通信,就对他们进行 /a>() 。

孩子是否也必须调用 CloseHandle() ?我在想,因为这些是标准 IO 句柄,所以当进程折叠时它们会自动释放。

谢谢!

I've written a two short programs that use anonymous pipes to communicate. The parent process shares the pipe handles by setting the standard IO handles for the child:

// -- Set STARTUPINFO for the spawned process -------------------------
ZeroMemory(&m_ChildSI, sizeof(STARTUPINFO));
GetStartupInfo(&m_ChildSI);

m_ChildSI.dwFlags       = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
m_ChildSI.wShowWindow   = SW_HIDE;
m_ChildSI.hStdError     = m_pipeChild.WritePipeHandle();
m_ChildSI.hStdOutput    = m_pipeChild.WritePipeHandle();
m_ChildSI.hStdInput     = m_pipeParent.ReadPipeHandle();

The child acquires a read pipe handle with a call to GetStdHandle:

hReadPipe = GetStdHandle(STD_INPUT_HANDLE)

My question is:
The pipe handles are created by the parent process that calls CloseHandle() on them, once parent and child have finished communication.

Does the child also have to call CloseHandle() also? I was thinking that because these are the standard IO handles, that they'd be automatically deallocated when the process folds.

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拥醉 2024-09-22 06:39:25

在 Win32 上,内核对象(例如管道)由一个或多个用户模式句柄引用。当所有句柄都关闭时,底层对象就可以关闭。

每个进程中的句柄虽然可能具有相同的值,并且可能引用相同的对象,但它们是不同的句柄,应该单独关闭。

On Win32, kernel objects such as pipes are references by one or more user mode handles. When all handles are closed, the underlying object can be closed.

The handles in each process, while they might have the same value, and might refer to the same object, are different handles, and should be closed separately.

深空失忆 2024-09-22 06:39:25

我刚刚在文档 管道句柄继承 指出:

“当子进程完成管道操作时,它应该通过调用 CloseHandle 或终止来关闭管道句柄,这会自动关闭句柄。”

I just read in the document Pipe Handle Inheritance on MSDN that:

"When the child has finished with the pipe, it should close the pipe handle by calling CloseHandle or by terminating, which automatically closes the handle."

蓝眼睛不忧郁 2024-09-22 06:39:25

当应用程序终止时,任何句柄都可以保持未关闭状态,Windows 将自动释放资源。但最好手动关闭它们,这样一切都合乎逻辑且连贯。当代码被重用或现代化时,保持句柄打开可能会导致错误和泄漏。

Any handle can be left unclosed when application terminates, Windows will free resources automatically. But it is better practice to close them manually so everything is logical and coherent. Leaving handles opened can lead to bugs and leaks when the code is reused or modernized.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文