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