有人可以解释管道缓冲区死锁吗? [英] Can someone explain pipe buffer deadlock?

查看:207
本文介绍了有人可以解释管道缓冲区死锁吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Popen状态的Python文档:

Python documentation to Popen states:

警告使用通讯()而不是.stdin.write,.stdout.read或.stderr.read,以避免由于其他任何OS管道缓冲区填满并阻塞子进程而导致死锁.

Warning Use communicate() rather than .stdin.write, .stdout.read or .stderr.read to avoid deadlocks due to any of the other OS pipe buffers filling up and blocking the child process.

现在,我正在尝试弄清这种死锁的发生方式以及原因.

Now, I'm trying to figure out how this deadlock can occur and why.

我的思维模型:子进程向stdout/err生成一些东西,将其缓冲,并在缓冲区填充后将其刷新到子过程的stdout/err,再通过管道将其发送到父进程.

My mental model: subproccess produces something to stdout/err, which is buffered and after buffer is filled, it's flushed to stdout/err of subproccess, which is send through pipe to parent process.

根据文档说明,管道具有其自己的缓冲区,并且在填充或子进程终止时,将其刷新到父进程.

From what documentation states, pipe has it's own buffer and when it's filled or subprocess terminates, it's flushed to to the parent process.

无论哪种方式(是否使用管道缓冲区),我都不完全确定如何发生死锁.我唯一能想到的是某种全局" OS管道缓冲进程将在努力争取,这听起来很奇怪.另一个是更多的进程将共享同一个管道,这不应单独发生.

Either way (with pipe buffer or not), I'm not entirely sure how deadlock can occur. Only thing I can think of is some kind of "global" OS pipe buffer processes will be striving for, which sounds strange. Another is that more processes will share same pipe, which should not happen on it's own.

有人可以解释一下吗?

推荐答案

小心,这有一个微妙的错误.

Careful, this has a subtle mistake in it.

我的心理模型:子过程产生 要输出/错误的东西,这是 缓冲并在缓冲区填充后, 它被冲洗到stdout/err 子过程,通过发送 管道到父进程.

My mental model: subproccess produces something to stdout/err, which is buffered and after buffer is filled, it's flushed to stdout/err of subproccess, which is send through pipe to parent process.

该缓冲区由父进程和子进程共享.

The buffer is shared by parent and child process.

子进程产生一些东西到stdout,这与父进程应该从中读取的缓冲区相同.

Subprocess produces something to stdout, which is the same buffer the parent process is supposed to be reading from.

当缓冲区已满时,写入将停止,直到清空缓冲区为止.刷新对管道没有任何意义,因为两个进程共享同一个缓冲区.

When the buffer is filled, writing stops until the buffer is emptied. Flush doesn't mean anything to a pipe, since two processes share the same buffer.

刷新到磁盘意味着设备驱动程序必须将字节向下推送到设备.刷新套接字意味着告诉TCP/IP停止等待累积缓冲区并发送内容.刷新到控制台意味着停止等待换行符,并将字节通过设备驱动程序推送到设备.

Flushing to disk means that the device driver must push the bytes down to the device. Flushing a socket means to tell TCP/IP to stop waiting to accumulate a buffer and send stuff. Flushing to a console means stop waiting for a newline and push the bytes through the device driver to the device.

这篇关于有人可以解释管道缓冲区死锁吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆