From 90dc5524635ba0775d5f43229cea90202924c2f2 Mon Sep 17 00:00:00 2001 From: Joshua Warner Date: Thu, 3 Nov 2011 12:30:51 -0600 Subject: [PATCH] implement Process.destroy --- classpath/java-lang.cpp | 10 ++++++++++ classpath/java/lang/Runtime.java | 4 +++- test/Processes.java | 31 +++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 test/Processes.java diff --git a/classpath/java-lang.cpp b/classpath/java-lang.cpp index 4f301572e9..0c8cead9c3 100644 --- a/classpath/java-lang.cpp +++ b/classpath/java-lang.cpp @@ -279,6 +279,11 @@ Java_java_lang_Runtime_waitFor(JNIEnv* e, jclass, jlong pid, jlong tid) return exitCode; } +extern "C" JNIEXPORT void JNICALL +Java_java_lang_Runtime_kill(JNIEnv*, jclass, jlong pid) { + TerminateProcess(reinterpret_cast(pid), 1); +} + Locale getLocale() { const char* lang = ""; const char* reg = ""; @@ -466,6 +471,11 @@ Java_java_lang_Runtime_waitFor(JNIEnv*, jclass, jlong pid, jlong) return exitCode; } +extern "C" JNIEXPORT void JNICALL +Java_java_lang_Runtime_kill(JNIEnv*, jclass, jlong pid) { + kill((pid_t)pid, SIGTERM); +} + Locale getLocale() { Locale fallback; diff --git a/classpath/java/lang/Runtime.java b/classpath/java/lang/Runtime.java index a24f850bf3..bf80507deb 100644 --- a/classpath/java/lang/Runtime.java +++ b/classpath/java/lang/Runtime.java @@ -122,6 +122,8 @@ public class Runtime { private static native void load(String name, boolean mapName); + private static native void kill(long pid); + public native void gc(); public native void exit(int code); @@ -147,7 +149,7 @@ public class Runtime { } public void destroy() { - throw new RuntimeException("not implemented"); + kill(pid); } public InputStream getInputStream() { diff --git a/test/Processes.java b/test/Processes.java new file mode 100644 index 0000000000..372513f0c1 --- /dev/null +++ b/test/Processes.java @@ -0,0 +1,31 @@ +import java.io.IOException; + +public class Processes { + public static void main(String[] args) { + long start = System.currentTimeMillis(); + try { + final Process p = Runtime.getRuntime().exec("sleep 10"); + new Thread() { + public void run() { + try { + Thread.sleep(100); + } catch(InterruptedException e) { + // ignore + } + p.destroy(); + } + }.start(); + try { + p.waitFor(); + } catch(InterruptedException e) { + // ignore + } + long stop = System.currentTimeMillis(); + if(stop - start > 5000) { + throw new RuntimeException("test failed; we didn't kill the process..."); + } + } catch(IOException e) { + throw new RuntimeException(e); + } + } +}