1

I'm using Ubuntu 16.04.

To execute some logic I need to start a process in Java as

String[] commandLine;
String[] environment;
//...
Process p = Runtime.getRuntime().exec(commandLine, environment);
InputStream processInputStream = p.getInputStream(); //<---- ?

But since JVM and the process are different ones I need to understand how they actually communicate. And through what (channels, sockets tcp/udp, pipes, or something else).

How does they actually transfer data?

6
  • 2
    "since JVM and the process are different ones I need to understand how they actually communicate" this is too broad. What are you expecting ? What do you think will be different based on the OS ? Commented May 29, 2017 at 13:35
  • 1
    @AxelH What do u mean too broad? If a process asks some data from another process there need to be some inter-process communication. So which one? Socket? If so, which socket (TCP/UDP)? Commented May 29, 2017 at 13:36
  • 1
    This is indeed an excellent question; I don't believe many people understand the implications of that question yet... I myself took some time understanding it really. But in the meanwhile, have you had a look at ProcessBuilder's .inheritIO() method? Commented May 29, 2017 at 13:38
  • 2
    Might I suggest to edit the question? I do have edit privileges myself but I prefer the OP do it. What about: "what are std{in,out,err} when you create a new process?" Commented May 29, 2017 at 13:47
  • 1
    Are you asking about Process.getInputStream() specifically? That method usually corresponds to the operating system construct that is exposed by the platform's C runtime as the stdout of the child process. On Linux or any POSIX-compliant system it will be a pipe. Commented May 29, 2017 at 14:14

2 Answers 2

2

Judging from the javadoc it seems to be using pipes by default.

Sign up to request clarification or add additional context in comments.

Comments

1

OK, here is a short test, I also have Ubuntu, although it's a 16.10, I think these will behave the same. The program I wrote:

public final class Test
{
    public static void main(final String... args)
        throws IOException
    {
        final ProcessBuilder pb = new ProcessBuilder("yes");
        final Process p = pb.start();

        try (
            final InputStream in = p.getInputStream();
        ) {
            while (true)
                in.read();
        }
    }
}

Using pstree -uplan, I found that that the PID of the yes process was some number n and when I did:

ls -l /proc/n/fd

I got:

lr-x------ 1 fge fge 64 May 29 15:52 0 -> pipe:[1482898]
l-wx------ 1 fge fge 64 May 29 15:52 1 -> pipe:[1482899]
l-wx------ 1 fge fge 64 May 29 15:52 2 -> pipe:[1482900]

which makes me say that the I/O exchange is done using anonymous pipes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.