Hola a todos,estoy tratando de obtener el PID que le asiga Fedora a un hilo que he creado en java.
He probado con esto:
Código:
public String getPID()
{
String cmd = new String("echo $$") ;
String PID = null;
try
{
Process myProcess = Runtime.getRuntime().exec(cmd);
BufferedReader stdout = new BufferedReader(new InputStreamReader(myProcess.getInputStream()));
String line = stdout.readLine();
while ( line != null )
{
System.out.println("Line: "+ line );
PID = line;
line = stdout.readLine();
}
int exitVal = myProcess.waitFor();
System.out.println( "exit value: " + exitVal );
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
return PID;
}
y con este otro:
Código:
public String getPID()
{
Vector<String> commands=new Vector<String>();
commands.add("/bin/bash");
commands.add("-c");
commands.add("echo $PID");
ProcessBuilder pb=new ProcessBuilder(commands);
Process pr=pb.start();
pr.waitFor();
if (pr.exitValue()==0)
{
BufferedReader outReader=new BufferedReader(new InputStreamReader(pr.getInputStream()));
return outReader.readLine().trim();
}
else
{
System.out.println("Error while getting PID");
return "";
}
}
Pero no hay manera ... La idea es obtener este pid cuando el hilo principal haga el start por tanto en el metodo run del hilo lo llamo:
Código:
public void run()
{
pid = getPID();
....
}