Click to See Complete Forum and Search --> : Detecting a process crash in code
SPatten
September 19th, 2003, 10:31 AM
I have a requirement to write a sort of "watchdog" applet. Basically it spawns my main application (using CreateProcess) and then justs sits in the background and must monitor the main application process for a crash. If it crashes I would need to clean up and spawn it again.
My question is this: How do I determine if another process is running or has frozen? Is there an API call for this that I can use?
Any ideas or help would be appreciated.
Thanks
Stef
Andreas Masur
September 19th, 2003, 11:13 AM
You can use the 'SendMessageTimeout()' (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/Windowing/MessagesandMessageQueues/MessagesandMessageQueuesReference/MessagesandMessageQueuesFunctions/SendMessageTimeout.asp) function with the flags 'SMTO_BLOCK | SMTO_ABORTIFHUNG ' to send a 'WM_NULL' message to the application window.
SPatten
September 29th, 2003, 03:51 AM
Thanks for the suggestion; I have already got a solution which seems to work (although I haven't tested it under every scenario it does work with the basic tests I've done).
This is my solution:
bool Startup::IsProcessRunning( DWORD ProcessId )
{
HANDLE hProc = OpenProcess( PROCESS_QUERY_INFORMATION, FALSE, ProcessId );
DWORD RetCode;
BOOL Res;
if (hProc != NULL)
{
Res = GetExitCodeProcess( hProc, &RetCode );
CloseHandle( hProc );
if( RetCode == STILL_ACTIVE )
return true;
else
return false;
}
return false;
}
Andreas Masur
September 29th, 2003, 04:36 AM
Well...I assume that a process will return 'STILL_ACTIVE' even if it is frozen...nut I never tested that... :cool:
SPatten
September 29th, 2003, 05:25 AM
Yes I think you are correct. My solution will only work in the case of a full blown crash where an exit code is returned. Not ideal but I chose this solution because I did not know how to determine the difference between an application freezing and an application that appears to have frozen but is in actual fact just under really heavy load for awhile. For my particular requirement I want to detect a crash/freeze and then clean it up and re-spawn the application, but in the case of an application being under heavy load (and appearing to have frozen) I don't want to kill it and re-spawn it.
So I opted for the less complete solution and only detect an exit code.
Of course this also detects a user initiated closure of the application (which is not a crash!).
pukys
October 1st, 2003, 03:21 PM
what about spawning your application, which connects to a pipe or window, and posts keep-alives or the current interation step?
If it doesn't count up for a time, kill it, and respawn it.
I used this approach to superwise my webserver... in case it has nothing to do, it posts a keepalive, in all other cases once per second the count of active threads. In case it doesn't answer, I'll kill and respawn it.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.