// sig1.cpp
// Glenn G. Chappell
// 17 Feb 2012
//
// For CS 321 Spring 2012
// Signal handler demo

#include <iostream>
using std::cout;
using std::endl;
#include <signal.h>  // for signal, SIGINT


int count;  // Loop counter, bumped up by signal handler


// handler
// Signal handler
// Prints message and bumps up global count
void handler(int i)  // param is which signal 
{
    const int bump = 10000;
    cout << "********** Bumping up count by " << bump << endl;
    count += bump;
}


// main
// Install handler for SIGINT, slowly prints increasing numbers
int main()
{
    // Set up signal handler for SIGINT (sent by ctrl-C in *ix)
    signal(SIGINT, handler);

    // Count up for a while, slowly
    for (count = 0; count < 1000000; ++count)
    {
        cout << "Hit ctrl-C  " << count << endl;
        for (volatile int k = 0; k < 30000000; ++k) ;
            // "volatile" so no optimizing out our delay loop
    }

    return 0;
}

