waitpid(...)
public
Waits for a child process to exit, returns its process id, and sets $? to a
Process::Status object containing
information on that process. Which child it waits on depends on the value
of pid:
> 0 |
Waits for the child whose process ID equals pid.
|
0 |
Waits for any child whose process group ID equals that of the calling
process.
|
-1 |
Waits for any child process (the default if no pid is given).
|
< -1 |
Waits for any child whose process group ID equals the absolute value of
pid.
|
The flags argument may be a logical or of the flag values
Process::WNOHANG (do not block if no child available) or Process::WUNTRACED
(return stopped children that haven’t been reported). Not all flags are
available on all platforms, but a flag value of zero will work on all
platforms.
Calling this method raises a SystemError if there are no child processes.
Not available on all platforms.
include Process
fork { exit 99 }
wait
$?.exitstatus
pid = fork { sleep 3 }
Time.now
waitpid(pid, Process::WNOHANG)
Time.now
waitpid(pid, 0)
Time.now
Show source
/*
* call-seq:
* Process.wait() => fixnum
* Process.wait(pid=-1, flags=0) => fixnum
* Process.waitpid(pid=-1, flags=0) => fixnum
*
* Waits for a child process to exit, returns its process id, and
* sets <code>$?</code> to a <code>Process::Status</code> object
* containing information on that process. Which child it waits on
* depends on the value of _pid_:
*
* > 0:: Waits for the child whose process ID equals _pid_.
*
* 0:: Waits for any child whose process group ID equals that of the
* calling process.
*
* -1:: Waits for any child process (the default if no _pid_ is
* given).
*
* < -1:: Waits for any child whose process group ID equals the absolute
* value of _pid_.
*
* The _flags_ argument may be a logical or of the flag values
* <code>Process::WNOHANG</code> (do not block if no child available)
* or <code>Process::WUNTRACED</code> (return stopped children that
* haven't been reported). Not all flags are available on all
* platforms, but a flag value of zero will work on all platforms.
*
* Calling this method raises a <code>SystemError</code> if there are
* no child processes. Not available on all platforms.
*
* include Process
* fork { exit 99 }
* wait
* $?.exitstatus
*
* pid = fork { sleep 3 }
* Time.now
* waitpid(pid, Process::WNOHANG)
* Time.now
* waitpid(pid, 0)
* Time.now
*/
static VALUE
proc_wait(argc, argv)
int argc;
VALUE *argv;
{
VALUE vpid, vflags;
int pid, flags, status;
rb_secure(2);
flags = 0;
rb_scan_args(argc, argv, "02", &vpid, &vflags);
if (argc == 0) {
pid = -1;
}
else {
pid = NUM2INT(vpid);
if (argc == 2 && !NIL_P(vflags)) {
flags = NUM2UINT(vflags);
}
}
if ((pid = rb_waitpid(pid, &status, flags)) < 0)
rb_sys_fail(0);
if (pid == 0) {
return rb_last_status = Qnil;
}
return INT2FIX(pid);
}