PIRL.Utilities

## Class UNIX_Process

- java.lang.Object
- - java.lang.Process
  - - PIRL.Utilities.UNIX_Process

    public class UNIX_Process
    extends Process

A UNIX_Process is an adapter for the UNIXProcess implementation of the abstract Process class that provides access to hidden information.

The API for the Java Process class, as of Java 1.6, has an unfortunate shortcoming: The user is unable to access the process identification (PID) of the executed process nor provide the timeout argument to the Object.wait method used by the waitFor method. Process is an abstract class that is implemented by a host OS dependent class - UNIXProcess for UNIX systems - which relies on JNI methods that make the necessary host process management system calls.

For the UNIXProcess class (as of Java 1.6) the PID value is held in the private "pid" variable. UNIX_Process provides an `ID()` method to get the PID value.

The ability to provide the timeout argument is a trivial modification of the waitFor method. UNIX_Process provides a `waitFor(long)` method that limits the wait for the process to exit to a timeout value.

An `exited()` method provides direct access to the running/exited flag for the process.

All Process API methods are simply passed to the underlying Process.

See Also:
  `Process`

### Field Summary

Fields
`static String` `ID` Class identification name with source code version and date.

### Constructor Summary

Constructors
Constructor and Description
`UNIX_Process(Process process)` Construct a UNIX_Process for a Process object on a UNIX system.

### Method Summary

Methods
`void` `destroy()`
`boolean` `exited()` Test if the process has exited.
`int` `exitValue()`
`InputStream` `getErrorStream()`
`InputStream` `getInputStream()`
`OutputStream` `getOutputStream()`
`int` `ID()` Get the Process ID that uniquely identifies the system process.
`int` `waitFor()`
`int` `waitFor(long timeout)` Wait for the process to exit up to a maximum timeout.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### ID

    public static final String ID

Class identification name with source code version and date.

See Also:
  Constant Field Values

### Constructor Detail

#### UNIX_Process

    public UNIX_Process(Process process)
                 throws NoSuchFieldException,
                        SecurityException

Construct a UNIX_Process for a Process object on a UNIX system.

Parameters:
  `process` - The Process object obtained from a ProcessBuilder's start method or a Runtime exec method.
Throws:
  `NoSuchFieldException` - If the process does not have one of the expected field variables. This will occur if the process is not implemented by a JFC UNIXProcess or the implementation has changed from what is expected.
  `SecurityException` - If a JVM SecurityManager prevents access to the private field values of the process object.
See Also:
  `ProcessBuilder`, `Runtime`

### Method Detail

#### getOutputStream

    public OutputStream getOutputStream()

Specified by:
  `getOutputStream` in class `Process`

#### getInputStream

    public InputStream getInputStream()

Specified by:
  `getInputStream` in class `Process`

#### getErrorStream

    public InputStream getErrorStream()

Specified by:
  `getErrorStream` in class `Process`

#### waitFor

    public int waitFor()
                throws InterruptedException

Specified by:
  `waitFor` in class `Process`
Throws:
  `InterruptedException`

#### exitValue

    public int exitValue()

Specified by:
  `exitValue` in class `Process`

#### destroy

    public void destroy()

Specified by:
  `destroy` in class `Process`

#### ID

    public int ID()

Get the Process ID that uniquely identifies the system process.

Returns:
  the integer ID used by the system to identify the process.

#### waitFor

    public int waitFor(long timeout)
                throws InterruptedException,
                       IOException

Wait for the process to exit up to a maximum timeout.

Causes the current thread to wait, if necessary, until the process represented by this `Process` object has terminated or the timeout period has expired. This method returns immediately if the subprocess has already terminated. If the subprocess has not yet terminated, the calling thread will be blocked. If the subprocess exits while the thread is blocked the exit status of the subprocess will be returned. If the timeout period expires while the thread is blocked an IOException will be thrown.

Parameters:
  `timeout` - The maximum amount of time, in milliseconds, to wait on subprocess exit. Wait forever if the value is <= 0 (the same as the `waitFor()` method}.
Returns:
  The exit value of the process.
Throws:
  `InterruptedException` - If the current thread is `interrupted` by another thread while it is waiting, then the wait is ended and an InterruptedException is thrown.
  `IOException` - If the timeout occurred before the process exit occurred.

#### exited

    public boolean exited()

Test if the process has exited.

The running/exited flag will only be set true after the forked process has exited.

This method tests the value of the UNIXProcess flag. The Process API may be used to determine if the process has exited via the `exitValue()` method which will throw an IllegalThreadStateException if the process has not yet exited.

Returns:
  true if the process has exited; false if it is still running.