Monday, April 23, 2018

Catching Signal, Self-driven with raise function

Till now, we were trying to send signals from one process to another.  Today, we will self-generate the signal within its own function using raise.

Here is the sample program.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <stdio.h>
#include <signal.h>

void signal_catch(int signum)
{
 printf("\tCaught the signal %d\n",signum);
}

void main()
{
 signal(SIGINT,signal_catch);
 
 printf("Issuing SIGINT signal to itself\n"); 
 raise (SIGINT);
 printf("Exiting\n");
}

As usual, we are registering signal function 'signal_catch' for SIGINT signal.  Using raise function, we are generating signal to its own function.

When we run the program, it is observed that signal_catch function will be executed because it caught the signal SIGINT because of 'raise'.


root@kali:~# ./a.out
Issuing SIGINT signal to itself
 Caught the signal 2
Exiting


No comments:

Post a Comment