/**************************** * March 5, 2008 * * Operating Systems * * LIACS * * Spring Semester 2008 * * Assignment #3 * * Written by Sjoerd Henstra * ****************************/ #include #include #include #include #include #include // printerror prints a custom error message followed by the error code and // description to stderr void printerror(const char* message) { fprintf(stderr, "%s\nERROR %i: %s\n", message, errno, strerror(errno)); } // signal handler, handles SIGUSR1 and SIGUSR2 properly static void handler(int signal) { int pid = getpid(); switch(signal) { case SIGUSR1: printf("process %i received SIGUSR1\n", pid); break; case SIGUSR2: printf("process %i received SIGUSR2\n", pid); break; default: printf("process %i caught another signal, this shouldn't happen\n", pid); break; } } int main() { int parent_pid; // holds parent process id, used by child to signal parent int child_pid; // hold child process id, used by parent to signal child int status; // set signal handlers for signals SIGUSR1 and SIGUSR2 if (signal(SIGUSR1, handler) == SIG_ERR) { printerror("Cannot set handler for SIGUSR1."); } if (signal(SIGUSR2, handler) == SIG_ERR) { printerror("Cannot set handler for SIGUSR2."); } parent_pid = getpid(); printf("parent pid: %i\n", parent_pid); // split into parent and child if ((child_pid = fork()) == 0) { printf("child pid: %i\n", getpid()); // send signal to parent kill(parent_pid, SIGUSR1); sleep(1); // LINE 51 <-------- printf("child is done\n"); } else { // send signals to child kill(child_pid, SIGUSR1); kill(child_pid, SIGUSR2); // LINE 59 <--------- sleep(1); // LINE 61 <--------- printf("parent is done\n"); // reap child wait(&status); printf("child reaped\n"); } return 0; }