Class: Process::Status
Overview
*******************************************************************
<code>Process::Status</code> encapsulates the information on the
status of a running or terminated system process. The built-in
variable <code>$?</code> is either +nil+ or a
<code>Process::Status</code> object.
fork { exit 99 } #=> 26557
Process.wait #=> 26557
$?.class #=> Process::Status
$?.to_i #=> 25344
$? >> 8 #=> 99
$?.stopped? #=> false
$?.exited? #=> true
$?.exitstatus #=> 99
Posix systems record information on processes using a 16-bit
integer. The lower bits record the process status (stopped,
exited, signaled) and the upper bits possibly contain additional
information (for example the program's return code in the case of
exited processes). Pre Ruby 1.8, these bits were exposed directly
to the Ruby program. Ruby now encapsulates these in a
<code>Process::Status</code> object. To maximize compatibility,
however, these objects retain a bit-oriented interface. In the
descriptions that follow, when we talk about the integer value of
_stat_, we're referring to this 16 bit value.
Instance Method Summary collapse
-
#&(num) ⇒ Fixnum
Logical AND of the bits in stat with num.
-
#==(other) ⇒ Boolean
Returns
true
if the integer value of stat equals other. -
#>>(num) ⇒ Fixnum
Shift the bits in stat right num places.
-
#coredump? ⇒ Boolean
Returns
true
if stat generated a coredump when it terminated. -
#exited? ⇒ Boolean
Returns
true
if stat exited normally (for example using anexit()
call or finishing the program). -
#exitstatus ⇒ Fixnum?
Returns the least significant eight bits of the return code of stat.
-
#inspect ⇒ String
Override the inspection method.
-
#pid ⇒ Fixnum
Returns the process ID that this status object represents.
-
#signaled? ⇒ Boolean
Returns
true
if stat terminated because of an uncaught signal. -
#stopped? ⇒ Boolean
Returns
true
if this process is stopped. -
#stopsig ⇒ Fixnum?
Returns the number of the signal that caused stat to stop (or
nil
if self is not stopped). -
#success? ⇒ true, ...
Returns
true
if stat is successful,false
if not. -
#termsig ⇒ Fixnum?
Returns the number of the signal that caused stat to terminate (or
nil
if self was not terminated by an uncaught signal). -
#to_i ⇒ Object
Returns the bits in stat as a
Fixnum
. -
#to_s ⇒ String
Show pid and exit status as a string.
Instance Method Details
#&(num) ⇒ Fixnum
462 463 464 465 466 467 468 |
# File 'process.c', line 462
static VALUE
pst_bitand(VALUE st1, VALUE st2)
{
int status = PST2INT(st1) & NUM2INT(st2);
return INT2NUM(status);
}
|
#==(other) ⇒ Boolean
Returns true
if the integer value of stat equals other.
442 443 444 445 446 447 |
# File 'process.c', line 442
static VALUE
pst_equal(VALUE st1, VALUE st2)
{
if (st1 == st2) return Qtrue;
return rb_equal(pst_to_i(st1), st2);
}
|
#>>(num) ⇒ Fixnum
483 484 485 486 487 488 489 |
# File 'process.c', line 483
static VALUE
pst_rshift(VALUE st1, VALUE st2)
{
int status = PST2INT(st1) >> NUM2INT(st2);
return INT2NUM(status);
}
|
#coredump? ⇒ Boolean
Returns true
if stat generated a coredump when it terminated. Not available on all platforms.
650 651 652 653 654 655 656 657 658 659 660 661 662 663 |
# File 'process.c', line 650
static VALUE
pst_wcoredump(VALUE st)
{
#ifdef WCOREDUMP
int status = PST2INT(st);
if (WCOREDUMP(status))
return Qtrue;
else
return Qfalse;
#else
return Qfalse;
#endif
}
|
#exited? ⇒ Boolean
Returns true
if stat exited normally (for example using an exit()
call or finishing the program).
581 582 583 584 585 586 587 588 589 590 |
# File 'process.c', line 581
static VALUE
pst_wifexited(VALUE st)
{
int status = PST2INT(st);
if (WIFEXITED(status))
return Qtrue;
else
return Qfalse;
}
|
#exitstatus ⇒ Fixnum?
612 613 614 615 616 617 618 619 620 |
# File 'process.c', line 612
static VALUE
pst_wexitstatus(VALUE st)
{
int status = PST2INT(st);
if (WIFEXITED(status))
return INT2NUM(WEXITSTATUS(status));
return Qnil;
}
|
#inspect ⇒ String
Override the inspection method.
system("false")
p $?.inspect #=> "#<Process::Status: pid 12861 exit 1>"
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
# File 'process.c', line 413
static VALUE
pst_inspect(VALUE st)
{
rb_pid_t pid;
int status;
VALUE vpid, str;
vpid = pst_pid(st);
if (NIL_P(vpid)) {
return rb_sprintf("#<%s: uninitialized>", rb_class2name(CLASS_OF(st)));
}
pid = NUM2PIDT(vpid);
status = PST2INT(st);
str = rb_sprintf("#<%s: ", rb_class2name(CLASS_OF(st)));
pst_message(str, pid, status);
rb_str_cat2(str, ">");
return str;
}
|
#pid ⇒ Fixnum
334 335 336 337 338 |
# File 'process.c', line 334
static VALUE
pst_pid(VALUE st)
{
return rb_attr_get(st, rb_intern("pid"));
}
|
#signaled? ⇒ Boolean
Returns true
if stat terminated because of an uncaught signal.
540 541 542 543 544 545 546 547 548 549 |
# File 'process.c', line 540
static VALUE
pst_wifsignaled(VALUE st)
{
int status = PST2INT(st);
if (WIFSIGNALED(status))
return Qtrue;
else
return Qfalse;
}
|
#stopped? ⇒ Boolean
Returns true
if this process is stopped. This is only returned if the corresponding wait
call had the WUNTRACED
flag set.
501 502 503 504 505 506 507 508 509 510 |
# File 'process.c', line 501
static VALUE
pst_wifstopped(VALUE st)
{
int status = PST2INT(st);
if (WIFSTOPPED(status))
return Qtrue;
else
return Qfalse;
}
|
#stopsig ⇒ Fixnum?
Returns the number of the signal that caused stat to stop (or nil
if self is not stopped).
521 522 523 524 525 526 527 528 529 |
# File 'process.c', line 521
static VALUE
pst_wstopsig(VALUE st)
{
int status = PST2INT(st);
if (WIFSTOPPED(status))
return INT2NUM(WSTOPSIG(status));
return Qnil;
}
|
#success? ⇒ true, ...
Returns true
if stat is successful, false
if not. Returns nil
if exited?
is not true
.
631 632 633 634 635 636 637 638 639 |
# File 'process.c', line 631
static VALUE
pst_success_p(VALUE st)
{
int status = PST2INT(st);
if (!WIFEXITED(status))
return Qnil;
return WEXITSTATUS(status) == EXIT_SUCCESS ? Qtrue : Qfalse;
}
|
#termsig ⇒ Fixnum?
Returns the number of the signal that caused stat to terminate (or nil
if self was not terminated by an uncaught signal).
561 562 563 564 565 566 567 568 569 |
# File 'process.c', line 561
static VALUE
pst_wtermsig(VALUE st)
{
int status = PST2INT(st);
if (WIFSIGNALED(status))
return INT2NUM(WTERMSIG(status));
return Qnil;
}
|
#to_i ⇒ Fixnum #to_int ⇒ Fixnum
315 316 317 318 319 |
# File 'process.c', line 315
static VALUE
pst_to_i(VALUE st)
{
return rb_iv_get(st, "status");
}
|
#to_s ⇒ String
Show pid and exit status as a string.
system("false")
p $?.to_s #=> "pid 12766 exit 1"
386 387 388 389 390 391 392 393 394 395 396 397 398 399 |
# File 'process.c', line 386
static VALUE
pst_to_s(VALUE st)
{
rb_pid_t pid;
int status;
VALUE str;
pid = NUM2PIDT(pst_pid(st));
status = PST2INT(st);
str = rb_str_buf_new(0);
pst_message(str, pid, status);
return str;
}
|