Skip to content

Commit

Permalink
Created an API for visiting the process tree
Browse files Browse the repository at this point in the history
  • Loading branch information
Jez Humble committed Jan 8, 2010
1 parent 98bddfb commit 1a7c42d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ If you’re interested in adding support for a new platform, check out the proje
Download latest
---------------

The current version of JavaSysMon is 0.2.0, released December 28th. You can get it here: http://github.com/jezhumble/javasysmon/downloads
The current version of JavaSysMon is 0.3.0, released January 8th. You can get it here: http://github.com/jezhumble/javasysmon/downloads

Run it with java -jar

Expand Down Expand Up @@ -57,5 +57,6 @@ Links
-----

* Code: http://github.com/jezhumble/javasysmon
* JavaDoc: http://jezhumble.github.com/javasysmon/
* Bugs/Features: http://github.com/arya/javasysmon/issues
* Mailing List: http://groups.google.com/group/javasysmon
30 changes: 28 additions & 2 deletions src/main/java/com/jezhumble/javasysmon/JavaSysMon.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,34 @@ public void killProcess(int pid) {
monitor.killProcess(pid);
}

public void killProcessTree(int pid, boolean descendantsOnly) {
processTree().find(pid).killTree(descendantsOnly);
/**
* Allows you to visit the process tree, starting at the node identified by pid.
* The process tree is traversed depth-first.
*
* @param pid The identifier of the node to start visiting
* @param processVisitor The visitor
*/
public void visitProcessTree(final int pid, final ProcessVisitor processVisitor) {
processTree().find(pid).accept(processVisitor, 0);
}

/**
* Kills the process tree starting at the process identified by pid. The
* process tree is killed from the bottom up to ensure that orphans are
* not generated.
* <p>
* This method uses {@link #visitProcessTree}.
*
* @param pid The identifier of the process at which to start killing the tree.
* @param descendantsOnly Whether or not to kill the process you start at,
* or only its descendants
*/
public void killProcessTree(final int pid, final boolean descendantsOnly) {
visitProcessTree(pid, new ProcessVisitor() {
public boolean visit(OsProcess process, int level) {
return !descendantsOnly || (pid != process.processInfo().getPid());
}
});
}

/**
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/com/jezhumble/javasysmon/OsProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,18 @@ public OsProcess find(int pid) {
return null;
}

public void killTree(boolean descendantsOnly) {
/**
* Method to allow visiting the process tree. Use the convenience method
* {@link JavaSysMon#visitProcessTree}
*
* @param processVisitor An instance of {@link ProcessVisitor}
* @param level The level currently being visited
*/
public void accept(ProcessVisitor processVisitor, int level) {
for (Iterator it = children.iterator(); it.hasNext(); ) {
((OsProcess) it.next()).killTree(false);
((OsProcess) it.next()).accept(processVisitor, level + 1);
}
if (!descendantsOnly) {
if (processVisitor.visit(this, level)) {
new JavaSysMon().killProcess(processInfo.getPid());
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/jezhumble/javasysmon/ProcessVisitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.jezhumble.javasysmon;

/**
* Allows you to visit the process tree using {@link JavaSysMon#visitProcessTree}
*/
public interface ProcessVisitor {
/**
* Called on every node. The process tree is traversed depth-first
* @param process The current process being visited
* @param level How many levels beneath the initial node visited you are (0-indexed)
* @return Whether or not to kill the process being visited
*/
boolean visit(OsProcess process, int level);
}

0 comments on commit 1a7c42d

Please sign in to comment.