#include #include #include #include #include #include #include void signal_action(int signo, siginfo_t *info, void *extra) { int int_val = info->si_value.sival_int; printf("Signal %d, value %d received from parent\n", signo, int_val); if (int_val == 4) { exit(0); } } static void signal_send(int pid, int v) { union sigval sval; sleep(1); sval.sival_int = v; printf("Parent - Sending %d\n", sval.sival_int); sigqueue(pid, SIGRTMIN + 1, sval); } int main(int argc, char *argv[]) { pid_t pid; sigset_t newmask, oldmask; int stat, res; pid = fork(); if (pid == 0) { struct sigaction action; printf("I am the child, and I am blocking the signals!!!\n"); sigemptyset(&newmask); sigaddset(&newmask, SIGRTMIN + 1); sigprocmask(SIG_BLOCK, &newmask, &oldmask); memset(&action, 0, sizeof(struct sigaction)); action.sa_flags = SA_SIGINFO; action.sa_sigaction = signal_action; res = sigaction(SIGRTMIN + 1, &action, NULL); if (res < 0) { perror("Sigaction"); return -1; } /* Wait 5 seconds, to show that signals are actually queued! */ sleep(5); printf("I am the child, and I am now going to listen to the parent!!!\n"); while(1) { sigsuspend(&oldmask); } printf("Child is exiting"); return 0; } signal_send(pid, 6); signal_send(pid, 8); signal_send(pid, 4); pid = wait(&stat); printf("Child exit status = %d\n", WEXITSTATUS(stat)); return 0; }