#include #include #include #include /** ** signal sampler ** ** short program to demonstrate how ** signals can kill a process, bounce off a process ** or be caught by a process **/ int main() { void catcher(int); /* a function to call on CtrlC */ int i; signal(SIGFPE, on_fpe); printf("Case 1: no special arrangements.."); for (i=0;i<10;i++){ putchar('*');fflush(stdout); sleep(1); } putchar('\n'); signal(SIGINT, SIG_IGN); /* ignore INTerrupts */ printf("Case 2: ignoring interrupts.."); for (i=0;i<10;i++){ putchar('*');fflush(stdout); sleep(1); } putchar('\n'); signal(SIGINT, catcher); /* handle interruptions */ printf("Case 3: catching interrupts.."); for (i=0;i<10;i++){ putchar('*');fflush(stdout); sleep(1); } putchar('\n'); return 0; } void catcher(int signum) { printf(" Ouch! on signal %d\n", signum); printf("enter y to divide 3 by 0.0: "); if (getchar() == 'y' ) int x = 3/0.0; system("who"); } /* handler for floating point exception, e.g. divide by 0 */ void on_fpe(int signum) { printf("Got a floating point exception!\n"); }