Comments (6)
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 some_pid
in a flat list, you could do something like:
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?
from procfs.
This would definitely work :)
I just thought, that it would be more convenient to directly use the proc(5) feature for children.
This doesn't require iterating through all processes and allows instant lookup:
/proc/[pid]/task/[tid]/children (since Linux 3.5)
A space-separated list of child tasks of this task. Each
child task is represented by its TID.
This option is intended for use by the checkpoint-restore
(CRIU) system, and reliably provides a list of children only
if all of the child processes are stopped or frozen. It does
not work properly if children of the target task exit while
the file is being read! Exiting children may cause non-exitβ
ing children to be omitted from the list. This makes this
interface even more unreliable than classic PID-based
approaches if the inspected task and its children aren't
frozen, and most code should probably not use this interface.
Until Linux 4.2, the presence of this file was governed by the
CONFIG_CHECKPOINT_RESTORE kernel configuration option. Since
Linux 4.2, it is governed by the CONFIG_PROC_CHILDREN option.
Do you see an problems with such an helper function?
Could backwards compatibility for Linux <3.5
become a problem?
from procfs.
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)
from procfs.
Ok. Nevermind, I obviously haven't read the full documentation.
and most code should probably not use this interface.
I completely omitted this part.
from procfs.
Ahh, yes it's tempting to use that /children
file, but I do believe that the recommended way to get process children is to iterate over all processes.
For completeness, I think procfs should eventually add some functions to interface with with the [pid]/task/[tid]/children
file (since it's a goal of procfs to cover everything in /proc
), but it wouldn't be the recommended way to list child processes for general use.
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! π
from procfs.
I'm definitely interested on contributing, so I'll probably add an example for getting children of a process :)
from procfs.
Related Issues (20)
- Possible vulnerability HOT 1
- Fails to build on FreeBSD HOT 2
- Expose `/proc/<pid>/maps` parsing (and others) HOT 4
- PID should be u32 HOT 2
- Confusing documentation for Stat::comm HOT 3
- Support (limited) use on other platforms HOT 12
- MountInfo does not unescape special characters
- Process uid() fails on some kernels HOT 2
- error[E0599]: no associated item named `PATH` found for struct `OFlags` in the current scope
- Please publish `procfs-core` HOT 2
- No way to match `KernelStats::cpu_time` entries to CPUs when CPUs are hot [un]plugged
- Make CpuInfo iterable HOT 2
- Add support for parsing /proc/mdstat
- KernelVersion::current reads /proc/sys/kernel/osrelease, patch version is incorrect in ubuntu HOT 3
- Consider leaving meminfo values as kilobytes/kibibytes HOT 3
- [Bug] `FDsIter` memory leak HOT 8
- Improve our examples
- Add support for parsing /proc/net/netstat
- Add support for `/proc/net/nf_conntrack`
- panic in `Snmp::from_buf_read` due to missing IcmpMsg line
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. πππ
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from procfs.