对于stdout和stderr,Process.Start的缓冲区大小是多少?

时间:2017-04-04 10:28:03

标签: c#

我试图找出这些信息。 显然,Microsoft没有提供代码来可靠地读取进程stdoutstderr而没有死锁,异常和其他问题。

死锁问题

http://www.codeducky.org/process-handling-net/

  

一个不太明显的问题是死锁问题。所有三个流程流(输入,输出和错误)都是有限的,它们可以缓冲多少内容。如果内部缓冲区填满,那么写入流的任何人都将阻止。例如,在此代码中,在进程退出之前,我们不会读出out和错误流。这意味着我们可以发现自己处于外部进程耗尽错误缓冲区的情况。在这种情况下,外部进程会阻止写入标准错误,而我们的.NET应用程序被阻止读取到标准输出结束。因此,我们发现自己处于僵局!

互联网上没有可靠的代码

有265次赞成回答,但我不打算使用它,因为它受ObjectDisposeException的影响并需要超时:
ProcessStartInfo hanging on "WaitForExit"? Why?

在被发现它可以导致ObjectDisposeException之前,它可能被推翻了。

遇到死锁的可能性有多大?

我想知道陷入僵局的可能性有多大。 为此,我需要知道windows 7下stderr和stdout的缓冲区大小。

如何找到它们? 我以为我可以捕获几个文件来查看大约文件大小会导致什么问题,但是Windows 7下没有猫。我甚至尝试使用git cat-file但它有错误的文档,没有使用示例,没有人回答过关于这个的问题:https://stackoverflow.com/questions/43203902/how-to-git-cat-file-a-file-in-current-dir

1 个答案:

答案 0 :(得分:-1)

John的评论是正确的,它的硬编码为4096字节。

https://github.com/Microsoft/referencesource/blob/master/System/services/monitoring/system/diagnosticts/Process.cs

    if (startInfo.RedirectStandardInput) {
            standardInput = new StreamWriter(new FileStream(standardInputWritePipeHandle, FileAccess.Write, 4096, false), Console.InputEncoding, 4096);
            standardInput.AutoFlush = true;
        }
        if (startInfo.RedirectStandardOutput) {
            Encoding enc = (startInfo.StandardOutputEncoding != null) ? startInfo.StandardOutputEncoding : Console.OutputEncoding;
            standardOutput = new StreamReader(new FileStream(standardOutputReadPipeHandle, FileAccess.Read, 4096, false), enc, true, 4096);
        }
        if (startInfo.RedirectStandardError) {
            Encoding enc = (startInfo.StandardErrorEncoding != null) ? startInfo.StandardErrorEncoding : Console.OutputEncoding;
            standardError = new StreamReader(new FileStream(standardErrorReadPipeHandle, FileAccess.Read, 4096, false), enc, true, 4096);

}

我已经通过运行cmd.exe type path\to\file.html命令对此进行了测试,但它打印了减少的输出,因此我编写了一行ruby脚本来打开文件并将其打印到stdout。它失败了4373字节文件并使用3007字节文件。它只是挂起,死锁。

来自Microsoft IMO的真正可悲的流程处理。