Tuesday, July 27, 2021

SIGINT handler in cxx


#include <iostream>
#include <csignal>
#ifdef _WIN32
#include <Windows.h>
#else
#include <unistd.h>
#endif


using namespace std;

void signalHandler( int signum ) {
    cout << "Interrupt signal (" << signum << ") received.\n";

    // cleanup and close up stuff here
    // terminate program

    exit(signum);
}

int main () {
    // register signal SIGINT and signal handler
    signal(SIGINT, signalHandler);

    while(1) {
        cout << "Going to sleep...." << endl;
        sleep(1);
    }

    return 0;
}

No comments:

Post a Comment