0

Original question : I successfully found the pid of a process using the following code. I'm looking to get the id of the child process, and here I believed I would achieve it by finding the id of the InputStreamReader. I'm not going about this the right way, however, as I'm unable to pinpoint which part of my code would correctly return an id of the child process.

Update and more info : My goal is keep track of the parent process id, announce the creation of a child process with it's ID and the parent's ID. Finally, announce the termination of the child process.

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;


class processes {
public static void main(String[] args) {
    try {
        Process p = Runtime.getRuntime().exec("/bin/ls");
        final InputStream is = p.getInputStream();
        final InputStream was = p.getInputStream();
        Thread t = new Thread(new Runnable() {
            public void run() {
                InputStreamReader isr = new InputStreamReader(is);
                InputStreamReader wasr = new InputStreamReader(was);
                int isid = 0;
                int ch;
                if(isr.getClass().getName().equals("java.lang.UNIXProcess")) {
                /* get the PID on unix/linux systems */
                try {
                    Field i = isr.getClass().getDeclaredField("pid");
                    i.setAccessible(true);
                    isid = i.getInt(isr);
                    System.out.println("is process");
                    }
                catch (Throwable e) {
                    }
                }
                try {
                    System.out.println(isid);

                    while ((ch = isr.read()) != -1) {
                        System.out.print((char) ch);
                    while ((ch = wasr.read()) != -1) {
                        System.out.print((char) ch);

                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();

        p.waitFor();
        t.join();
        int pid = 0;
        //final int isid = 0;
        //int isid = 0;
        if(p.getClass().getName().equals("java.lang.UNIXProcess")) {
            /* get the PID on unix/linux systems */
            try {
                Field f = p.getClass().getDeclaredField("pid");
                f.setAccessible(true);
                //i.setAccessible(true);
                pid = f.getInt(p);
                //isid = i.getInt(is);
                }
            catch (Throwable e) {
                }
            }
        System.out.println("Process p " + pid + " terminates.");
        //System.out.println(pid);
    } catch (Exception e) {
        e.printStackTrace();
        }
    }
}
user25976
  • 1,005
  • 4
  • 18
  • 39
  • An InputStreamReader is a Java object, not a UNIX process. What are you trying to retrieve here? – RealSkeptic Oct 23 '14 at 22:20
  • @RealSkeptic I'm trying to find the id of the child process. I thought this would be the id of the InputStreamReader, but now that you tell me it's a java object, i'm rethinking my question. – user25976 Oct 23 '14 at 22:21
  • `InputStreamReaders` don't have IDs. Your question doesn't make sense. – user207421 Oct 23 '14 at 22:50
  • 1
    You can find the answer here: http://stackoverflow.com/questions/4750470/how-to-get-pid-of-process-ive-just-started-within-java-program – David Roussel Oct 25 '14 at 20:47

1 Answers1

0

I'm looking to get the id of the child process, and here I believed I would achieve it by finding the id of the InputStreamReader.

It won't work.

  • An InputStreamReader doesn't have an ID.
  • Neither does the InputStream that it wraps.
  • Neither does the FileDescriptor that is inside the InputStream.

And your code won't work because neither the reader or the stream will ever be an instance of UNIXProcess.

If you need the child processes pid, you have to keep a reference to the Process object.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • You could probably do it with native linux api. You could probably check what's the pid of the process owning the file descriptor. – Jazzwave06 Oct 23 '14 at 22:48
  • @Stephen C How would I keep a reference to the Process object? – user25976 Oct 23 '14 at 23:08
  • Process p = ...; // this is a reference to your process – JimN Oct 24 '14 at 01:35
  • @JimN I see what you mean, but how can I reference the thread ID with the process ID? I just updated my question a lot. – user25976 Oct 24 '14 at 01:40
  • @StephenC I'd like to accept your answer, but I'd like to know more about "if you need the child processes pid, you have to keep a reference to the Process object." – user25976 Oct 25 '14 at 19:31
  • @user25976 - JimN has already answered that. It is `p` in your code, though I have no idea why you are running `ls` twice – Stephen C Oct 25 '14 at 22:24
  • @StephenC That's a mistake--I was actually trying to just read it twice. I've now removed process q, and the second time the ls is read is with InputStreamReader wasr – user25976 Oct 25 '14 at 22:47