[英]waitid() error: invalid argument
我有來自APUE書和練習的代碼,我需要用waitid()替換wait():
#include "apue.h"
#include <sys/wait.h>
int main(void)
{
pid_t pid;
int status;
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid == 0) /* child */
exit(7);
if (wait(&status) != pid) /* wait for child */
err_sys("wait error");
pr_exit(status); /* and print its status */
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid == 0) /* child */
abort(); /* generates SIGABRT */
if (wait(&status) != pid) /* wait for child */
err_sys("wait error");
pr_exit(status); /* and print its status */
if ((pid = fork()) < 0)
err_sys("fork error");
else if (pid == 0) /* child */
status /= 0; /* divide by 0 generates SIGFPE */
if (wait(&status) != pid) /* wait for child */
err_sys("wait error");
pr_exit(status); /* and print its status */
exit(0);
}
我試過這個:
id_t pid;
siginfo_t info;
pid = fork();
// ...
waitid(P_PID, pid, &info, WNOHANG) // also tried with WNOWAIT
並得到waitid錯誤:參數無效。 當我嘗試: waitid(P_PID, pid, &info, WEXITED)
我得到所有三個waitid()調用的信號編號:17,其中原始代碼的輸出分別是信號7,6和8。 為什么我會得到“無效參數”?如何強制系統生成信號7,6和8?
waitid(P_ALL, 0, &info, WEXITED)
,其中info
的類型為siginfo_t
,收集與wait(&status)
相同的子進程。 他們有所不同
waitid()
的siginfo_t *
參數及其解釋與 。 wait()
的int *
參數及其解釋,和 你好像waitid()
都想使用waitid()
等待一個特定的孩子,那就是: waitid(P_PID, pid, &info, WEXITED)
。
請注意, wait()
返回成功時收集的子進程的pid,而waitid()
在成功時返回0
。
另請注意,盡管siginfo_t
結構具有名為si_status
的成員,但它不等同於wait()
通過其第二個參數提供給調用者的值。 siginfo_t.si_status
是進程的實際退出代碼,而wait()
提供的狀態是幾個不同字段的位掩碼。 你可以通過WEXITSTATUS()
宏從后者獲得退出代碼,但你最好檢查它是否實際上正常終止( WIFEXITED()
)而不是發出信號( WIFSIGNALED()
)。
來自man waitid:
The child state changes to wait for are specified by ORing one or more of the following flags in options:
WEXITED Wait for children that have terminated.
WSTOPPED Wait for children that have been stopped by delivery of a signal.
WCONTINUED Wait for (previously stopped) children that have been resumed by delivery of SIGCONT.
The following flags may additionally be ORed in options:
WNOHANG As for waitpid().
WNOWAIT Leave the child in a waitable state; a later wait call can be used to again retrieve the child status information.
和:
EINVAL The options argument was invalid.
所以,你應該提供WEXITED。
編輯:我為您的代碼添加了幾個定義:
#define pr_exit(n) printf("%d\n", n)
#define err_sys perror
和#includes提供聲明,我看到的是退出狀態0x700,0x86和0x88,其中AFAICS是完全正確的。
那些退出狀態分別是{正常退出狀態7},{退出信號6}和{退出信號8}。 請注意,由於信號7,退出狀態7與退出不同。
(信號17是linux中的SIGCHLD。我不明白為什么你的孩子會以信號17退出。他們或者父母要么必須設置一個SIGCHLD處理程序才能引起這種情況.SIGCHLD有特定和“不尋常”行為。)
必須在options參數中指定WCONTINUED,WEXITED或WSTOPPED中的至少一個。
來自APUE的書
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.