I am trying to write a small program where I can send signals and then ask for an action to be triggered if that signal is received. For example, here is an example where I am trying to write a programme that will say you pressed ctrl*c when someone presses ctrl+c. My questions are what you would expect from a beginner so bear with me. First, how can I get ro test the functionality of my program? My udnerstanding is you get the process ID and then through kill -signalnumber PID you will be able to send the signal? For example kill -2 2345 to send teh SIGINT to PID 2345. But mine doesn't work. First I opened a new shell then used shellname & on the terminal to get the PID, then on the terminal wrote kill -2 2345 if the PID was that for example. But I always recieve '... No such process'. Where I am getting it wrong?
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
void signalhandler(int sig);
int main(void)
{
char s[200];
(void) signal (SIGINT, signalhandler);
if (signal(SIGINT, signalhandler) == SIG_ERR) {
perror("signal");
exit(1);
}
return 0;
}
void signalhandler(int sig)
{
printf("You pressed CTRL+C!\n");
}