I understand that we are working under a UNIX system, and that therefore what you are looking for is to perform a signal processing. To do this you must add information to your process to ask it to capture a specific type of signal, and in doing so, execute a function.
I'll give you a small example of how to do this, where:
my_handler is the function that will be executed when they receive a signal (in this example all the signals they capture execute the same function
signal is the function that associates the signal and the function we want to execute.
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
void mi_handler(int sig){
int i;
for (i=10;i>=0;i--){
printf("<%d> %d\n",sig,i);
sleep(1);
}
}
void main()
{
if (signal(SIGINT, mi_handler) == SIG_ERR){
printf("Error al asignar tratamiento a SIGINT\n");
exit (-1);
}
if (signal(SIGQUIT, mi_handler) == SIG_ERR){
printf("Error al asignar tratamiento a SIGQUIT\n");
exit (-1);
}
while (1){
printf("Sigo durmiendo\n");
sleep(10);
}
exit (0);
}