【发布时间】:2012-02-21 15:43:59
【问题描述】:
当使用Runtime.exec() 在 java 中创建子进程时,我知道我必须填充输入/排出输出流以防止子进程阻塞。
有趣的是,Process 的 javadoc 声明了更多内容:
...failure to promptly write the input stream or read the output stream of
the subprocess may cause the subprocess to block, and even deadlock.
我想知道在这种情况下子进程可能也死锁!
问题:
1、什么情况下会死锁?
2. 为什么会死锁?
3. 能否提供一个简短的示例程序来显示这种死锁?
4. 这个死锁是操作系统的bug吗?
【问题讨论】:
-
+1... (不是您的问题的答案,因此是评论)... 请注意,很久以前(当我阅读 "When Runtime .exec 不会” IIRC),我决定通过立即分叉/退出 Runtime.exec 的 shell 脚本并将第二个 shell 脚本的任何输出重定向到临时文件(然后解析这些来自 Java 的临时文件)。这很残酷,但很有效(我在 Linux 和 OS X 上都做过......不知道 Windows):)
标签: java process deadlock block