Movatterモバイル変換


[0]ホーム

URL:


HomeClassesMethods

In Files

  • process.c

Parent

Object

Methods

Files

Class/Module Index[+]

Quicksearch
No matching classes.

Process::Status

Process::Status encapsulates the information onthe status of a running or terminated system process. The built-in variable$? is eithernil or aProcess::Status object.

fork {exit99 }#=> 26557Process.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. Thelower bits record the process status (stopped, exited, signaled) and theupper bits possibly contain additional information (for example theprogram's return code in the case of exited processes). Pre Ruby 1.8,these bits were exposed directly to the Ruby program. Ruby now encapsulatesthese in aProcess::Status object. To maximizecompatibility, however, these objects retain a bit-oriented interface. Inthe descriptions that follow, when we talk about the integer value ofstat, we're referring to this 16 bit value.

Public Class Methods

Process::Status.wait(pid=-1, flags=0) → Process::Statusclick to toggle source

Waits for a child process to exit and returns aProcess::Status object containing information onthat process. Which child it waits on depends on the value ofpid:

> 0

Waits for the child whose process ID equalspid.

0

Waits for any child whose process group ID equals that of the callingprocess.

-1

Waits for any child process (the default if nopid is given).

< -1

Waits for any child whose process group ID equals the absolute value ofpid.

Theflags argument may be a logical or of the flag valuesProcess::WNOHANG (do not block if no child available) or Process::WUNTRACED(return stopped children that haven't been reported). Not all flags areavailable on all platforms, but a flag value of zero will work on allplatforms.

Returnsnil if there are no child processes. Not available onall platforms.

May invoke the scheduler hookprocess_wait.

fork {exit99 }#=> 27429Process::Status.wait#=> pid 27429 exit 99$?#=> nilpid =fork {sleep3 }#=> 27440Time.now#=> 2008-03-08 19:56:16 +0900Process::Status.wait(pid,Process::WNOHANG)#=> nilTime.now#=> 2008-03-08 19:56:16 +0900Process::Status.wait(pid,0)#=> pid 27440 exit 99Time.now#=> 2008-03-08 19:56:19 +0900

This is an EXPERIMENTAL FEATURE.

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

Public Instance Methods

stat & num → integerclick to toggle source

Logical AND of the bits instat withnum.

fork {exit0x37 }Process.waitsprintf('%04x',$?.to_i)#=> "3700"sprintf('%04x',$?&0x1e00)#=> "1600"
                static VALUEpst_bitand(VALUE st1, VALUE st2){    int status = PST2INT(st1) & NUM2INT(st2);    return INT2NUM(status);}
stat == other → true or falseclick to toggle source

Returnstrue if the integer value ofstat equalsother.

                static VALUEpst_equal(VALUE st1, VALUE st2){    if (st1 == st2) return Qtrue;    return rb_equal(pst_to_i(st1), st2);}
stat >> num → integerclick to toggle source

Shift the bits instat rightnum places.

fork {exit99 }#=> 26563Process.wait#=> 26563$?.to_i#=> 25344$?>>8#=> 99
                static VALUEpst_rshift(VALUE st1, VALUE st2){    int status = PST2INT(st1) >> NUM2INT(st2);    return INT2NUM(status);}
coredump? → true or falseclick to toggle source

Returnstrue ifstat generated a coredump when itterminated. Not available on all platforms.

                static VALUEpst_wcoredump(VALUE st){#ifdef WCOREDUMP    int status = PST2INT(st);    if (WCOREDUMP(status))        return Qtrue;    else        return Qfalse;#else    return Qfalse;#endif}
exited? → true or falseclick to toggle source

Returnstrue ifstat exited normally (for exampleusing anexit() call or finishing the program).

                static VALUEpst_wifexited(VALUE st){    int status = PST2INT(st);    if (WIFEXITED(status))        return Qtrue;    else        return Qfalse;}
exitstatus → integer or nilclick to toggle source

Returns the least significant eight bits of the return code ofstat. Only available ifexited? istrue.

fork { }#=> 26572Process.wait#=> 26572$?.exited?#=> true$?.exitstatus#=> 0fork {exit99 }#=> 26573Process.wait#=> 26573$?.exited?#=> true$?.exitstatus#=> 99
                static VALUEpst_wexitstatus(VALUE st){    int status = PST2INT(st);    if (WIFEXITED(status))        return INT2NUM(WEXITSTATUS(status));    return Qnil;}
inspect → stringclick to toggle source

Override the inspection method.

system("false")p$?.inspect#=> "#<Process::Status: pid 12861 exit 1>"
                static VALUEpst_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;}
pid → integerclick to toggle source

Returns the process ID that this status object represents.

fork {exit }#=> 26569Process.wait#=> 26569$?.pid#=> 26569
                static VALUEpst_pid_m(VALUE self){    rb_pid_t pid = pst_pid(self);    return PIDT2NUM(pid);}
signaled? → true or falseclick to toggle source

Returnstrue ifstat terminated because of anuncaught signal.

                static VALUEpst_wifsignaled(VALUE st){    int status = PST2INT(st);    if (WIFSIGNALED(status))        return Qtrue;    else        return Qfalse;}
stopped? → true or falseclick to toggle source

Returnstrue if this process is stopped. This is only returnedif the corresponding wait call had the Process::WUNTRACED flag set.

                static VALUEpst_wifstopped(VALUE st){    int status = PST2INT(st);    if (WIFSTOPPED(status))        return Qtrue;    else        return Qfalse;}
stopsig → integer or nilclick to toggle source

Returns the number of the signal that causedstat to stop (ornil if self is not stopped).

                static VALUEpst_wstopsig(VALUE st){    int status = PST2INT(st);    if (WIFSTOPPED(status))        return INT2NUM(WSTOPSIG(status));    return Qnil;}
success? → true, false or nilclick to toggle source

Returnstrue ifstat is successful,false if not. Returnsnil ifexited? is nottrue.

                static VALUEpst_success_p(VALUE st){    int status = PST2INT(st);    if (!WIFEXITED(status))        return Qnil;    return WEXITSTATUS(status) == EXIT_SUCCESS ? Qtrue : Qfalse;}
termsig → integer or nilclick to toggle source

Returns the number of the signal that causedstat to terminate (ornil if self was not terminated by an uncaught signal).

                static VALUEpst_wtermsig(VALUE st){    int status = PST2INT(st);    if (WIFSIGNALED(status))        return INT2NUM(WTERMSIG(status));    return Qnil;}
to_i → integerclick to toggle source

Returns the bits instat as anInteger. Poking around in these bits is platformdependent.

fork {exit0xab }#=> 26566Process.wait#=> 26566sprintf('%04x',$?.to_i)#=> "ab00"
                static VALUEpst_to_i(VALUE self){    int status = pst_status(self);    return RB_INT2NUM(status);}
to_s → stringclick to toggle source

Show pid and exit status as a string.

system("false")p$?.to_s#=> "pid 12766 exit 1"
                static VALUEpst_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;}

This page was generated for Ruby 3.0.0

Generated with Ruby-doc Rdoc Generator 0.42.0.


[8]ページ先頭

©2009-2025 Movatter.jp