What is the correct way to use signal handlers?
I saw the codes below from this question and was wondering why do you still need to put signal(SIGSEGV,sig_func);
inside the sig_func? Wouldn't that create an unending loop when the process receives a SIGSEGV signal?
void sig_func(int sig)
{
write(1, "Caught signal 11\n", 17);
signal(SIGSEGV,sig_func);
}
int main()
{
signal(SIGSEGV,sig_func); //Install the signal handler
//Do work here
}
The signal manual says:
The repeat call to
signal
is used to reinstall the custom handler after it (might) have been reset toSIG_DFL
.