When a process launches another process, the launched process becomes the subprocess of the launching process. The launched process, in turn, can launch another process and this chain can continue. This results in a process tree. Often, we would have to deal with a buggy subprocess and might want to kill that subprocess, or we might want to know the subprocesses that are launched and might want to get some information about it.
In Java 9, two new APIs in the Process
class have been added: children()
and descendants()
. The children()
API allows you to get a list of the snapshot of processes that are the immediate children of the current process, and the descendants()
API provides a snapshot of processes that are recursively children()
of the current process, that is, they are invoking children()
recursively on each child process.
In this recipe, we will look at both the children()
and descendants()
APIs and see what information we can gather from the snapshot of the process...