我试图弄清楚我是否可以使用传递给标志设置为 SA_SIGINFO 的 sigaction 的自定义 SIGCHLD 处理程序来获得终止子进程的信号。据我了解,si_status 应该根据 si_code 值报告退出值或信号值。
这是 SIGCHLD 处理程序的代码:
static void sigchdl (int sig, siginfo_t *siginfo, void *context)
{
int code = siginfo -> si_code, status = siginfo -> si_status;
if(code == CLD_EXITED)
if(WIFEXITED(status)) printf("the background command with PID: %ld has been successfull\n,(long)siginfo>si_pid);
else if(code == CLD_KILLED) {
//2 = SIGINT, 9 = SIGKILL, 15 = SIGTERM (man signal)
if(WTERMSIG(status) == 2 || WTERMSIG(status) == 9 || WTERMSIG(status) == 15)
printf("Il comando in background con PID: %ld e' stato terminato dal segnale %d, chiamato da: %d\n", (long)siginfo->si_pid, siginfo -> si_status, getpid());
else
perror("Command terminated abnormally\n");
}
}
现在我的问题是,首先:可以这样做吗?
第二:如果是,我可以使用 si_status 和 si_code 吗?