I have some aspirations of one day programming a MUD engine, and I will need to be able to handle both user I/O and timed events. I have written a little program that uses SIGALRM to update the time. I am wondering if I have a sensible "architecture" for the program, given that global variables should generally be avoided. The program is:
Ignoring for now that I do not continually flush the input buffer, is this a reasonable approach, with the alarm handler setting a global variable? Thank you.Code:#include <stdlib.h> #include <stdio.h> #include <signal.h> #include <unistd.h> volatile unsigned int alarmTrig; static void alrmHandler(int x) //indicates alarm triggered and resets same { alarmTrig = 1; alarm(1); return; } int main(int argc, char *argv[]) { struct sigaction theHandler; //sigaction struct char inpChar; int i, bytesRead = 0; sigset_t emptyMask; sigemptyset(&emptyMask); theHandler.sa_handler = alrmHandler; theHandler.sa_mask = emptyMask; //nothing special theHandler.sa_flags = 0; //nothing special here theHandler.sa_restorer = NULL; //option obsolete sigaction(SIGALRM, &theHandler, NULL); alarmTrig=0; alarm(1); for(i=0; i<20; i++) { while(alarmTrig == 0) { bytesRead = fread(&inpChar, 1, 1, stdin); if(bytesRead == 1) { printf("You entered %c.\n",inpChar); } } if (alarmTrig == 1) { alarmTrig=0; printf("%d seconds have gone by.\n",i); alarm(1); } } return 0; }



LinkBack URL
About LinkBacks



