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

Print this page




 202     }
 203 
 204     public InputStream getErrorStream() {
 205         return stderr;
 206     }
 207 
 208     public synchronized int waitFor() throws InterruptedException {
 209         while (!hasExited) {
 210             wait();
 211         }
 212         return exitcode;
 213     }
 214 
 215     public synchronized int exitValue() {
 216         if (!hasExited) {
 217             throw new IllegalThreadStateException("process hasn't exited");
 218         }
 219         return exitcode;
 220     }
 221 
 222     private static native void destroyProcess(int pid);
 223     public void destroy() {
 224         // There is a risk that pid will be recycled, causing us to
 225         // kill the wrong process!  So we only terminate processes
 226         // that appear to still be running.  Even with this check,
 227         // there is an unavoidable race condition here, but the window
 228         // is very small, and OSes try hard to not recycle pids too
 229         // soon, so this is quite safe.
 230         synchronized (this) {
 231             if (!hasExited)
 232                 destroyProcess(pid);
 233         }
 234         try { stdin.close();  } catch (IOException ignored) {}
 235         try { stdout.close(); } catch (IOException ignored) {}
 236         try { stderr.close(); } catch (IOException ignored) {}
 237     }
 238 
















 239     /* This routine initializes JNI field offsets for the class */
 240     private static native void initIDs();
 241 
 242     static {
 243         initIDs();
 244     }
 245 
 246     /**
 247      * A buffered input stream for a subprocess pipe file descriptor
 248      * that allows the underlying file descriptor to be reclaimed when
 249      * the process exits, via the processExited hook.
 250      *
 251      * This is tricky because we do not want the user-level InputStream to be
 252      * closed until the user invokes close(), and we need to continue to be
 253      * able to read any buffered data lingering in the OS pipe buffer.
 254      */
 255     static class ProcessPipeInputStream extends BufferedInputStream {
 256         ProcessPipeInputStream(int fd) {
 257             super(new FileInputStream(newFileDescriptor(fd)));
 258         }




 202     }
 203 
 204     public InputStream getErrorStream() {
 205         return stderr;
 206     }
 207 
 208     public synchronized int waitFor() throws InterruptedException {
 209         while (!hasExited) {
 210             wait();
 211         }
 212         return exitcode;
 213     }
 214 
 215     public synchronized int exitValue() {
 216         if (!hasExited) {
 217             throw new IllegalThreadStateException("process hasn't exited");
 218         }
 219         return exitcode;
 220     }
 221 
 222     private static native void destroyProcess(int pid, boolean force);
 223     public void destroy(boolean force) {
 224         // There is a risk that pid will be recycled, causing us to
 225         // kill the wrong process!  So we only terminate processes
 226         // that appear to still be running.  Even with this check,
 227         // there is an unavoidable race condition here, but the window
 228         // is very small, and OSes try hard to not recycle pids too
 229         // soon, so this is quite safe.
 230         synchronized (this) {
 231             if (!hasExited)
 232                 destroyProcess(pid, force);
 233         }
 234         try { stdin.close();  } catch (IOException ignored) {}
 235         try { stdout.close(); } catch (IOException ignored) {}
 236         try { stderr.close(); } catch (IOException ignored) {}
 237     }
 238 
 239     public void destroy() {
 240         destroy(false);
 241     }
 242 
 243     public Process destroyForcibly() {
 244         destroy(true);
 245         return this;
 246     }
 247 
 248     @Override
 249     public boolean isAlive() {
 250         synchronized (this) {
 251             return !hasExited;
 252         }
 253     }
 254 
 255     /* This routine initializes JNI field offsets for the class */
 256     private static native void initIDs();
 257 
 258     static {
 259         initIDs();
 260     }
 261 
 262     /**
 263      * A buffered input stream for a subprocess pipe file descriptor
 264      * that allows the underlying file descriptor to be reclaimed when
 265      * the process exits, via the processExited hook.
 266      *
 267      * This is tricky because we do not want the user-level InputStream to be
 268      * closed until the user invokes close(), and we need to continue to be
 269      * able to read any buffered data lingering in the OS pipe buffer.
 270      */
 271     static class ProcessPipeInputStream extends BufferedInputStream {
 272         ProcessPipeInputStream(int fd) {
 273             super(new FileInputStream(newFileDescriptor(fd)));
 274         }