Thread: Finding out pid of process

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As suggested in the other thread, the EASY solution is to let p2 write it's own pid to a file (because it's easy to find out which pid your own process has).

    However, another solution is to introduce another application (call it p3) that acts as a proxy-object for p2 - essentially, it just starts p3, writes the process id to a file (since it is starting the process, it also knows it's pid from the call to fork). Once it's done with that, it can fall asleep (perhaps waiting for p2 to signal it's own death?).

    If both of these solutions are no good, then you do indeed some way or another scan the process list - whether you read the output of ps or reading from /proc. By the way, popen("ps") may be a simple way to get your process output into a pipe.

    Going back to the context of "watchdog" - whilst application dying by exiting is certainly one case of "no longer working", there are many other instances of applications not working where the process is still alive - it's just not doing what you expect it to do (e.g. it's got stuck in an infinite loop [while-loop with scanf() and poor checking of scanf's return value, or poor handling of error-situations even if the error is indeed detected, is a typical case of this], or it's waiting inside the OS for something to happen, and it never happens [e.g. reading from a pipe, but the other side is not writing anything, but also not closing the pipe, or waiting for a semaphore that never gets signalled]).

    The above can of course not be detected by checking if the process is still alive - it will be alive but not working.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The easiest way would be to have the watchdog launch p2. Then you not only have the PID, you actually get a signal when the process dies, and you can get its exit code.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    Distributed Programming beyonddc's Avatar
    Join Date
    Dec 2001
    Location
    Mass
    Posts
    31
    I actually would like to do something similar. Perhaps there might be a POSIX API that allows you to do so?
    -dc

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by beyonddc View Post
    I actually would like to do something similar. Perhaps there might be a POSIX API that allows you to do so?
    Not directly, but if you use the method described by CornedBee, the signal SIGCHLD will tell you that a child process exited.

    Again, read the discussion about "what does alive mean" that I instigated above.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #20
    Distributed Programming beyonddc's Avatar
    Join Date
    Dec 2001
    Location
    Mass
    Posts
    31
    Quote Originally Posted by matsp View Post
    Not directly, but if you use the method described by CornedBee, the signal SIGCHLD will tell you that a child process exited.

    Again, read the discussion about "what does alive mean" that I instigated above.

    --
    Mats
    Unfortunately my application doesn't exec the process that needs to be monitor.
    -dc

  6. #21
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Again, as mats said, what does it mean for this process to be alive?

    For example, does it log something to a log file periodically? If so, watch the log and check that the update time doesn't exceed a certain threshold. Does it create a system-wide mutex or semaphore for which existence could be queried? Does it bind to a port as a listener such that you could check to see if you can bind to that port? There are a number of ways to do this sort of thing.

  7. #22
    Distributed Programming beyonddc's Avatar
    Join Date
    Dec 2001
    Location
    Mass
    Posts
    31
    Quote Originally Posted by rags_to_riches View Post
    Again, as mats said, what does it mean for this process to be alive?

    For example, does it log something to a log file periodically? If so, watch the log and check that the update time doesn't exceed a certain threshold. Does it create a system-wide mutex or semaphore for which existence could be queried? Does it bind to a port as a listener such that you could check to see if you can bind to that port? There are a number of ways to do this sort of thing.
    Those are all good thoughts. I'll look into it a bit more.

    We're looking into using SNMP to query the system for all running software information.
    -dc

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