Skip to content
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

Getting processess_by_name for the name of the running process returns duplicated processes #1449

Open
PlexSheep opened this issue Jan 8, 2025 · 3 comments
Labels

Comments

@PlexSheep
Copy link

Describe the bug
I want to use this library to see if my daemon is already running, so I tried to just search for a process with the name netpulsed. This searching is done by a different part of the netpulsed executable. That way, instead of finding the expected two processes (my actual running daemon if it runs and the setup process), it somehow finds 18 processes!

To look more into this, I wrote a tiny process list with this library. It works pretty well by itself, but if you search for programs with the same name as the executed binary, it shows many strange processes. Is it perhaps a strange implementation detail that forks to get the processes?

This on a GNU/Linux system: Linux theseus 6.9.3-76060903-generic #202405300957~1732141768~22.04~f2697e1 SMP PREEMPT_DYNAMIC Wed N x86_64 x86_64 x86_64 GNU/Linux

To Reproduce

Trivial example:

use sysinfo::System;
fn main() {
    let s = System::new_all();
    let processes: Vec<&sysinfo::Process> = s
        .processes_by_name(env!("CARGO_BIN_NAME").as_ref())
        .collect();
    for p in processes {
        println!("pid: {}\tname: {}", p.pid(), p.name().to_string_lossy());
    }
}

Output:

pid: 56382	name: aaa
pid: 56376	name: aaa
pid: 56332	name: aaa
pid: 56381	name: aaa
pid: 56383	name: aaa
pid: 56379	name: aaa
pid: 56385	name: aaa
pid: 56377	name: aaa
pid: 56378	name: aaa
pid: 56389	name: aaa
pid: 56374	name: aaa
pid: 56388	name: aaa
pid: 56380	name: aaa
pid: 56387	name: aaa
pid: 56384	name: aaa
pid: 56386	name: aaa
pid: 56375	name: aaa
@GuillaumeGomez
Copy link
Owner

Maybe take a look at Process:thread_kind?

@PlexSheep
Copy link
Author

PlexSheep commented Jan 8, 2025

After some more research, I see that Linux treats threads as separate processes internally, I did not know that, apologies. Do I then understand correctly, that the process with thread kind None is what's normally called the process, while the others are the threads? If so, it might also be a good idea to add a note for that to the proccess struct in the documentation (unless I missed it?)

pid: 67059	name: aaa	thread kind: Some(Userland)
pid: 67055	name: aaa	thread kind: Some(Userland)
pid: 67044	name: aaa	thread kind: None
pid: 67056	name: aaa	thread kind: Some(Userland)
pid: 67057	name: aaa	thread kind: Some(Userland)
pid: 67062	name: aaa	thread kind: Some(Userland)
pid: 67053	name: aaa	thread kind: Some(Userland)
pid: 67052	name: aaa	thread kind: Some(Userland)
pid: 67060	name: aaa	thread kind: Some(Userland)
pid: 67051	name: aaa	thread kind: Some(Userland)
pid: 67063	name: aaa	thread kind: Some(Userland)
pid: 67054	name: aaa	thread kind: Some(Userland)
pid: 67048	name: aaa	thread kind: Some(Userland)
pid: 67061	name: aaa	thread kind: Some(Userland)
pid: 67050	name: aaa	thread kind: Some(Userland)
pid: 67049	name: aaa	thread kind: Some(Userland)
pid: 67058	name: aaa	thread kind: Some(Userland)

Anyway, that's definitely helpful! Thank you for the quick response :)

@GuillaumeGomez
Copy link
Owner

Might be worth adding this information in Process type documentation directly I suppose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants