-2

I have an assignment to write a signal handler function that catches the SIGKILL signal and displays error messages the first 3 times it is called. On the 4th time that it handles a SIGKILL, it should set the signal handler to default and then send out SIGKILL to its process (which it will not catch).

I guess to use a loop and display the error messages in the first 3 iterations. Am I right? I have difficult to send SIGKILL to its process and set the handler to the default (which confuses me).

Can you give me advices?

Kohn J.
  • 25
  • 2
  • 6

2 Answers2

11

According to man 7 signal,

The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
shinkou
  • 5,138
  • 1
  • 22
  • 32
5

what he said is true you cannot catch SIGKILL ans SIGSTOP

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

void funCatch(int signal)
{
    printf("sucessfully caught the kill signal\n");
}

int main(int argc,char **argv)
{
    if(signal(SIGKILL,funCatch)==SIG_ERR)
    {
        printf("cannot catch signal\n");
    }
    return 0;
}

this is a sample code to verify the above statement.

Madan Ram
  • 880
  • 1
  • 11
  • 18
  • 1
    Using `perror` instead of a simple `printf` would show that `errno` is set to `EINVAL` ("Invalid argument"). `sigaction` behaves similarly BTW. – stefanct Oct 27 '15 at 22:34