wait

Number0x03
Signature(pid, &status, flags) -> pid or -err
Grouptask
Since1.0
Blockingconditional

Wait for a child to exit and reap it, reading back its exit status.

With a specific PID, waits on that child. With -1, waits on any child. Without WNOHANG the call blocks until a matching child exits; with WNOHANG it returns 0 immediately if none has. On success the child is reaped and its PID returned.

The status word carries how the child died:

Pitfalls

A child that exits but is never waited on stays a zombie until reaped. A parent that spawns and kills children without ever calling wait leaks them; a long-lived supervisor must reap. If the parent dies first, its zombies reparent to pid 1, which reaps them.

Arguments

RegisterNameDescription
r0pidPID of the child to wait on, or -1 for any child
r1&statusPointer the kernel fills with the child's exit status. May be null? — confirm
r2flags`WNOHANG` to return immediately if no child has exited

Returns

The PID of the reaped child, or 0 if WNOHANG and none had exited.

Errors

CodeCondition
ERR_BADARGPID is negative and not -1
ERR_NOENTNo matching child exists, or none became reapable
ERR_BADPTRStatus pointer is invalid

See also