pcntl_waitid
(PHP 8 >= 8.4.0)
pcntl_waitid — Waits for a child process to change state
Description
int
$idtype = P_ALL,?int
$id = null,array
&$info = [],int
$flags = WEXITED,array
&$resource_usage = []): bool
Obtains status information pertaining to termination, stop, and/or continue events in one of the caller's child processes.
Unless WNOHANG flag is passed, the calling process will become blocked until an error occurs, or status information becomes available that satisfies all of the following:
- The status information is from one of the child processes in the set of child processes specified by the
idtypeandidarguments. - The state change in the status information matches one of the state change flags set in the
flagsargument.
If matching status information is available prior to the call to pcntl_waitid(), return shall be immediate. If matching status information is available for two or more child processes, the order in which their status is reported is unspecified.
Note:
This documentation covers the POSIX specification of the
waitidfunction, along with some additional parameters specific to implementations on Linux, NetBSD and FreeBSD. Please see your system'swaitid(2)man page for specific details as to howwaitidworks on your system.
Parameters
idtypeid- The
idtypeandidarguments are used to specify which children to wait for.POSIX standard idtypeandidargumentsIf idtypeisP_ALLwait for any child process, idis ignored.If idtypeisP_PIDwait for the child with process ID equal to id.If idtypeisP_PGIDwait for any child with process group ID equal to id.Linux specific idtypeandidargumentsIf idtypeisP_PIDFD(since Linux 5.4)wait for the child referred to by the PID file descriptor specified in id. (See the Linuxpidfd_open(2)man page for further information on PID file descriptors.)NetBSD and FreeBSD specific idtypeandidargumentsIf idtypeisP_UIDwait for processes whose effective user ID is equal to id.If idtypeisP_GIDwait for processes whose effective group ID is equal to id.If idtypeisP_SIDwait for processes whose session ID is equal to id. If the child process started its own session, its session ID will be the same as its process ID. Otherwise the session ID of a child process will match the caller's session ID.FreeBSD specific idtypeandidargumentsIf idtypeisP_JAILIDwait for processes within a jail whose jail identifier is equal to id. infoThe
infoparameter is set to an array containing information about the signal.infoarray may contain the following keys:signo: Signal numbererrno: System error numbercode: Signal codestatus: Exit value or signalpid: Sending process IDuid: Real user ID of sending processutime: User time consumedstime: System time consumed
flagsThe value of
flagsis the value of zero or more of the following constants OR'ed together:possible values for flagsWCONTINUEDStatus shall be returned for any continued child process whose status either has not been reported since it continued from a job control stop or has been reported only by calls to pcntl_waitid() with the WNOWAITflag set.WEXITEDWait for processes that have exited. WNOHANGDo not hang if no status is available; return immediately. WNOWAITKeep the process whose status is returned in infoin a waitable state. This shall not affect the state of the process; the process may be waited for again after this call completes.WSTOPPEDStatus shall be returned for any child that has stopped upon receipt of a signal, and whose status either has not been reported since it stopped or has been reported only by calls to pcntl_waitid() with the WNOWAITflag set.resource_usageThe
resource_usageparameter is set to an array containing resource usage statistics from the child process. This is supported either if the wait6 system call is available (e.g. on FreeBSD), or on Linux through the raw waitid system call.
Return Values
pcntl_waitid() returns true if WNOHANG was specified and status is not available for any process specified by idtype and id.
pcntl_waitid() returns true due to the change of state of one of its children.
Otherwise, false is returned and pcntl_get_last_error() can be used to get the errno error number.
Note:
Once an
errnoerror number has been obtained, pcntl_strerror() can be used to get the text message associated with it.
Errors/Exceptions
ECHILD | The calling process has no existing unwaited-for child processes. |
EINTR | pcntl_waitid() was interrupted by a signal. |
EINVAL | An invalid value was specified for flags, or idtype and id specify an invalid set of processes. |
Changelog
| Version | Description |
|---|---|
| 8.5.0 | resource_usage was added. |
See Also
- pcntl_waitpid() - Waits on or returns the status of a forked child
- pcntl_wait() - Waits on or returns the status of a forked child
- pcntl_fork() - Forks the currently running process
- pcntl_signal() - Installs a signal handler
- pcntl_wifexited() - Checks if status code represents a normal exit
- pcntl_wifstopped() - Checks whether the child process is currently stopped
- pcntl_wifsignaled() - Checks whether the status code represents a termination due to a signal
- pcntl_wexitstatus() - Returns the return code of a terminated child
- pcntl_wtermsig() - Returns the signal which caused the child to terminate
- pcntl_wstopsig() - Returns the signal which caused the child to stop