如何处理来自外部进程的不需要的流? [英] What to do with unneeded streams from an external process?

查看:165
本文介绍了如何处理来自外部进程的不需要的流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在单独的进程中执行命令时,例如使用 Runtime.getRuntime()。exec(...)方法,其JavaDoc声明:

When I execute a command in a separate process, for example by using the Runtime.getRuntime().exec(...) method, whose JavaDoc states:

Executes the specified command and arguments in a separate process.

我需要对此流程中的流做什么,因为知道流程应该存在直到Java程序存在吗? (这是一个细节,但Java程序负责杀死这个过程,并且该过程本身具有内置的安全性,如果它注意到生成他的Java程序不再运行的话,它会自动杀死它。)

What do I need to do with the streams from this process, knowing that the process shall live until the Java program exists? (this is a detail but the Java program takes care of killing this process and the process itself has a safety built-in where it kills itself should it notice that the Java program who spawned him his not running anymore).

如果我们认为这个过程根本不产生输出(例如因为所有错误消息和stdout被重定向到/ dev / null并且所有通信都是使用文件/套接字/其他来完成的),我需要对输入流做什么?

If we consider that this process produces no output at all (for example because all error messages and stdout are redirected to /dev/null and all communications are done using files/sockets/whatever), what do I need to do with the input stream?

我应该有一个(或两个?)Java线程什么都不运行,试图读取stdout / stderr?

Should I have one (or two?) Java threads running for nothing, trying to read stdout/stderr?

处理从不产生stdout / stderr的Java程序产生的长期外部进程的正确方法是什么?

What is the correct way to deal with a long-living external process spawned from a Java program that produces no stdout/stderr at all?

编辑

基本上我将shell脚本包装在另一个shell脚本中,确保将所有内容重定向到 /开发/空。我很确定如果我的outtershell脚本(将所有内容重定向到/ dev / null)仍会在 stdout stderr上生成任何内容,那么我的Un * x将不合规。然而,我觉得令人难以置信的是,我会以某种方式在应用程序的生命周期中无所事事地运行线程。真的令人难以理解。

Basically I wrap the shell script in another shell script that makes sure to redirect everything to /dev/null. I'm pretty sure my Un*x would be non-compliant if my "outter" shell script (the one redirecting everything to /dev/null) would still generate anything on stdout or stderr. Yet I find it mindboggling that I would somehow be supposed to have threads running during the lifecycle of the app "for nothing". Really boggles the mind.

推荐答案

我相信如果你对这些进程的输入和输出不感兴趣,那么处理进程输入和输出的正确方法是要及时关闭它们。如果子进程随后分别尝试在stdin或stdout上调用read或write,则会抛出IOException。子进程有责任处理它无法读取或写入的事实。

I believe the correct way to deal with a process's input and output if you are not interested in them is to close them promptly. If the child process subsequently tried to call read or write on stdin or stdout respectively, then an IOException would be thrown. It would be the responsibility of the child process to deal with the fact that it cannot read or write.

大多数进程都会忽略它们无法写入和静默丢弃的事实写道。在Java中也是如此,其中System.out是PrintWriter,因此将忽略stdout抛出的任何IOExceptions。这几乎是将输出重定向到/ dev / null时所发生的情况 - 所有输出都被静默丢弃。

Most processes will ignore the fact that they cannot write and silently discard and writes. This is true in Java, where System.out is a PrintWriter, so any IOExceptions thrown by stdout are ignored. This is pretty much what happens when you redirect output to /dev/null -- all output is silently discarded.

听起来你已经阅读了有关进程的API和为什么读取/写入过程非常重要,如果它希望进行任何写入或读取过程。但我要重申一下,问题是有些操作系统只为(特别是)stdout分配了非常有限的缓冲区,所以不允许这个缓冲区填满是很重要的。这意味着要么及时读取子进程的任何输出,要么通知操作系统您不需要输出进程,并且它可以释放任何保留的资源,并拒绝任何进一步尝试写入stdout或从stdin读取(而不是而不仅仅是在资源可用之前悬挂。)

It sounds like you've read the API on Processes and why it's important to read/write to the process if it expects to be doing any writing or reading of its own. But I'll reiterate, the problem comes that some OSes only allocated very limited buffers for (specifically) stdout, so it is important to either not allow this buffers to fill up. This means either reading any output of the child process promptly, or notifying the OS that you do not require the output of the process and that it can release any resources held, and reject any further attempted to write to stdout or read from stdin (rather than just hanging until resources become available).

这篇关于如何处理来自外部进程的不需要的流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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