1

I have tried implementing this function:

void alarm_handler(int signal)
{
    if(signal==SIGKILL)
    {
        fprintf(stderr,"Process killed\n");
        exit(SIGKILL);
    }
}

And used it in main this way:

signal(SIGKILL,alarm_handler);

So if I press ctrl+c, before exiting it shall print "Process killed", but id does not print it.Why?

Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187

2 Answers2

14

Ctrl+C usually sends SIGINT, not SIGKILL.

orlp
  • 112,504
  • 36
  • 218
  • 315
  • Not usually, but always (unless you configure the terminal tty in very weird ways) is Ctrl-C sending `SIGINT` – Basile Starynkevitch Mar 28 '12 at 20:14
  • 1
    @Basile Starynkevitch: I didn't want to exclude the possibility of a weird terminal. It is possible to write one / configure one that sends `SIGKILL` instead of `SIGINT` :) – orlp Mar 29 '12 at 07:17
3

The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.

Use SIGINT.

Vodun
  • 1,377
  • 1
  • 10
  • 12