Thread: [C] Process pid by name

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    56

    [C] Process pid by name

    Hi

    I use linux OS.
    I've already written a function that allow me to get the process name by pid. (searching in /proc). Now I'd like to perform the inverse task.I mean get the process pid by its name.

    I could write a function that search in every folder in /proc for the process name, but i think that should be another solution. Something like include a kernel structure that keep information on the active process.

    Can you suggest me another way? or some references where i can study?

    Thx

    D.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Dedalus View Post
    Something like include a kernel structure that keep information on the active process.
    I don't know the specifics of this, ie, the kernel does not have to have the name at all, but it might. I guess it does, since it puts those command lines in /proc.

    The big issue is that if the normal "user space" tools are not good enough, to get some sort of customized output from the kernel would probably require you to write a separate kernel module, which that will be quite a task.

    This is just an educated guess, tho, maybe somebody is aware of something else.

    Anyway, even if you got the info from the kernel, I don't think that will be some kind of big advantage over just processing the data that the kernel already maintains in /proc. You will still have to search thru a list of names, all that will be saved is creating the list. Which shouldn't take more than one or two functions, maybe 30-60 lines; you put all the names in a struct array of some sort:
    Code:
    struct {
         char *cmdline;
         int pid;
         int status;
         int uid;
    } process;
    and whatever else you might want to know. Generally there is only a few hundred processes anyway. You could use popen() with the ps command to do this too.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Check out what is in the /proc/[PID]/maps file for your PID in question, than compare that to running: ps -ef | grep [PID]. Please do not use the [PID] string, simply replace this with the exact integer pid you are seeking.

    A book I have been reading "Self Service Linux" addresses this in good detail and how to get all the PID information including its complete memory signature on the system.
    Last edited by slingerland3g; 07-16-2009 at 08:24 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. init adopts zombie process?
    By password636 in forum Linux Programming
    Replies: 4
    Last Post: 07-01-2009, 10:05 AM
  2. create a child process that creates a child process
    By cus in forum Linux Programming
    Replies: 9
    Last Post: 01-13-2009, 02:14 PM
  3. Getting other processes class names
    By Hawkin in forum Windows Programming
    Replies: 3
    Last Post: 03-20-2008, 04:02 PM
  4. Problem with forking a process
    By Unitedroad in forum C Programming
    Replies: 10
    Last Post: 10-04-2007, 01:43 AM
  5. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM

Tags for this Thread