You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First, I am not sure if this is the right place for this issue. Is there a forum for such a question?
I want to create a new sub process with
Runtime.getRuntime().exec( ...
Then later I want to be able to kill that process and its childs.
The java Process.destroyForcibly() did not work, but JavaSysMon.killProcessTree() does.
So I need the PID of the newly created process.
My current approach is to do it this way:
Create a lock around the start procedure
get list of all child PIDs
create the sub process
get list of all child PIDs
find the PID that did not existed before
Here is my code:
// locking
synchronized(ExternCmd.class){
int[] childsBefore = getChildPids();
Process process = Runtime.getRuntime().exec( cmdLine.toArray( new String[ cmdLine.size() ]), null, workdir );
int[] childsAfter = getChildPids();
int subPid = -1;
for( int i = 0; i < childsAfter.length; i++ ){
if( Arrays.binarySearch( childsBefore, childsAfter[i] ) < 0 ){
Assert.isTrue( subPid < 0, "A PID was already found. The result is ambiguous." );
subPid = childsAfter[i];
Assert.isTrue( subPid > 0 );
}
}
Assert.isTrue( subPid > 0, "The PID was not found" );
} // end of locking, now use the subPid
private int[] getChildPids(){
JavaSysMon sysMon = new com.jezhumble.javasysmon.JavaSysMon();
List<?> childs = sysMon.processTree().find(sysMon.currentPid()).children();
int[] res = new int[childs.size()];
int idx = 0;
for( Object c : childs ){
res[idx] = ((OsProcess)c).processInfo().getPid();
}
Arrays.sort( res );
return res;
}
But this seems to have drawbacks and be fragile. Isn't it?
It would be good if starting synchronized is not needed
If somewhere in the same application another process is started independantly this would result in a ambiguous PID.
My question:
Is there a better way to it?
If not, could JavaSysMon implement support for this use case?
thx
Frank
The text was updated successfully, but these errors were encountered:
Hi
First, I am not sure if this is the right place for this issue. Is there a forum for such a question?
I want to create a new sub process with
Then later I want to be able to kill that process and its childs.
The java Process.destroyForcibly() did not work, but JavaSysMon.killProcessTree() does.
So I need the PID of the newly created process.
My current approach is to do it this way:
Here is my code:
But this seems to have drawbacks and be fragile. Isn't it?
My question:
thx
Frank
The text was updated successfully, but these errors were encountered: