PDA

View Full Version : How to make a clean exit when application is terminated by "kill" command



baobui
26th July 2011, 13:22
Hi,
I have an application in which I use the watchdog timer. This is an embedded application so it does not have the "close" button. The only way tho close it is "kill" it from the console. The problem is when the application is "killed", the watchdog timer is not deactivated, so it reset the device.

What is the protected function that I have to re-implement to de-activate the watchdog when application is killed? I tried closeEvent() but this function is not called the application is killed.

Thanks

mcosta
26th July 2011, 16:57
If you're talking about UNIX "kill" command, it send a SIGNAL to the application.
Some signals can be handled bou others cannot. Read here (http://linux.die.net/man/2/signal) and here (http://linux.die.net/man/7/signal) for more details.

If you handle a signal, you can send a quit event to QApplication

baobui
27th July 2011, 04:24
Thanks,

I finally make it work by this code:

void signalhandler(int sig)
{
if (sig == SIGINT)
{
printf("will quit by SIGINT\n");
qApp->quit();
}
else if (sig == SIGTERM)
{
printf("will quit by SIGTERM\n");
qApp->quit();
}
}

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
autoprint w;
signal(SIGINT,signalhandler);
signal(SIGTERM,signalhandler);
QObject::connect(&a, SIGNAL(aboutToQuit()),&w,SLOT(cleanup()));
w.show();
return a.exec();
}