![](/img/trans.png)
[英]sigaction : using “void (*sa_sigaction)(int, siginfo_t *, void *);”
[英]Can i get the signal int constant (like SIGINT or SIGKILL) that terminated a process using using sigaction's flag siginfo with SIGCHLD in C?
我試圖弄清楚是否可以使用傳遞給標志設置為 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 嗎?
問題解決了。
新片段:
static void sigchdl (int sig, siginfo_t *siginfo, void *context)
{
int code = siginfo -> si_code, status = siginfo -> si_status;
printf("si_code: %d\n", code);
printf("si_status: %d\n", status);
if(code == CLD_EXITED) { if(WIFEXITED(status)) printf("Il comando in background con PID: %ld e' terminato normalmente\n",
(long)siginfo->si_pid); }
else if(code == CLD_KILLED) {
//2 = SIGINT, 9 = SIGKILL, 15 = SIGTERM
if(status == 2 || status == 9 || 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 if(code == CLD_DUMPED) perror("il comando è terminato in modo anormale \n");
}
問題在於 WTERMSIG 導致在比賽中毫無用處。 狀態已保持信號恆定值。 仍然需要 si_code 檢查來識別“由信號終止”的情況。
還添加了 CLD_DUMPED 代碼來識別進程的異常中斷。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.