getting the Process ID in UNIX

F

Ferman

Hi,

I am developing a java program on the UNIX environment. I want to get the id
of my process. What should I do ? or what class method do I use ?

Can you help me ?
 
G

Gordon Beaton

I am developing a java program on the UNIX environment. I want to
get the id of my process. What should I do ? or what class method do
I use ?

Can you help me ?

There is no pure Java way. You have a number of platform dependent
alternatives however:

- call getpid() from a native method

- tell the application what its pid will be, by passing $$ either as a
property or an argument from a shell script:

#!/bin/sh
exec java -Dpid=$$ MyApp

- exec a child process that can tell you its parent pid:

String[] cmd = { "/bin/bash", "-c", "echo $PPID" };
Process p = Runtime.getRuntime().exec(cmd);

then read the pid from p's InputStream.

- on some platforms you can use a FileInputStream to read from the
process file system. For example on linux you can read the pid from
/proc/self/stat.

/gordon
 
F

Ferman

How do I call the getpid() function from a native method ? Is there so a
function ?

Gordon Beaton said:
I am developing a java program on the UNIX environment. I want to
get the id of my process. What should I do ? or what class method do
I use ?

Can you help me ?

There is no pure Java way. You have a number of platform dependent
alternatives however:

- call getpid() from a native method

- tell the application what its pid will be, by passing $$ either as a
property or an argument from a shell script:

#!/bin/sh
exec java -Dpid=$$ MyApp

- exec a child process that can tell you its parent pid:

String[] cmd = { "/bin/bash", "-c", "echo $PPID" };
Process p = Runtime.getRuntime().exec(cmd);

then read the pid from p's InputStream.

- on some platforms you can use a FileInputStream to read from the
process file system. For example on linux you can read the pid from
/proc/self/stat.

/gordon
 
G

Gordon Beaton

How do I call the getpid() function from a native method ? Is there
so a function ?

Are you asking how to write a native method, or how to call getpid()
from it?


Yes there is such a function. You did say UNIX?

http://www.opengroup.org/onlinepubs/007904975/functions/getpid.html


You call it from the native method like this:

pid_t pid = getpid();


To learn how to write a native method, read about JNI. For example
here:

http://java.sun.com/j2se/1.4.2/docs/guide/jni/index.html


If you don't know about JNI, the other mechanisms I suggested are
easier.

/gordon
 
R

Roedy Green

I am developing a java program on the UNIX environment. I want to get the id
of my process. What should I do ? or what class method do I use ?

That is a Unix specific thing, so there won't likely be a Java method.
It would have to be faked on every non-Unix platform.

This is time to brush up on JNI. See
http://mindprod.com/jgloss/jni.html
 
F

Ferman

Hi,


I found a solution. I run a command by exec() on UNIX OS.

program part like this;
try
{
String vCommand({"echo", "$$"});
Process p = Runtime.getRuntime().exec(vCommand);
BufferedReader br =
new BufferedReader(new InputStreamReader(p.getInputStream()));
int pid = Integer.parseInt(br.readLine());
System.out.println(pid);
p.destroy();
p = null;
br = null;
}
catch (java.io.IOException e)
{
System.out.println(e);
}

But it is give me "$$" characters. I didn't obtain the process id. What is
the problem ?
OS: SunOS
 
G

Gordon Beaton

I found a solution. I run a command by exec() on UNIX OS.

No, you didn't.
String vCommand({"echo", "$$"});
Process p = Runtime.getRuntime().exec(vCommand); [...]
But it is give me "$$" characters. I didn't obtain the process id.
What is the problem ?

$$ only has special meaning to the shell. Runtime.exec() does not run
your program in a shell. And even if it did, that would only tell you
the pid of the shell, not the pid of the JVM you call it from.

Did you read my original reply? I provided 4 examples. One of them
uses Runtime.exec() to run a shell that can tell you the pid of its
*parent*.

/gordon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top