Is it possible to have a program restart automatically if it crashes?
Something like:
- An unhandled exception is thrown.
- Release all resources allocated by process.
- Start over and call main.
I would like this behavior for a server application I’m working on. If clients miss use the server it can get a std::bac_alloc exception, in which case I would like the server to simply restart instead of crashing and shutting down, thus avoiding manual startup.
I’ve done this before in Windows by running said program from another program via a win32 CreateProcess call. The other program then waits on the “monitored” process to exit, and calls its
CreateProcess()again if it does. You wait for a process to exit by performing a WaitForSingleObject on the process’ handle, which you get as one of the return values from yourCreateProcess()call.You will of course want to program in some way to make the monitoring process shut itself and its child process down.