#include <signal.h> int sigqueue(pid_t pid, int sig, const union sigval value);
union sigval { int sival_int; void *sival_ptr; };
#include <signal.h> int sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
struct sigaction { void (*sa_handler)(int); void (*sa_sigaction)(int, siginfo_t *, void *); sigset_t sa_mask; int sa_flags; void (*sa_restorer)(void); };
siginfo_t { int si_signo; /* Signal number */ int si_errno; /* An errno value */ int si_code; /* Signal code */ int si_trapno; /* Trap number that causedhardware-generated signal(unused on most architectures) */ pid_t si_pid; /* Sending process ID */ uid_t si_uid; /* Real user ID of sending process */ int si_status; /* Exit value or signal */ clock_t si_utime; /* User time consumed */ clock_t si_stime; /* System time consumed */ sigval_t si_value; /* Signal value */ int si_int; /* POSIX.1b signal */ void *si_ptr; /* POSIX.1b signal */ int si_overrun; /* Timer overrun count; POSIX.1b timers */ int si_timerid; /* Timer ID; POSIX.1b timers */ void *si_addr; /* Memory location which caused fault */ long si_band; /* Band event (was int in glibc 2.3.2 and earlier) */ int si_fd; /* File descriptor */ short si_addr_lsb; /* Least significant bit of address since Linux 2.6.32) */ }
void sig_func(int signo, siginfo_t *info, void *arg) { // sleep(6); printf("====%s== [child] handle signo: %d==arg: %d=\n", __func__, signo, info->si_int); } void child_process_do(void) { struct sigaction act; printf("====%s==child pid: %d===\n", __func__, getpid()); //signal(SIGRTMIN, sig_func); //signal(SIGALRM, sig_func); act.sa_sigaction = sig_func; act.sa_flags = SA_SIGINFO; if (sigaction(SIGALRM, &act, NULL) < 0) { //此处安装信号处理函数 fprintf(stderr, "sigaction: %s\n", strerror(errno)); return; } while (1) { sleep(10); } } void parent_process_do(pid_t pid) { int i, val = 100; union sigval sigarg; sleep(1); printf("====%s==parent pid: %d===\n", __func__, getpid()); for (i = 0; i < 5; i++) { printf("====%s==[parent] send signal <%d> to pid <%d>==\n", __func__, SIGRTMIN, pid); //kill(pid, SIGRTMIN); //kill(pid, SIGALRM); sigarg.sival_int = val + i; sigqueue(pid, SIGALRM, sigarg); //此处发送SIGALRM信号,sigarg为要传送的参数 sleep(1); } waitpid(pid, NULL, 0); } int main(int argc, const char *argv[]) { pid_t pid; pid = fork(); if (pid < 0) { fprintf(stderr, "fork: %s\n", strerror(errno)); return -1; } if (0 == pid) { child_process_do(); } else { parent_process_do(pid); } return 0; }
原文:http://blog.csdn.net/shallnet/article/details/41451815