class Process::Status
一个 Process::Status 包含有关系统进程的信息。
线程局部变量 $? 最初为 nil。一些方法会给它赋一个 Process::Status 对象,该对象代表一个系统进程(正在运行或已终止)。
`ruby -e "exit 99"` stat = $? # => #<Process::Status: pid 1262862 exit 99> stat.class # => Process::Status stat.to_i # => 25344 stat.stopped? # => false stat.exited? # => true stat.exitstatus # => 99
Public Class Methods
Source
static VALUE
rb_process_status_waitv(int argc, VALUE *argv, VALUE _)
{
rb_check_arity(argc, 0, 2);
rb_pid_t pid = -1;
int flags = 0;
if (argc >= 1) {
pid = NUM2PIDT(argv[0]);
}
if (argc >= 2) {
flags = RB_NUM2INT(argv[1]);
}
return rb_process_status_wait(pid, flags);
}
类似于 Process.wait,但返回一个 Process::Status 对象(而不是整数 pid 或 nil);有关 pid 和 flags 的值,请参阅 Process.wait。
如果存在子进程,则等待子进程退出并返回一个包含该进程信息的 Process::Status 对象;设置线程局部变量 $?。
Process.spawn('cat /nop') # => 1155880 Process::Status.wait # => #<Process::Status: pid 1155880 exit 1> $? # => #<Process::Status: pid 1155508 exit 1>
如果没有子进程,则返回一个“空”的 Process::Status 对象,该对象不代表实际进程;不设置线程局部变量 $?。
Process::Status.wait # => #<Process::Status: pid -1 exit 0> $? # => #<Process::Status: pid 1155508 exit 1> # Unchanged.
可能会调用调度器钩子 Fiber::Scheduler#process_wait。
Not available on all platforms.
Public Instance Methods
Source
static VALUE
pst_equal(VALUE st1, VALUE st2)
{
if (st1 == st2) return Qtrue;
return rb_equal(pst_to_i(st1), st2);
}
返回 to_i 的值是否等于 other。
`cat /nop` stat = $? # => #<Process::Status: pid 1170366 exit 1> sprintf('%x', stat.to_i) # => "100" stat == 0x100 # => true
Source
static VALUE
pst_wcoredump(VALUE st)
{
#ifdef WCOREDUMP
int status = PST2INT(st);
return RBOOL(WCOREDUMP(status));
#else
return Qfalse;
#endif
}
如果进程在终止时生成了核心转储,则返回 true,否则返回 false。
Not available on all platforms.
Source
static VALUE
pst_wifexited(VALUE st)
{
int status = PST2INT(st);
return RBOOL(WIFEXITED(status));
}
如果进程正常退出(例如,使用 exit() 调用或程序完成),则返回 true,否则返回 false。
Source
static VALUE
pst_wexitstatus(VALUE st)
{
int status = PST2INT(st);
if (WIFEXITED(status))
return INT2NUM(WEXITSTATUS(status));
return Qnil;
}
如果进程已退出,则返回进程的返回码的最低八位;否则返回 nil。
`exit 99` $?.exitstatus # => 99
Source
static VALUE
pst_inspect(VALUE st)
{
rb_pid_t pid;
int status;
VALUE str;
pid = pst_pid(st);
if (!pid) {
return rb_sprintf("#<%s: uninitialized>", rb_class2name(CLASS_OF(st)));
}
status = PST2INT(st);
str = rb_sprintf("#<%s: ", rb_class2name(CLASS_OF(st)));
pst_message(str, pid, status);
rb_str_cat2(str, ">");
return str;
}
返回 self 的字符串表示形式。
system("false") $?.inspect # => "#<Process::Status: pid 1303494 exit 1>"
Source
static VALUE
pst_pid_m(VALUE self)
{
rb_pid_t pid = pst_pid(self);
return PIDT2NUM(pid);
}
返回进程的进程 ID。
system("false") $?.pid # => 1247002
Source
static VALUE
pst_wifsignaled(VALUE st)
{
int status = PST2INT(st);
return RBOOL(WIFSIGNALED(status));
}
如果进程因未捕获的信号而终止,则返回 true,否则返回 false。
Source
static VALUE
pst_wifstopped(VALUE st)
{
int status = PST2INT(st);
return RBOOL(WIFSTOPPED(status));
}
如果进程已停止,并且相应的 wait 调用设置了 Process::WUNTRACED 标志,则返回 true,否则返回 false。
Source
static VALUE
pst_wstopsig(VALUE st)
{
int status = PST2INT(st);
if (WIFSTOPPED(status))
return INT2NUM(WSTOPSIG(status));
return Qnil;
}
返回导致进程停止的信号编号,如果进程未停止,则返回 nil。
Source
static VALUE
pst_success_p(VALUE st)
{
int status = PST2INT(st);
if (!WIFEXITED(status))
return Qnil;
return RBOOL(WEXITSTATUS(status) == EXIT_SUCCESS);
}
返回
-
如果进程已成功完成并退出,则为
true。 -
如果进程已不成功完成并退出,则为
false。 -
如果进程未退出,则为
nil。
Source
static VALUE
pst_wtermsig(VALUE st)
{
int status = PST2INT(st);
if (WIFSIGNALED(status))
return INT2NUM(WTERMSIG(status));
return Qnil;
}
返回导致进程终止的信号编号,如果进程未被未捕获的信号终止,则返回 nil。
Source
static VALUE
pst_to_i(VALUE self)
{
int status = pst_status(self);
return RB_INT2NUM(status);
}
返回 self 的系统相关的整数状态。
`cat /nop` $?.to_i # => 256
Source
static VALUE
pst_to_s(VALUE st)
{
rb_pid_t pid;
int status;
VALUE str;
pid = pst_pid(st);
status = PST2INT(st);
str = rb_str_buf_new(0);
pst_message(str, pid, status);
return str;
}
返回 self 的字符串表示形式。
`cat /nop` $?.to_s # => "pid 1262141 exit 1"