Question
How do I trigger SIGINT in this program? #include #include #include #include void sig_handler(int); volatile sig_atomic_t cleanup_exit; int main() { // This variable determines if
How do I trigger SIGINT in this program?
#include #include #include #include
void sig_handler(int);
volatile sig_atomic_t cleanup_exit;
int main() { // This variable determines if we should exit our driver loop // Type sig_atomic_t is an integer that is guaranteed to be processed atomically // Atomically means operations cannot be interrupted (no race conditions) // Why atomic? Consider what happens if a signal is received in the handler cleanup_exit = false;
// Install our signal handlers signal(SIGINT, sig_handler); signal(SIGTERM, sig_handler); // Loop until SIGTERM
// Use pkill(1) to send the termination signal SIGTERM puts("Program running..."); while (! cleanup_exit) { ; }
puts("Cleaning up and exiting"); return 0; }
void sig_handler(int sig) { // save errno if you are doing anything that could trigger an error // i.e. anything that is not async-signal-safe int save_errno = errno;
switch (sig) { case SIGINT : puts("CTRL-C pressed"); break; case SIGTERM : puts("SIGTERM received"); cleanup_exit = true; break;
default : printf("%d not handled ", sig); }
errno = save_errno; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started