Finding out pid of process

This is a discussion on Finding out pid of process within the Linux Programming forums, part of the Platform Specific Boards category; Is there anyway to find out the pid of a process totally unrelated to the one you're in? The only ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    101

    Question Finding out pid of process

    Is there anyway to find out the pid of a process totally unrelated to the one you're in?

    The only thing I found was sysctl here but I don't think I should be using functions that go that close to the kernel.

    I know the name of the executable of course. And all the windows solutions don't apply because I have have to do it in c and I'm on windows.

    Thanks for any ideas

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I'm confused. Why do you think a linux command has any hope of working on a windows box?

    (And, by definition, once you say the word "pid" then you are "that close to the kernel".)

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    I'm confused. Why do you think a linux command has any hope of working on a windows box?
    My bad - I meant to say I'm on linux, sorry.

    (And, by definition, once you say the word "pid" then you are "that close to the kernel".)
    Maybe I didn't write correctly, what I meant to say was that with sysctl you can change kernel parameters at run time. I'm pretty ok with reading that kind of information but not with being able to modify it.

    Anyway, do you know how to find the pid?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,449
    Moved to Linux Programming.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    Have a look through all the pseudo-files in /proc would be my guess.

    What happens if there's more than one match?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I was going to suggest ps + grep, but that's not necessarily a C solution.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    Have a look through all the pseudo-files in /proc would be my guess.
    I don't know what you mean by this

    I was going to suggest ps + grep, but that's not necessarily a C solution.
    I thought about launching a fork to this and then somehow manipulate the output into my process but I have a feeling that I'm over complicating

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    start with ls /proc

    There should be an entry for each active PID

    In each /proc/PID are some other files which describe the process.
    One of those should tell you what you need to know.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    start with ls /proc
    There should be an entry for each active PID
    Yes, this is how 'ps' works, I don't think there is another way.
    This is why '/proc'[esses] is named like that after all.

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Cant you call ps within a C code (with exec family) and redirect the stdout output to a file? Or read from the stdout to see what ps printed?

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    Yes the solution I'm thinking bout is launching a process to do an exec with ps aux --sort user but before launching it switching it's stdout file descriptor to a pipe to the father process.

    My problem with this is that I'll have to do that over and over until the process dies.
    Isn't that busy waiting and if so bad programming?

  12. #12
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by kotoko View Post
    Yes the solution I'm thinking bout is launching a process to do an exec with ps aux --sort user but before launching it switching it's stdout file descriptor to a pipe to the father process.

    My problem with this is that I'll have to do that over and over until the process dies.
    Isn't that busy waiting and if so bad programming?
    Well. Obviously you wouldn't do it continuously; once a minute, once every five minutes, however often you want an update. But if you want something real-time-ish, then it's going to have to run all the time, I would guess (no matter what solution you choose, since signals are Right Out).

  14. #14
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by kotoko View Post
    Yes the solution I'm thinking bout is launching a process to do an exec with ps aux --sort user but before launching it switching it's stdout file descriptor to a pipe to the father process.

    My problem with this is that I'll have to do that over and over until the process dies.
    Isn't that busy waiting and if so bad programming?
    What else can you do though? You can use search the /proc directory directly instead of using ps or ls /proc and having to parse the printed information. That would be better.

    What do you want to do anyways? The question was about finding the pid of the process. That won't change...

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    101
    The question was about finding the pid of the process. That won't change...
    Maybe I should reformulate the question.

    I need to do a watchdog for a process. That process (let's call it p2) is also coded by me.

    However, since this is a assignment for school, I'm supposed to figure a way to see if p2 is still alive without p2 doing anything at all.

    The way I'm thinking this means no semaphores and no pipes.

    I thought that if I had the pid things would be easier to find if the process was running hence my question.

Page 1 of 2 12 LastLast
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, 01: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21