English 中文(简体)
为什么采用重新定向投入/产出流的互动过程,导致申请被停止?
原标题:Why executing interactive process with redirected input/output streams causes application being stopped?

I have a console Java program that executes sh -i in a separate process and copies the data between the processes input/output stream and corresponding System streams:

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

class StreamCopier implements Runnable {
    private InputStream in;
    private OutputStream out;

    public StreamCopier(InputStream in, OutputStream out) {
        this.in = in;
        this.out = out;
    }

    public void run() {
        try {
            int n = 0;
            byte[] buffer = new byte[4096];
            while (-1 != (n = in.read(buffer))) {
                out.write(buffer, 0, n);
                out.flush();
            }
        } catch (IOException e) {
            System.out.println(e);
        }
    }
}

public class Test {
    public static void main(String[] args)
            throws IOException, InterruptedException {
        Process process = Runtime.getRuntime().exec("sh -i");
        new Thread(new StreamCopier(
                process.getInputStream(), System.out)).start();
        new Thread(new StreamCopier(
                process.getErrorStream(), System.err)).start();
        new Thread(new StreamCopier(
                System.in, process.getOutputStream())).start();
        process.waitFor();
    }
}

将其置于欧罗因之下,结果如下:

$ 
[1]+  Stopped                 java -cp . Test

是否有任何人澄清为何申请被停止,以及如何避免申请?

这与我的

最佳回答

您可以援引诸如<代码>sh -i +m等壳,从而放弃工作控制。 这意味着<代码>fg和bg的指令不会奏效,Ctrl+ Z将中止 your的申请、炮弹和从中开始的所有方案。

如果你仍然想要控制工作,那么你就应该利用一个假胜地与车道进行沟通,这为车壳的使用创造了新的脂肪,但我想 Java不支持这一点。

问题回答

SIGTTIN或SIGTTOU正在阻止你。 这些信号在试图使国际会计组织接触到TTY时被送到一个背景过程。 在这种情况下,“后向”是指“而不是终端控制过程组”。 我怀疑,你 re子正在制造新的石块,并吞.你。 然后,母体方案(java)的确是IO(在你看来可能是从TTY读物),并且获得SIGTTIN。

证实这一理论的简单办法是用一个简单的(而不是壳体)取代sh,而后者不会试图推倒。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签