Class: Process::Status

Inherits:
Object show all
Defined in:
process.c,
process.c

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

Instance Method Details

#&(num) ⇒ Fixnum

Logical AND of the bits in stat with num.

fork { exit 0x37 }
Process.wait
sprintf('%04x', $?.to_i)       #=> "3700"
sprintf('%04x', $? & 0x1e00)   #=> "1600"

Returns:



538
539
540
541
542
543
544
# File 'process.c', line 538

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.

Returns:

  • (Boolean)


518
519
520
521
522
523
# File 'process.c', line 518

static VALUE
pst_equal(VALUE st1, VALUE st2)
{
    if (st1 == st2) return Qtrue;
    return rb_equal(pst_to_i(st1), st2);
}

#>>(num) ⇒ Fixnum

Shift the bits in stat right num places.

fork { exit 99 }   #=> 26563
Process.wait       #=> 26563
$?.to_i            #=> 25344
$? >> 8            #=> 99

Returns:



559
560
561
562
563
564
565
# File 'process.c', line 559

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.

Returns:

  • (Boolean)

Returns:

  • (Boolean)


726
727
728
729
730
731
732
733
734
735
736
737
738
739
# File 'process.c', line 726

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).

Returns:

  • (Boolean)

Returns:

  • (Boolean)


657
658
659
660
661
662
663
664
665
666
# File 'process.c', line 657

static VALUE
pst_wifexited(VALUE st)
{
    int status = PST2INT(st);

    if (WIFEXITED(status))
	return Qtrue;
    else
	return Qfalse;
}

#exitstatusFixnum?

Returns the least significant eight bits of the return code of stat. Only available if exited? is true.

fork { }           #=> 26572
Process.wait       #=> 26572
$?.exited?         #=> true
$?.exitstatus      #=> 0

fork { exit 99 }   #=> 26573
Process.wait       #=> 26573
$?.exited?         #=> true
$?.exitstatus      #=> 99

Returns:



688
689
690
691
692
693
694
695
696
# File 'process.c', line 688

static VALUE
pst_wexitstatus(VALUE st)
{
    int status = PST2INT(st);

    if (WIFEXITED(status))
	return INT2NUM(WEXITSTATUS(status));
    return Qnil;
}

#inspectString

Override the inspection method.

system("false")
p $?.inspect #=> "#<Process::Status: pid 12861 exit 1>"

Returns:



489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
# File 'process.c', line 489

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;
}

#pidFixnum

Returns the process ID that this status object represents.

fork { exit }   #=> 26569
Process.wait    #=> 26569
$?.pid          #=> 26569

Returns:



410
411
412
413
414
# File 'process.c', line 410

static VALUE
pst_pid(VALUE st)
{
    return rb_attr_get(st, id_pid);
}

#signaled?Boolean

Returns true if stat terminated because of an uncaught signal.

Returns:

  • (Boolean)

Returns:

  • (Boolean)


616
617
618
619
620
621
622
623
624
625
# File 'process.c', line 616

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.

Returns:

  • (Boolean)

Returns:

  • (Boolean)


577
578
579
580
581
582
583
584
585
586
# File 'process.c', line 577

static VALUE
pst_wifstopped(VALUE st)
{
    int status = PST2INT(st);

    if (WIFSTOPPED(status))
	return Qtrue;
    else
	return Qfalse;
}

#stopsigFixnum?

Returns the number of the signal that caused stat to stop (or nil if self is not stopped).

Returns:



597
598
599
600
601
602
603
604
605
# File 'process.c', line 597

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.

Returns:

  • (true, false, nil)

Returns:

  • (Boolean)


707
708
709
710
711
712
713
714
715
# File 'process.c', line 707

static VALUE
pst_success_p(VALUE st)
{
    int status = PST2INT(st);

    if (!WIFEXITED(status))
	return Qnil;
    return WEXITSTATUS(status) == EXIT_SUCCESS ? Qtrue : Qfalse;
}

#termsigFixnum?

Returns the number of the signal that caused stat to terminate (or nil if self was not terminated by an uncaught signal).

Returns:



637
638
639
640
641
642
643
644
645
# File 'process.c', line 637

static VALUE
pst_wtermsig(VALUE st)
{
    int status = PST2INT(st);

    if (WIFSIGNALED(status))
	return INT2NUM(WTERMSIG(status));
    return Qnil;
}

#to_iFixnum #to_intFixnum

Returns the bits in stat as a Fixnum. Poking around in these bits is platform dependent.

fork { exit 0xab }         #=> 26566
Process.wait               #=> 26566
sprintf('%04x', $?.to_i)   #=> "ab00"

Overloads:



391
392
393
394
395
# File 'process.c', line 391

static VALUE
pst_to_i(VALUE st)
{
    return rb_ivar_get(st, id_status);
}

#to_sString

Show pid and exit status as a string.

system("false")
p $?.to_s         #=> "pid 12766 exit 1"

Returns:



462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'process.c', line 462

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;
}