我正在使用信號在我的程式中傳輸訊息,我撰寫如下代碼:
union sigval sv;
sv.sival_ptr = (void*) data;
pid_t pid = getpid();
if (sigqueue(pid, SIGUSR2, sv) != 0) {
Log("failed: %d", errno);
return 0;
} else {
// do something
}
我發現如果我不注冊處理程式,我的程式會在呼叫時退出sigqueue
,就好像我用 SIGTERM 發送信號一樣,但我的代碼我發送的是 SIGUSR2。我檢查了檔案并沒有談論退出,所以我做錯了什么?
PS 該程式可能沒有按設計注冊處理程式。
uj5u.com熱心網友回復:
我檢查了檔案并沒有談論退出,所以我做錯了什么?
您根本沒有找到正確的檔案:-)
如果用戶未設定處理程式,則從man 7 signal您將獲得信號串列和默認行為。
從手冊頁中竊取:
DESCRIPTION
Linux supports both POSIX reliable signals (hereinafter "standard sig-
nals") and POSIX real-time signals.
Signal dispositions
Each signal has a current disposition, which determines how the process
behaves when it is delivered the signal.
The entries in the "Action" column of the table below specify the de-
fault disposition for each signal, as follows:
Term Default action is to terminate the process.
Ign Default action is to ignore the signal.
Core Default action is to terminate the process and dump core (see
core(5)).
Stop Default action is to stop the process.
Cont Default action is to continue the process if it is currently
stopped.
并從默認操作表中:
Signal Standard Action Comment
------------------------------------------------------------------------
SIGABRT P1990 Core Abort signal from abort(3)
...
SIGUSR1 P1990 Term User-defined signal 1
SIGUSR2 P1990 Term User-defined signal 2
如您所見,如果用戶未指定其他操作,則程式在收到 SIGUSR1 或 SIGUSR2 時終止。
uj5u.com熱心網友回復:
“我檢查了檔案,沒有談論退出”。
該檔案確實談到了退出/終止。從信號手冊:
The entries in the "Action" column of the table below specify the default disposition for each signal, as follows:
Term Default action is to terminate the process.
...
Signal Standard Action Comment
-----------------------------------------
...
SIGUSR2 P1990 Term User-defined signal 2
這告訴我們,SIGUSR2 的默認操作是終止行程。這正是您所觀察到的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/448219.html