Hal Vaughan wrote:
> In Java actions can be set to perform on exit. Is there any way to do
that
> in C or C++?
As Ian said, check out std::atexit. See also std::terminate. Beyond
those, the Boost.Test Program Execution Monitor may be your most
****table option.
http://www.boost.org/libs/test/doc/components/prg_exec_monitor/index.html
> Is there any way to catch a kill signal and if one is sent,
> do some closing down routines before dying?
It's usually not too difficult, but is inherently somewhat non-****table.
If Boost.Test doesn't do what you need, and you're on a POSIX system,
try installing handlers for SIGSEGV and whatever other signals interest
you using the POSIX sigaction function.
> I need to be able to shut down the connection to hardware when the
program
> ends and most of the time that isn't an issue (since I have an exit
> command), but it would help if there were a way to do that if the
program
> were killed accidently as well. (And I'm sure there's no way to do that
in
> case of a crash or core dump!)
Again, if Boost.Test doesn't cover your situation, but if you are using
the GNU C library, man catchsegv(1). In the worst case scenario, you
might arrange for your stack trace to include whatever info some other
process needs to close the hardware gracefully.
http://www.fifi.org/cgi-bin/man2html/usr/share/man/man1/catchsegv.1.gz


|