sig_kill.c
1 #include <stdio.h> 2 #include <unistd.h> 3 #include <stdlib.h> 4 #include <signal.h> 5 #include <sys/types.h> 6 7 /* signal handler */ 8 void int_handler(int sig) 9 { 10 int mypid = getpid(); 11 printf("[%i] Ouch - shot in the ...\n", mypid); 12 printf("[%i] exit\n", mypid); 13 exit(2); 14 } 15 16 int main(int argc, char *argv[]) 17 { 18 pid_t pid; 19 20 pid = fork(); 21 if (pid < 0) { 22 fprintf(stderr, "fork failed\n"); 23 exit(1); 24 } 25 26 if (pid == 0) { // child 27 int mypid = getpid(); 28 signal(SIGINT, int_handler); 29 while (1) { 30 usleep(50000); 31 printf("[%i] Running amok!!!\n", mypid); 32 } 33 } 34 else { // parent 35 int mypid = getpid(); 36 int i; 37 for(i=0; i<3; i++) { 38 printf("[%i] is gonna kill you ...\n", mypid); 39 sleep(1); 40 } 41 printf("[%i] is shooting (SIGINT) !!!\n", mypid); 42 kill(pid, SIGINT); 43 printf("[%i] exit\n", mypid); 44 } 45 exit(0); 46 } |