gpt4 book ai didi

winapi - 无缓冲 CreateNamedPipe 用作 CreateProcess 的标准输出

转载 作者:行者123 更新时间:2023-12-01 01:36:27 28 4
gpt4 key购买 nike

我想执行任意命令行应用程序并在生成时读取其标准输出。我用 CreateNamedPipe创建一个管道,然后将另一端(打开使用 CreateFile)供应到 CreateProcess .如果目标进程没有明确地使用标准输出缓冲进行操作,是否有办法确保有问题的管道没有缓冲,或者至少系统最小值用作缓冲区大小?

最佳答案

您无法真正控制缓冲区大小。您可以将读取和写入缓冲区大小传递给 CreateNamedPipe ,但内核会自动增加这些缓冲区大小。基本上,缓冲区将始终至少与在任何给定时间准备读取的最大数据量一样大。换句话说,您对可用数据的响应速度越快,写入管道的数据块越小,保留的缓冲区就越小。

The input and output buffer sizes are advisory. The actual buffer size reserved for each end of the named pipe is either the system default, the system minimum or maximum, or the specified size rounded up to the next allocation boundary. ... Whenever a pipe write operation occurs, the system first tries to charge the memory against the pipe write quota. ... If the remaining pipe write quota is too small to fulfill the request, the system will try to expand the buffers to accommodate the data using nonpaged pool reserved for the process.



但是,我认为缓冲区大小并不重要。管道不会延迟数据的发送,直到缓冲区“已满”,并且没有与 TCP 的“nagle”选项等效的选项,因此保持较小的缓冲区大小不会改善延迟。

请记住,当您将管道连接到控制台应用程序的 stdout 时,输出通常在写入管道之前由该应用程序缓冲。如果您想要无缓冲输出,则需要使用 stderr。

此外,在使用继承的管道句柄时需要注意的是,生成的应用程序将继承您的所有句柄,因此,如果您打开了一个文件或套接字,则生成一个应用程序,然后关闭该句柄、文件/套接字等.将保持打开状态,直到产生的子进程停止,这可能导致意外的共享冲突和其他奇怪的问题。

关于winapi - 无缓冲 CreateNamedPipe 用作 CreateProcess 的标准输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1569425/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com