#include <stdio.h> #include <stdlib.h> #include <signal.h> pid_t pid; void driver_handler(int signo) //司机的信号处理函数 { if (signo == SIGUSR1) printf("Let's go!\n"); if (signo == SIGUSR2) printf("Stop the bus!\n"); if (signo == SIGTSTP) kill(pid, SIGUSR1); } void conductor_handler(int signo) //售票员的信号处理函数 { if (signo == SIGINT) kill(getppid(), SIGUSR1); if (signo == SIGQUIT) kill(getppid(), SIGUSR2); if (signo == SIGUSR1) { printf("Please get off the bus!\n"); kill(getppid(), SIGKILL); exit(0); } } int main() { if ((pid = fork()) == -1) { perror("fork"); exit(-1); } if (pid == 0)//conductor { signal(SIGINT,conductor_handler); signal(SIGQUIT,conductor_handler); signal(SIGUSR1,conductor_handler); signal(SIGTSTP, SIG_IGN); } else//driver { signal(SIGUSR1, driver_handler); signal(SIGUSR2, driver_handler); signal(SIGTSTP, driver_handler); signal(SIGINT,SIG_IGN); signal(SIGQUIT,SIG_IGN); } while (1); return 0; }
