views:

175

answers:

4

How to reload a crashed process on Windows? Of course, I can run a custom monitoring Win service process. But, for example, Firefox: it doesn't seem to install such a thing, but still it can restart itself when it crashes.

+1  A: 

Firefox constantly saves its state to the hard disk, every time you open a tab or click a link, or perform some other action. It also saves a flag saying it shut down safely.

On startup, it reads this all back, and is able to "restore" based on that info.

zildjohn01
+3  A: 

On Vista and above, you can use the RegisterApplicationRestart API to automatically restart when it crashes or hangs.

Before Vista, you need to have a top level exception filter which will do the restart, but be aware that running code inside of a compromised process isn't entirely secure or reliable.

Michael
+1. Nice one. Didn't know about that API call on Vista
Mitch Wheat
A: 

If I recall correctly Windows implements at least some subset of POSIX and so "must" have the signal interface (things like SIGKILL, SIGSEGV, SIGQUIT etc.).

I've never done this but on linux, but you could try setting the unexpected termination trap with signal() (signal.h). From quick scan of docs it seems that very few things can be done while handling signal, it may be possible that even starting a new process is on forbidden list.

Now that I've thought about it, I'd probably go with master/worker pattern, very simple parent thread that does nothing but spawns the worker (that does all the UI / other things). If it does not set a specific "I'm gonna die now" bit but still dies (parent process always gets message / notification that spawned process died) then master respawns the worker. The main theme is keep master very simple and hard to die due to own bugs.

Pasi Savolainen
+1  A: 
  • Structured exception handling (SEH) allows you to catch program crashes and to do something when it happens.
    See: __try and __except
    SEH can be very dangerous though and could lead to your program hanging instead. Please see this article for more information.

  • If you write your program as an NT service then you can set the first, second and subsequent failure actions to "Restart the service".

  • For Windows 2008 server and Windows Vista and Windows 7 you can use the Win32 API RegisterApplicationRestart

Please see my answer here for more information about dealing with different types of program crashes.

Brian R. Bondy