Git Product home page Git Product logo

Comments (6)

eminence avatar eminence commented on June 27, 2024

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.

Nukesor avatar Nukesor commented on June 27, 2024

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.

Nukesor avatar Nukesor commented on June 27, 2024

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.

Nukesor avatar Nukesor commented on June 27, 2024

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.

eminence avatar eminence commented on June 27, 2024

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.

Nukesor avatar Nukesor commented on June 27, 2024

I'm definitely interested on contributing, so I'll probably add an example for getting children of a process :)

from procfs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.