/* * ipcsignal.c - Shows how to use the kill system call * to send a signal from one process to another */ #include // needed for pid_t #include // needed for wait system call #include // needed for fork, getpid, getppid, kill system calls #include // needed for exit #include // needed for signal system call #include // needed for printf, perror void signal_handler(int sig) { pid_t pid = getpid(); printf("Process %d received signal %d\n", pid, sig); } int main() { // Set a signal handler for a user defined signal number 1 signal(SIGUSR1, signal_handler); pid_t pid = fork(); // fork into 2 processes if(pid < 0) // error { perror("fork"); exit(EXIT_FAILURE); } else if(pid == 0) // child { // Send a signal to the parent by its PID pid_t parent_pid = getppid(); printf("Sending SIGUSR1(%d) to %d\n", SIGUSR1, parent_pid); kill(parent_pid, SIGUSR1); return 0; // Return success } else // parent { // Wait for child wait(0); } return 0; // Return success }