Home > Net >  Sending Signals in C?
Sending Signals in C?

Time:10-13

I read:

There are two signals that a process can’t ignore – SIGKILL = terminate the receiving process – SIGSTOP = suspend the receiving process

And some even claimed there is no way we can declare handlers for them

But In C I can write:

#include <signal.h>
#include <stdio.h>

void sigint_handler(int signum) {
    printf("I'm ignoring you!\n");
}

int main() {
    signal(SIGKILL,sigint_handler);
    for(;;) { /*endless loop*/ } return 0;
}

Isn't this a contradiciton?

Side Question, When I write kill 123 in the terminal what signal will be sent I can't find this information anywhere in the internet?

CodePudding user response:

Per POSIX-1.2017 General Information §2.4.3 Signal Actions , a signal may have one of three different dispositions or "actions taken" when it is delivered to a process:

  • SIG_DFL: take the default or "normal" action.
  • SIG_IGN: ignore the signal (take no action).
  • user-defined: the signal is "caught" by a user-defined signal handler.

That said, the same section of POSIX also clarifies:

The system shall not allow the action for the signals SIGKILL or SIGSTOP to be set to SIG_IGN.
....
The system shall not allow a process to catch the signals SIGKILL and SIGSTOP.

If you checked the return code and errno of your signal() call, you'd almost certainly see it failing with EINVAL.

  • Related