St.Antario
St.Antario

Reputation: 27375

Understanding processes in Java [linux]

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?

Upvotes: 1

Views: 61

Answers (2)

fge
fge

Reputation: 121702

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.

Upvotes: 1

Rik van de Laar
Rik van de Laar

Reputation: 66

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

Upvotes: 2

Related Questions