//------------------------------------------------------------------- // myitimer.cpp // // This program shows how to setup an interval timer in a Linux // program so that it will periodically issue a SIGALRM signal. // // compile using: $ make myitimer // // programmer: ALLAN CRUSE // written on: 16 SEP 2004 // revised on: 20 SEP 2005 //------------------------------------------------------------------- #include // for printf() #include // for signal() #include // for setitimer() int signaled = 0; // global variable void on_alarm( int signum ) { signaled = 1; } // signal handler int main( int argc, char **argv ) { // install our signal-handler signal( SIGALRM, on_alarm ); // initialize and install our interval-timer struct itimerval ourit, oldit; // specify the initial delay (i.e., one second) ourit.it_value.tv_sec = 1; ourit.it_value.tv_usec = 0; // specify the periodic delay (i.e., one-half second) ourit.it_interval.tv_sec = 0; ourit.it_interval.tv_usec = 500000; // replace the default itimer with our itimer setitimer( ITIMER_REAL, &ourit, &oldit ); // loop to print another line each time 'SIGALRM' is received printf( "\ec" ); // reset the terminal (this clears the screen) int letter = 'A', count = 0; do { if ( !signaled ) continue; else ++count; signaled = 0; printf( "%20s", " " ); for (int i = 0; i < 40; i++) printf( "%c", letter ); printf( "\n" ); if ( letter == 'Z' ) letter = 'A'; else ++letter; } while ( count < 52 ); // restore original interval-timer and signal-handler setitimer( ITIMER_REAL, &oldit, NULL ); signal( SIGALRM, SIG_DFL ); printf( "\n" ); }