csci4061/notes/08-signals-code/no-interruptions-sigaction.c

42 lines
1.3 KiB
C
Raw Normal View History

2018-01-29 23:28:37 +00:00
// A C program that does not terminate from an interrupt signal.
// Usually pressing Ctrl-C sends this to the foreground program.
//
// To stop this program from running, open another terminal and try
// > pkill -9 a.out
// assuming you named the output program a.out
//
// This version uses portable signal handler setup
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
// Function run when a signals are caught
void handle_signals(int sig_num) {
if(sig_num == SIGINT){
printf("\nNo SIGINT-erruptions allowed.\n");
}
else if(sig_num == SIGTERM){
printf("\nTry to SIGTERM me? Piss off!\n");
}
fflush(stdout);
}
int main () {
// Set handling functions for programs using portable handler setup
// with sigaction()
struct sigaction my_sa = {}; // carries information about handler and options
my_sa.sa_handler = handle_signals; // run function handle_signals
sigemptyset(&my_sa.sa_mask); // don't block any other signals during handling
my_sa.sa_flags = SA_RESTART; // restart system calls on signals if possible
sigaction(SIGTERM, &my_sa, NULL); // register SIGTERM with given action
sigaction(SIGINT, &my_sa, NULL); // register SIGINT with given action
// Infinite loop
while(1) {
sleep(1);
printf("Ma-na na-na!\n");
fflush(stdout);
}
return 0;
}