-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Child processes #78
Comments
Hello! Thanks for the kind words about procfs! Getting a list of child processes for one process is pretty simple. Here's how you can get all the direct children of PID1: for child in all_processes()
.unwrap()
.into_iter()
.filter(|p| p.stat.ppid == 1)
{
println!("{} {}", child.pid, child.stat.comm);
} But this will only get you the direct children. If you want a list of all children, you need a slightly different approach. One way to do this is to keep a HashMap that maps a PID to all its children PIDs: let mut map = HashMap::<i32, Vec<i32>>::new();
for proc in all_processes().unwrap() {
map.entry(proc.stat.ppid).or_default().push(proc.pid);
} Then if you wanted a list of all children of fn get_children_recursive(child_map: &HashMap<i32, Vec<i32>>, pid: i32) -> Vec<i32> {
let mut children: Vec<i32> = child_map.get(&pid).cloned().unwrap_or_default();
for child in children.clone() {
children.extend(get_children_recursive(child_map, child));
}
children
}
let all_children = get_children_recursive(&map, some_pid); For your own purposes, does this look like it would work for you? |
This would definitely work :)
Do you see an problems with such an helper function? |
There's also this nice post, which explains why they added the feature. (Before that, the only approach to get child processes was exactly as you described above) |
Ok. Nevermind, I obviously haven't read the full documentation. I completely omitted this part. |
Ahh, yes it's tempting to use that For completeness, I think procfs should eventually add some functions to interface with with the Independently of that, procfs should also have some documentation and/or examples about how to get a list of child processes. If you're interested in working on any of this, let me know! 😀 |
I'm definitely interested on contributing, so I'll probably add an example for getting children of a process :) |
Hi!
First of all, thanks for providing this library! I'm currently looking for a nice solution to get all child processes of a specific process. After looking at
psutil
andprocfs
, I found thatprocfs
is a lot cleaner and definitely better documented! Your library certainly deserves more attention :)I would like to add the feature for
Process::children()
and it would be awesome to get a short introduction or a few tips on how to approach this.Another thing I'm thinking about is, whether it should only be
Process::children_ids()
plus another functionProcess::children()
, which actually returns a vector ofProcess
.The text was updated successfully, but these errors were encountered: