./src/solaris/classes/java/lang/UNIXProcess.java.linux

Print this page
rev 5754 : [mq]: getPid-patch

*** 32,41 **** --- 32,46 ---- import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; + import java.nio.charset.Charset; + import java.nio.file.Files; + import java.nio.file.LinkOption; + import java.nio.file.Path; + import java.nio.file.Paths; import java.util.Arrays; import java.util.concurrent.Executors; import java.util.concurrent.Executor; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit;
*** 268,277 **** --- 273,309 ---- @Override public synchronized boolean isAlive() { return !hasExited; } + @Override + public int getPid() { + if (!isAlive()) { + return -1; + } else { + return pid; + } + } + + @Override + public String getProcessName(int pid) { + // look at /proc/<pid>/comm + if (pid < 0) { + throw new IllegalArgumentException( "pid == " + pid ); + } + Path p = Paths.get( "/proc/" + pid + "/comm" ); + if (!Files.exists(p, LinkOption.NOFOLLOW_LINKS )) { + return null; + } + + try { + return Files.readAllLines( p, Charset.defaultCharset() ).get(0); + } catch (IOException ioex) { + throw new RuntimeException( ioex ); + } + } + /* This routine initializes JNI field offsets for the class */ private static native void initIDs(); static { initIDs();