What's a good manner to use SIGINT for killing app in C++? -
i'm developing server application , want use sigint killing application. although know 2 ways, i'm not sure way kill application.
are there better ways handle siging?
int main() { startserver(); 1. while(true) {sleep(3); if(sigflag) break;} 2. std::getline(std::cin, param); stopserver(); return 0; }
#include<stdio.h> #include<signal.h> #include<unistd.h> bool run = true; void sig_handler(int signo) { if (signo == sigint) { printf("received sigint\n"); run = false; } } int main(void) { if (signal(sigint, sig_handler) == sig_err) printf("\ncan't catch sigint\n"); // long long wait can issue signal process while(run) sleep(1); return 0; } source: http://www.thegeekstuff.com/2012/03/catch-signals-sample-c-code/
instead of printf use return or exit
Comments
Post a Comment