Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

244 Process Control Chapter 8


8.7 waitidFunction


The Single UNIX Specification includes an additional function to retrieve the exit status
of a process. Thewaitidfunction is similar towaitpid,but provides extra flexibility.

#include <sys/wait.h>
int waitid(idtype_tidtype,id_t id,siginfo_t *infop,intoptions);
Returns: 0 if OK,−1 on error

Likewaitpid,waitidallows a process to specify which children to wait for.
Instead of encoding this information in a single argument combined with the process ID
or process group ID, two separate arguments areused. Theidparameter is interpreted
based on the value ofidtype.The types supported aresummarized in Figure8.9.

Constant Description
P_PID Wait for a particular process:idcontains the process ID of the child to wait for.
P_PGID Wait for any child process in a particular process group:idcontains the process
group ID of the children to wait for.
P_ALL Wait for any child process:idis ignored.

Figure 8.9Theidtypeconstants forwaitid

Theoptionsargument is a bitwise OR of the flags shown in Figure8.10. These flags
indicate which state changes the caller is interested in.

Constant Description
WCONTINUED Wait for a process that has previously stopped and has been continued, and
whose status has not yet been reported.
WEXITED Wait for processes that have exited.
WNOHANG Return immediately instead of blocking if there is no child exit status available.
WNOWAIT Don’t destroy the child exit status. The child’s exit status can be retrieved by a
subsequent call towait,waitid,orwaitpid.
WSTOPPED Wait for a process that has stopped and whose status has not yet been reported.

Figure 8.10Theoptionsconstants forwaitid

At least one ofWCONTINUED,WEXITED,orWSTOPPEDmust be specified in theoptions
argument.
Theinfopargument is a pointer to asiginfostructure. This structurecontains
detailed information about the signal generated that caused the state change in the child
process. Thesiginfostructure is discussed further in Section 10.14.

Of the four platforms covered in this book, only Linux 3.2.0, Mac OS X 10.6.8, and Solaris 10
provide support forwaitid.Note, however,that Mac OS X 10.6.8 doesn’t set all the
information we expect in thesiginfostructure.
Free download pdf