I remember reading that sigaction
is the preferred function to use instead of signal
, which is not as portable (pretty sure it was from The Linux Programming Interface which is an essential reference in my opinion).
My first thought was that Ctrl+C might not send the SIGINT
signal if the system is not POSIX compliant, so I made a test program to see what relevant signals I can raise. The answer by @Potatoswatter popped up while I was trying this out and it seem more relevant to your immediate question of not getting output. Regardless, here is a POSIX friendly source file to test four signals (SIGINT
, SIGTERM
, SIGTSTP
, and SIGQUIT
):
#define _POSIX_C_SOURCE 200809L
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
void sigHandler(int sig);
static int did_catch = 0;
static int sig_caught = 0;
int main() {
struct sigaction sa;
/* buidl sigaction struct */
sa.sa_handler = sigHandler;
if (sigemptyset(&sa.sa_mask) != 0) { perror("\nsigemptyset"); exit(EXIT_FAILURE); }
sa.sa_flags = 0;
/* handle signals */
if (sigaction(SIGINT, &sa, NULL) != 0) { perror("\nproblem adding SIGINT"); exit(EXIT_FAILURE); }
if (sigaction(SIGTERM, &sa, NULL) != 0) { perror("\nproblem adding SIGTERM"); exit(EXIT_FAILURE); }
if (sigaction(SIGQUIT, &sa, NULL) != 0) { perror("\nproblem adding SIGQUIT"); exit(EXIT_FAILURE); }
if (sigaction(SIGTSTP, &sa, NULL) != 0) { perror("\nproblem adding SIGTSTP"); exit(EXIT_FAILURE); }
/* wait for signal */
while (!did_catch) {
pause();
}
/* check caught signals */
if (sig_caught == SIGINT) printf("\nsignal is SIGINT\n");
else if (sig_caught == SIGTERM) printf("\nsignal is SIGTERM\n");
else if (sig_caught == SIGQUIT) printf("\nsignal is SIGQUIT\n");
else if (sig_caught == SIGTSTP) printf("\nsignal is SIGTSTP\n");
else printf("\nsignal captured is not listed\n");
return 0;
}
void sigHandler(int sig) {
sig_caught = sig;
did_catch = 1;
}
Hope this helps.