/***************************************************************/ /* */ /* File: sigTalk.c */ /* Purpose: Demonstrate signals and communicating processes. */ /* Fork a process and parent sends signals to child using */ /* the kill() system call. Child catches signals using */ /* signal() system call. */ /* */ /***************************************************************/ #include #include // Signal handlers void sighup(); void sigint(); void sigquit(); int main() { int pid; // fork process if ((pid = fork()) < 0) { perror("fork"); exit(1); } if (pid == 0) { // Child process // set signal handlers signal(SIGHUP,sighup); signal(SIGINT,sigint); signal(SIGQUIT, sigquit); for(;;); } else { // Parent process printf("\nPARENT: sending SIGHUP\n\n"); kill(pid,SIGHUP); sleep(3); /* pause for 3 secs */ printf("\nPARENT: sending SIGINT\n\n"); kill(pid,SIGINT); sleep(3); /* pause for 3 secs */ printf("\nPARENT: sending SIGQUIT\n\n"); kill(pid,SIGQUIT); sleep(3); } } // end function main void sighup() { signal(SIGHUP,sighup); /* reset signal */ printf("CHILD: I have received a SIGHUP\n"); } void sigint() { signal(SIGINT,sigint); /* reset signal */ printf("CHILD: I have received a SIGINT\n"); } void sigquit() { printf("My parent process has killed me!!!\n"); exit(0); }