Search code examples
cwaitpid

Cannot compile with waitid() and P_PID


I am new to Linux. I am trying to use waitid() to wait for a child process. When I try to compile a file including the following lines using gcc:

id_t cpid = fork();
siginfo_t status;
waitid(P_PID, cpid, &status, WEXITED);

The following error was generated:

error: ‘P_PID’ undeclared (first use in this function)

I included the following libraries:

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <string.h> 
#include <time.h> 

Did I miss something?

Another question is that how can I use WIFSIGNALED() to retrieve information from type siginfo_t?


Solution

  • You need to include <sys/wait.h> and define _XOPEN_SOURCE, as documented in the manual.

    The WIFSIGNALED macro must be used with the integer status obtained from wait, waitpid or waitid. In the case of waitpid, the status is available as the si_status member of the siginfo_t structure. In other words, you would use WIFSIGNALED(info.si_status), info being a structure of type siginfo_t whose address you previously passed to waitid().