fork () will start a new process by copying some of the memory of the parents, but both of them are separate processes. Therefore, it is safe to use the built-in signal handler. The following is an example of starting a child ....
#include <stdio.h> #include <unistd.h> #include <signal.h> #include <stdlib.h> int SHOULD_RUN = 1; void int_sig_handler(int signal){ printf("SIG_INT\n"); if(fork() == 0){ // child code printf("I am Child"); //signal(SIGINT, int_sig_handler); SHOULD_RUN = 1; while(SHOULD_RUN){ printf("I am Running Still...\n"); sleep(1); } exit(0); }else{ // parent code printf("parent"); } SHOULD_RUN = 0; } int main(int argc, const char *argv[]){ signal(SIGINT, int_sig_handler); while(SHOULD_RUN){ sleep(1); } return 0; }
source share