/*------------------------------------------------------------------*/ /* testsignalfork.c */ /* Author: Bob Dondero */ /* Ignoring signals in the parent (but not the child) process. */ /*------------------------------------------------------------------*/ #define _GNU_SOURCE #include #include #include #include #include #include int main(int argc, char *argv[]) { void (*pfRet)(int); pid_t iPid; int i = 0; pfRet = signal(SIGINT, SIG_IGN); if (pfRet == SIG_ERR) {perror(argv[0]); return EXIT_FAILURE; } fflush(NULL); iPid = fork(); if (iPid == -1) {perror(argv[0]); return EXIT_FAILURE; } if (iPid == 0) { pfRet = signal(SIGINT, SIG_DFL); if (pfRet == SIG_ERR) {perror(argv[0]); return EXIT_FAILURE; } for (;;) { printf("Child process: %d\n", i); i++; } /* Never should reach this point. */ } for (;;) { printf("Parent process: %d\n", i); i++; } /* Never should reach this point. */ } /* Sample execution: $ gcc -Wall -ansi -pedantic testsignalfork.c -o testsignalfork $ testsignalfork Parent process: 0 Parent process: 1 ... Child process: 0 Child process: 1 ... ^C <- Kills child process, but not parent process Parent process: 12345 Parent process: 12346 ... */