[英]Understanding processes in Java [linux]
我正在使用Ubuntu 16.04
。
要執行某些邏輯,我需要在Java中啟動一個進程
String[] commandLine;
String[] environment;
//...
Process p = Runtime.getRuntime().exec(commandLine, environment);
InputStream processInputStream = p.getInputStream(); //<---- ?
但由於JVM
和流程不同,我需要了解它們實際上是如何通信的。 並通過什么(通道,套接字tcp / udp,管道或其他東西)。
他們如何實際傳輸數據?
從javadoc來看,它似乎默認使用管道。
好的,這是一個簡短的測試,我也有Ubuntu,雖然它是16.10,但我認為它們的行為是一樣的。 我寫的程序:
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();
}
}
}
使用pstree -uplan
,我發現yes
進程的PID是某個數字n,當我這樣做時:
ls -l /proc/n/fd
我有:
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]
這讓我說I / O交換是使用匿名管道完成的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.