While learning signal() system call, I supposed to come across the following code,
#include <stdio.h>
#include <signal.h>
void sigproc(int);
void quitproc(int);
int main(int argc,char **argv)
{
signal(SIGINT, sigproc); //Is it like a normal Call to signal()?
signal(SIGQUIT, quitproc);// This too?
printf("ctrl- c disabled use ctrl\ to quit \n");
while(1);
return 0;
}
void sigproc(int signo)
{
printf("you have pressed ctrl - c \n");
}
void quitproc(int signo)
{
printf("U cant quit\n");
// exit(0);
}
I am calling the function signal() twice in main(). But its executed only when I’m pressing Ctrl-C and Ctrl-\ keys. I thought its also like normal function call. What is actually happening in the signal handler functions?
The
signalfunction establishes a signal handler. What it means: “When my process receives this signal, run this function instead of doing whatever the default was“.So, in your example the calls to
signaldon’t call the function. To actually see the signals in action, do this:From another terminal:
As a side note,
printfand friends aren’t async-signal-safe. It might come as a shocker, but it’s unsafe to use them in signal handlers.As a side side note, even if you tagged your question
Unixit’s important to know that signals (and thesignalfunction) are standard, integral parts of C. Signal handling and thesignalfunction are described in C99 in §7.14.1.