0

I have come across a situation wherein I need to find out the Unix process pid from the object description.

For ex:- I have a script that is run in unix from java code that creates a new process.

When this is printed from the java code, I get something like

java.lang.UNIXProcess@87cbde

It is quite evident from the above that the object is refering to a UNIXProcess.

Is it possible to find the PID of this process in UNIX with the help of this object description?

Galaxin
  • 474
  • 2
  • 9
  • 21
  • 1
    possible duplicate of [How do I find the process ID (pid) of a process started in java?](http://stackoverflow.com/questions/5284139/how-do-i-find-the-process-id-pid-of-a-process-started-in-java) – assylias Jan 23 '13 at 14:19
  • 1
    The "object description" here is just the class name and the hashCode of the object. The PID is not printed out. – Peter Lawrey Jan 23 '13 at 14:19
  • See also: http://stackoverflow.com/questions/4750470/know-pid-java-process – assylias Jan 23 '13 at 14:20
  • Getting the PID can be a little cumbersome, but you can [refer to this question](http://stackoverflow.com/q/35842/1979005) for more information. – ameed Jan 23 '13 at 14:30
  • @MathSquared11235 That explains how to get the pid of the JVM that creates the process, not of the process itself. – assylias Jan 23 '13 at 16:24

1 Answers1

1

Is it possible to find the PID of this process in UNIX with the help of this object description?

The simple answer is No. There is no way.

Firstly, that "descriptor" is simply the output of the default implementation of toString(). It consists of the object's class name and its "identity hash code". It does not encode the state of the object.

The identity hashcode is a value that is typically calculated based on the address of the object the first time the method was called. However:

  • it is a 32bit (max) value and that can't encode a full 64bit address on a 64 bit JVM,

  • the object may no longer be at the same address it was when the identity hashcode was computed, and

  • you can't turn a machine address into a Java reference (or vice versa) in pure Java. (And even use of non-pure Java tricks is liable to give you JVM stability problems if you get it wrong.)

However, if you have an object reference for the UnixProcess object, it should be possible to use reflection to poke around in its private fields and dig out the UNIX pid value.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216