Error waitid (): invalid argument

I have this code from an APUE book and exercises in which I need to replace wait () with waitid ():

#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 received a waitid error: Invalid argument. When I tried: waitid(P_PID, pid, &info, WEXITED)I got the signal number: 17 for all three calls of waitid (), where the original code output is signals 7, 6 and 8, respectively. Why am I getting an "invalid argument" and how to get the system to generate signals 7, 6 and 8?

+4
source share
2 answers

waitid(P_ALL, 0, &info, WEXITED), where it infohas a type siginfo_t, collects the same child processes as wait(&status). They differ

  • argument siginfo_t * waitid()and its interpretation in comparison with argument int * wait()and its interpretation, and
  • .

, waitid() , : waitid(P_PID, pid, &info, WEXITED).

, wait() pid , , waitid() 0 .

, siginfo_t si_status, wait(), . siginfo_t.si_status - , , wait(), . WEXITSTATUS(), , (WIFEXITED()) (WIFSIGNALED()).

+2

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 - SIGCHLD linux. , 17. , SIGCHLD, . SIGCHLD "" .)

+1

All Articles