Thread: Get PID of a process by giving the name

  1. #1
    Registered User
    Join Date
    Oct 2021
    Posts
    10

    Get PID of a process by giving the name

    Linux, EndeavourOS, glibc.

    I would like to be able to find the PID of a process when I supply the program name. I know I could just use the external pidof program, but this is just a small part of code used in a larger program, and I'd rather keep it all in one C file if it's going to be called often.

    Currently, I have this horrid hack that can only tell me if a program is open when given the name, but it'll not give back the PID, which I want:

    pid.c:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    // gcc -Wall pid.c -o pid && pid
    
    int main(int argc, char *argv[]) {
        
       if (0 == system("pidof -x xterm > /dev/null"))
          printf("Running. The pid is x\n");
       else
          printf("Not running.\n"); 
    
       return 0;
    
    }
    I know, it's horrible, but I wanted to at least show some work and get some code going.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,189
    Take a look at the source code for killall5 (which pidof actually runs; note line 1021 which passes control to the function main_pidof).
    sysvinit/killall5.c at master * limingth/sysvinit * GitHub

    Or maybe you're thinking of something more along these lines:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    int main(int argc, char **argv)
    {
        if (argc != 2)
        {
            fprintf(stderr, "Usage: mypidof PROGNAME\n");
            exit(EXIT_FAILURE);
        }
     
        char line[1024] = "pidof ";
        strncat(line, argv[1], sizeof line - 7);
     
        FILE *fin = popen(line, "r");
     
        while (fgets(line, sizeof line, fin))
            printf("%s", line);
     
        pclose(fin);
     
        return 0;
    }
    Last edited by john.c; 10-23-2021 at 02:05 PM.
    We live as it were by chance, and by chance we are governed. - Seneca

  3. #3
    Registered User
    Join Date
    Oct 2021
    Posts
    10
    I typed out an entire answer and pressed the wrong button and lost it...

    I got the killall5 code running and stripped it down ever so little, but it's still above 950 lines, so that may take time. As of now, it's a little too big to reasonably insert to my program, but I'll see if I can get it down to around 200 lines, just making it return a pid.

    As for the next code, I actually had something very much like this already. I think at least that popen is much better than system, so it's definitely a step up. I'll be using that for now.

    Thanks!

  4. #4
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,423
    You can also reference/copy this:

    src/proc.c * master * Lee Shallis / gasp * GitLab

    Don't remember if I put the MIT License on it yet but I'll do that eventually if I haven't, anyways I tried breaking down the common stuff to their own functions so it should be relatively easy to find the type of code you're looking for, just remember that I haven't finished with the project yet so keep any eye out for slip-ups that would cause bugs, don't think there's any in that particular code but it never hurts to be wary in any case.

  5. #5
    Registered User
    Join Date
    Oct 2021
    Posts
    10
    Quote Originally Posted by awsdert View Post
    You can also reference/copy this:

    src/proc.c * master * Lee Shallis / gasp * GitLab

    Don't remember if I put the MIT License on it yet but I'll do that eventually if I haven't, anyways I tried breaking down the common stuff to their own functions so it should be relatively easy to find the type of code you're looking for, just remember that I haven't finished with the project yet so keep any eye out for slip-ups that would cause bugs, don't think there's any in that particular code but it never hurts to be wary in any case.
    Thanks for the link. I'll had a quick look and wasn't able to find the specific code, but it is over 2500 lines, so that's probably why, and it has multiple files, which tend to confuse me as I often prefer to just throw all my code into one.

  6. #6
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,423
    Quote Originally Posted by bathtime View Post
    Thanks for the link. I'll had a quick look and wasn't able to find the specific code, but it is over 2500 lines, so that's probably why, and it has multiple files, which tend to confuse me as I often prefer to just throw all my code into one.
    Yeah most of the process specific stuff was kept there, only realised just now the best way to capture the process name in linux is to just read the first path in /proc/<PID>/maps, typically that's gonna contain the process name at the end of the path, I can't think of a scenario where it would ever not be so, if anyone happens to know of such a situation I would gladly hear or rather read it.

    Edit: Here's what Geany gave me when I told it to open /proc/self/maps:
    Code:
    56453587d000-56453587e000 r--p 00000000 08:01 3414079                    /usr/bin/geany

  7. #7
    Registered User
    Join Date
    Oct 2021
    Posts
    10
    Quote Originally Posted by awsdert View Post
    Yeah most of the process specific stuff was kept there, only realised just now the best way to capture the process name in linux is to just read the first path in /proc/<PID>/maps, typically that's gonna contain the process name at the end of the path, I can't think of a scenario where it would ever not be so, if anyone happens to know of such a situation I would gladly hear or rather read it.

    Edit: Here's what Geany gave me when I told it to open /proc/self/maps:
    Code:
    56453587d000-56453587e000 r--p 00000000 08:01 3414079                    /usr/bin/geany
    I saw some references to /proc here and there, so that makes sense. I'm betting the proc route is also much more efficient than having xlib search for window names.

    I'm going to look into this and post back what I find.

  8. #8
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,423
    Quote Originally Posted by bathtime View Post
    I saw some references to /proc here and there, so that makes sense. I'm betting the proc route is also much more efficient than having xlib search for window names.

    I'm going to look into this and post back what I find.
    Just found that with firefox it doesn't show any paths in the first line, before my suggestion I tried loading the cmdline file in geany but it showed japanese, wasn't till later I noticed geany was mistaking the file as UTF-16, reloading as UTF-8 show me just "geany" for Geany while for firefox it showed "/usr/lib/firefox/firefox", at least for the pid I was in anyways, either way that should be the most reliable for process name, though not for windows, you'd still have to use xlib for now for that, can always investigate the /proc/<PID> standard to see what it should have in there

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux process trees and process chains
    By elururajesh in forum Linux Programming
    Replies: 2
    Last Post: 07-20-2015, 06:22 AM
  2. how to get process info ( to extract process thread id )
    By umen242 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2009, 01:08 PM
  3. Process sending file descriptors to another process
    By Yasir_Malik in forum C Programming
    Replies: 4
    Last Post: 04-07-2005, 07:36 PM
  4. Giving 100%!!
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 03-13-2005, 03:41 PM
  5. Child Process & Parent Process Data :: Win32
    By kuphryn in forum Windows Programming
    Replies: 5
    Last Post: 09-11-2002, 12:19 PM

Tags for this Thread