handling Signal
Jun 26, 2021
linux signal hander
#include <signal.h>
#include <unistd.h> // for STDOUT_FILENO
#include <stdio.h>void handler(int num) {
write(STDOUT_FILENO, "refuse to die\n", 13);
}int main() {
// with sigaction, second ctrl + C will work
/*
struct sigaction sa;
sa.sa_handler = handler;
sigaction(SIGINT, &sa, nullptr);
*/ signal(SIGINT, handler); // ctrl + C will not work forever while(1) {
printf("infinite loop with pid:%d\n", getpid());
sleep(1);
}
return 0;
}