简体   繁体   English

waitid()错误:参数无效

[英]waitid() error: invalid argument

I have this code from APUE book and exercise in which I need to replace wait() with waitid(): 我有来自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);
}

I tried this: 我试过这个:

id_t    pid;
siginfo_t info;
pid = fork();
// ...

waitid(P_PID, pid, &info, WNOHANG) // also tried with WNOWAIT

and got waitid error: Invalid argument. 并得到waitid错误:参数无效。 When I tried: waitid(P_PID, pid, &info, WEXITED) I got Signal number: 17 for all three waitid() calls where original code's output are signals 7, 6 and 8 respectively. 当我尝试: waitid(P_PID, pid, &info, WEXITED)我得到所有三个waitid()调用的信号编号:17,其中原始代码的输出分别是信号7,6和8。 Why am I getting "invalid argument" and how can I force system to generate signals 7, 6 and 8? 为什么我会得到“无效参数”?如何强制系统生成信号7,6和8?

waitid(P_ALL, 0, &info, WEXITED) , where info is of type siginfo_t , collects the same child processes as wait(&status) does. waitid(P_ALL, 0, &info, WEXITED) ,其中info的类型为siginfo_t ,收集与wait(&status)相同的子进程。 They differ in 他们有所不同

  1. the siginfo_t * argument of waitid() and its interpretation vs . waitid()siginfo_t *参数及其解释 the int * argument of wait() and its interpretation, and wait()int *参数及其解释,和
  2. the meaning of the two functions' return values. 两个函数的返回值的含义。

You appear to want to use waitid() to wait for a particular child each time, however, and that would be this: waitid(P_PID, pid, &info, WEXITED) . 你好像waitid()都想使用waitid()等待一个特定的孩子,那就是: waitid(P_PID, pid, &info, WEXITED)

Note that whereas wait() returns the pid of the child process it collected on success, waitid() returns 0 on success. 请注意, wait()返回成功时收集的子进程的pid,而waitid()在成功时返回0

Note also that although the siginfo_t structure has a member named si_status , it is not equivalent to the value wait() provides to the caller via its second argument. 另请注意,尽管siginfo_t结构具有名为si_status的成员,但它不等同于wait()通过其第二个参数提供给调用者的值。 siginfo_t.si_status is the process's actual exit code, whereas the status provided by wait() is a bitmask of several different fields. siginfo_t.si_status是进程的实际退出代码,而wait()提供的状态是几个不同字段的位掩码。 You get the exit code from the latter via the WEXITSTATUS() macro, though you would be wise to check whether it in fact terminated normally ( WIFEXITED() ) vs. being signaled ( WIFSIGNALED() ). 你可以通过WEXITSTATUS()宏从后者获得退出代码,但你最好检查它是否实际上正常终止( WIFEXITED() )而不是发出信号( WIFSIGNALED() )。

From man waitid: 来自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.

And: 和:

   EINVAL The options argument was invalid.

So, you should probably provide WEXITED. 所以,你应该提供WEXITED。

Edit: I added a couple of defines to your code: 编辑:我为您的代码添加了几个定义:

#define pr_exit(n) printf("%d\n", n)
#define err_sys perror

and #includes to provide declarations and what I see is exit statuses of 0x700, 0x86 and 0x88, which AFAICS is perfectly correct. 和#includes提供声明,我看到的是退出状态0x700,0x86和0x88,其中AFAICS是完全正确的。

Those exit statuses are {normal exit status of 7}, {exit with signal 6}, and {exit with signal 8}, respectively. 那些退出状态分别是{正常退出状态7},{退出信号6}和{退出信号8}。 Note that exit status of 7 is not the same as exiting due to signal 7. 请注意,由于信号7,退出状态7与退出不同。

(Signal 17 is SIGCHLD in linux. I can't see why your children would be exiting with signal 17. Either they or the parent would have to be setting up a SIGCHLD handler in order to cause that. SIGCHLD has specific and "unusual" behavior.) (信号17是linux中的SIGCHLD。我不明白为什么你的孩子会以信号17退出。他们或者父母要么必须设置一个SIGCHLD处理程序才能引起这种情况.SIGCHLD有特定和“不寻常”行为。)

At least one of WCONTINUED, WEXITED, or WSTOPPED must be specified in the options argument. 必须在options参数中指定WCONTINUED,WEXITED或WSTOPPED中的至少一个。

from APUE book 来自APUE的书

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM