How to write a program that monitors a process

How to write a program that monitors a process

Post by Einat Ari » Fri, 26 Apr 2002 16:45:14



Hi,
I'm trying to write a unix program that knows when another process
goes down,(normally or abnormally). I know the pid of the process that
I want to monitor.
I tried using named-pipes - creating the pipe in the monitored process
using mkfifo, and then monitoring it using poll, but sometimes the
poll returns unexpectedly with POLLHUP even if process is still active
and the pipe hasn't been broken.
If you know any other solutions for this problem, please answer.

Thanks in advance,
Einat Ariel

 
 
 

How to write a program that monitors a process

Post by Donald McLachl » Fri, 26 Apr 2002 23:38:17


If it is a child process, use waitpid().

--

Communications Research Centre / RNS    Tel     (613) 998-2845
3701 Carling Ave.,                      Fax     (613) 998-9648
Ottawa, Ontario
K2H 8S2
Canada

 
 
 

How to write a program that monitors a process

Post by Paul Pluzhniko » Sat, 27 Apr 2002 00:10:58



> I'm trying to write a unix program that knows when another process
> goes down,(normally or abnormally). I know the pid of the process that
> I want to monitor.

If your monitor can start the monitored process, the easiest thing
to do is simply to waitpid() for it.

Quote:> If you know any other solutions for this problem, please answer.

You may also do something like:

  while (1) {
      if (-1 == kill(pid, 0) && ESRCH == errno) {
          break; /* monitored process is gone */
      }
      sleep(1); /* insert appropriate delay */
  }

Cheers,

 
 
 

How to write a program that monitors a process

Post by Josh Jone » Sat, 27 Apr 2002 00:50:25



>      if (-1 == kill(pid, 0) && ESRCH == errno) {

I've noticed in some of your previous posts (and this one obviously)
that you go against the traditional "function() == value" conditional
and use instead "value == function()"  ... any reason for this?  Just
being curious.

--
  Joshua Jones  ::  jajones(at)cc.gatech.edu  ::  http://www.intmain.net

  "Quotes in .sigs are useless." -- Me

 
 
 

How to write a program that monitors a process

Post by Donald McLachl » Sat, 27 Apr 2002 01:44:22




> >      if (-1 == kill(pid, 0) && ESRCH == errno) {

> I've noticed in some of your previous posts (and this one obviously)
> that you go against the traditional "function() == value" conditional
> and use instead "value == function()"  ... any reason for this?  Just
> being curious.

I use what you call the traditional style, but I know the reason for using
the other style (and agree with it ... just never do it); defensive programming!

If you meant to type "if(a == 5)", but you typed "if(a = 5)", it is legal syntax,
and not only have you changed the value of a, but the expression always evaluates
to true.

Using the other style if you meant to type "if(5 == a)" but typed "if(5 = a)"
the compile would let you know because it is a syntax error.

Don

--

Communications Research Centre / RNS    Tel     (613) 998-2845
3701 Carling Ave.,                      Fax     (613) 998-9648
Ottawa, Ontario
K2H 8S2
Canada

 
 
 

How to write a program that monitors a process

Post by Chuck Dillo » Sat, 27 Apr 2002 02:24:14



> Hi,
> I'm trying to write a unix program that knows when another process
> goes down,(normally or abnormally). I know the pid of the process that
> I want to monitor.
> I tried using named-pipes - creating the pipe in the monitored process
> using mkfifo, and then monitoring it using poll, but sometimes the
> poll returns unexpectedly with POLLHUP even if process is still active
> and the pipe hasn't been broken.
> If you know any other solutions for this problem, please answer.

> Thanks in advance,
> Einat Ariel

Assuming a simple wait wasn't the answer.  Consider using a semaphore or a lock
file.  The process to be monitored should hold the 'lock'.  If the monitor can
get the 'lock' the process doesn't exist.  Beware of race conditions!

-- ced

--
Chuck Dillon
Senior Software Engineer
Accelrys Inc., a subsidiary of Pharmacopeia, Inc.

 
 
 

How to write a program that monitors a process

Post by Neil Evert » Sat, 27 Apr 2002 03:15:50


Quote:>Hi,
>I'm trying to write a unix program that knows when another process
>goes down,(normally or abnormally). I know the pid of the process that
>I want to monitor.
>I tried using named-pipes - creating the pipe in the monitored process
>using mkfifo, and then monitoring it using poll, but sometimes the
>poll returns unexpectedly with POLLHUP even if process is still active
>and the pipe hasn't been broken.
>If you know any other solutions for this problem, please answer.

Hi,
        Have you thought about using popen.

        Depending upon how often you need to poll it might be useful.

* untested program
------------------------------------------------

FILE    *fp

        fp = popen("ps -ef | fgrep ttyS0 | awk \` { print $2 }\`",
"r");

        if (fp != NULL) {
                if ( fgets( popen_buf, sizeof(popen_buf), fp) ) {
                        popen_buf[sizeof(popen_buf)-1] = '\0';

                        if (atio(popen_buf))
                                proc_id = atio(popen_buf);
                }

                pclose(fp);
        }
---------------------------------------------------

        You could grep for "<spc>pid<spc>" and compare the 2 pids,
this will at least help you to see if the process is still running.

        You could of course do the above through a script and return a
value back through the exec commands (I assume they can call a script,
only ever used it for programs)

HTH
Neil E.
Remove [removetoreply] from email address to mail me direct

 
 
 

How to write a program that monitors a process

Post by Thomas H. Ptac » Sat, 27 Apr 2002 03:41:58


Quote:> I'm trying to write a unix program that knows when another process
> goes down,(normally or abnormally). I know the pid of the process that
> I want to monitor.

ptrace(PT_ATTACH, pid_of_target, 0, 0);
ptrace(PT_CONTINUE, pid_of_target, (caddr_t)1, 0);

Works on BSDs supporting ptrace() and allows you to watch
processes that are unrelated to the monitor/watchdog process
without modifying the code of the target.

Conceptually you'd be doing the same thing that "gdb" does
to debug processes by PID.

 
 
 

How to write a program that monitors a process

Post by Victor Wagn » Sat, 27 Apr 2002 04:06:24



:>
:>      if (-1 == kill(pid, 0) && ESRCH == errno) {

: I've noticed in some of your previous posts (and this one obviously)
: that you go against the traditional "function() == value" conditional
: and use instead "value == function()"  ... any reason for this?  Just
: being curious.

One big reason is that if you mistype assignment (one =) for equality
(two ==) compilier would complain immediately, becouse
it cannot assign to -1 while is quite able to assign to errno.

--
Machine Always Crashes, If Not, The Operating System Hangs (MACINTOSH)
        -- Topic on #Linux

 
 
 

How to write a program that monitors a process

Post by Josh Jone » Sat, 27 Apr 2002 05:35:27



> I use what you call the traditional style, but I know the reason for using
> the other style (and agree with it ... just never do it); defensive programming!

> If you meant to type "if(a == 5)", but you typed "if(a = 5)", it is legal syntax,
> and not only have you changed the value of a, but the expression always evaluates
> to true.

Thanks, I understand the rationale now... however, for me at least, this
is one of the most uncommon programming errors.  I can't recall the last
time I made this particular error... IMHO, the readability of the
'traditional' style *far* outweighs any protection one might get from
using the other method.  After all, quite a few conditionals are just
used for checking return values, so when reading a program quickly for
whatever reason, I want to see the function being called immediately,
at the beginning of the line, instead of having to scan across every line
to find it.  At any rate, it was interesting since I'd never seen it
done that way before.

--
  Joshua Jones  ::  jajones(at)cc.gatech.edu  ::  http://www.intmain.net

  "Quotes in .sigs are useless." -- Me

 
 
 

How to write a program that monitors a process

Post by Paul Pluzhniko » Sat, 27 Apr 2002 10:37:36



> Thanks, I understand the rationale now... however, for me at least, this
> is one of the most uncommon programming errors.  I can't recall the last
> time I made this particular error...

This error may be rare, but when you do make it, you'll
probably spend 2 days trying to find it, because your
mind will refuse to read the code "as written" instead of
"as intended".

Quote:> IMHO, the readability of the 'traditional' style *far*
> outweighs any protection one might get from
> using the other method.  

It's a matter of opinion...

Once you know what it is for, it is not at all difficult
to read the expression "backwards" ...

*Far* outweighs even when you loose 2 days tracking it down
under deadline pressure? Maybe, maybe not...

Cheers,