Module: Process
Relationships & Source Files | |
Namespace Children | |
Modules:
| |
Classes:
| |
Defined in: | process.c, process.c, ruby.c, signal.c |
Overview
::Module
to handle processes.
Constant Summary
-
CLOCK_BOOTTIME =
see .clock_gettime
CLOCKID2NUM(CLOCK_BOOTTIME)
-
CLOCK_BOOTTIME_ALARM =
see .clock_gettime
CLOCKID2NUM(CLOCK_BOOTTIME_ALARM)
-
CLOCK_MONOTONIC =
see .clock_gettime
CLOCKID2NUM(CLOCK_MONOTONIC)
-
CLOCK_MONOTONIC_COARSE =
see .clock_gettime
CLOCKID2NUM(CLOCK_MONOTONIC_COARSE)
-
CLOCK_MONOTONIC_FAST =
see .clock_gettime
CLOCKID2NUM(CLOCK_MONOTONIC_FAST)
-
CLOCK_MONOTONIC_PRECISE =
see .clock_gettime
CLOCKID2NUM(CLOCK_MONOTONIC_PRECISE)
-
CLOCK_MONOTONIC_RAW =
see .clock_gettime
CLOCKID2NUM(CLOCK_MONOTONIC_RAW)
-
CLOCK_MONOTONIC_RAW_APPROX =
see .clock_gettime
CLOCKID2NUM(CLOCK_MONOTONIC_RAW_APPROX)
-
CLOCK_PROCESS_CPUTIME_ID =
see .clock_gettime
CLOCKID2NUM(CLOCK_PROCESS_CPUTIME_ID)
-
CLOCK_PROF =
see .clock_gettime
CLOCKID2NUM(CLOCK_PROF)
-
CLOCK_REALTIME =
see .clock_gettime
CLOCKID2NUM(CLOCK_REALTIME)
-
CLOCK_REALTIME_ALARM =
see .clock_gettime
CLOCKID2NUM(CLOCK_REALTIME_ALARM)
-
CLOCK_REALTIME_COARSE =
see .clock_gettime
CLOCKID2NUM(CLOCK_REALTIME_COARSE)
-
CLOCK_REALTIME_FAST =
see .clock_gettime
CLOCKID2NUM(CLOCK_REALTIME_FAST)
-
CLOCK_REALTIME_PRECISE =
see .clock_gettime
CLOCKID2NUM(CLOCK_REALTIME_PRECISE)
-
CLOCK_SECOND =
see .clock_gettime
CLOCKID2NUM(CLOCK_SECOND)
-
CLOCK_THREAD_CPUTIME_ID =
see .clock_gettime
CLOCKID2NUM(CLOCK_THREAD_CPUTIME_ID)
-
CLOCK_UPTIME =
see .clock_gettime
CLOCKID2NUM(CLOCK_UPTIME)
-
CLOCK_UPTIME_FAST =
see .clock_gettime
CLOCKID2NUM(CLOCK_UPTIME_FAST)
-
CLOCK_UPTIME_PRECISE =
see .clock_gettime
CLOCKID2NUM(CLOCK_UPTIME_PRECISE)
-
CLOCK_UPTIME_RAW =
see .clock_gettime
CLOCKID2NUM(CLOCK_UPTIME_RAW)
-
CLOCK_UPTIME_RAW_APPROX =
see .clock_gettime
CLOCKID2NUM(CLOCK_UPTIME_RAW_APPROX)
-
CLOCK_VIRTUAL =
see .clock_gettime
CLOCKID2NUM(CLOCK_VIRTUAL)
-
PRIO_PGRP =
see .setpriority
INT2FIX(PRIO_PGRP)
-
PRIO_PROCESS =
see .setpriority
INT2FIX(PRIO_PROCESS)
-
PRIO_USER =
see .setpriority
INT2FIX(PRIO_USER)
-
RLIMIT_AS =
Maximum size of the process’s virtual memory (address space) in bytes.
see the system getrlimit(2) manual for details.
INT2FIX(RLIMIT_AS)
-
RLIMIT_CORE =
Maximum size of the core file.
see the system getrlimit(2) manual for details.
INT2FIX(RLIMIT_CORE)
-
RLIMIT_CPU =
CPU time limit in seconds.
see the system getrlimit(2) manual for details.
INT2FIX(RLIMIT_CPU)
-
RLIMIT_DATA =
Maximum size of the process’s data segment.
see the system getrlimit(2) manual for details.
INT2FIX(RLIMIT_DATA)
-
RLIMIT_FSIZE =
Maximum size of files that the process may create.
see the system getrlimit(2) manual for details.
INT2FIX(RLIMIT_FSIZE)
-
RLIMIT_MEMLOCK =
Maximum number of bytes of memory that may be locked into RAM.
see the system getrlimit(2) manual for details.
INT2FIX(RLIMIT_MEMLOCK)
-
RLIMIT_MSGQUEUE =
Specifies the limit on the number of bytes that can be allocated for POSIX message queues for the real user ID of the calling process.
see the system getrlimit(2) manual for details.
INT2FIX(RLIMIT_MSGQUEUE)
-
RLIMIT_NICE =
Specifies a ceiling to which the process’s nice value can be raised.
see the system getrlimit(2) manual for details.
INT2FIX(RLIMIT_NICE)
-
RLIMIT_NOFILE =
Specifies a value one greater than the maximum file descriptor number that can be opened by this process.
see the system getrlimit(2) manual for details.
INT2FIX(RLIMIT_NOFILE)
-
RLIMIT_NPROC =
The maximum number of processes that can be created for the real user ID of the calling process.
see the system getrlimit(2) manual for details.
INT2FIX(RLIMIT_NPROC)
-
RLIMIT_RSS =
Specifies the limit (in pages) of the process’s resident set.
see the system getrlimit(2) manual for details.
INT2FIX(RLIMIT_RSS)
-
RLIMIT_RTPRIO =
Specifies a ceiling on the real-time priority that may be set for this process.
see the system getrlimit(2) manual for details.
INT2FIX(RLIMIT_RTPRIO)
-
RLIMIT_RTTIME =
Specifies limit on CPU time this process scheduled under a real-time scheduling policy can consume.
see the system getrlimit(2) manual for details.
INT2FIX(RLIMIT_RTTIME)
-
RLIMIT_SBSIZE =
Maximum size of the socket buffer.
INT2FIX(RLIMIT_SBSIZE)
-
RLIMIT_SIGPENDING =
Specifies a limit on the number of signals that may be queued for the real user ID of the calling process.
see the system getrlimit(2) manual for details.
INT2FIX(RLIMIT_SIGPENDING)
-
RLIMIT_STACK =
Maximum size of the stack, in bytes.
see the system getrlimit(2) manual for details.
INT2FIX(RLIMIT_STACK)
-
RLIM_INFINITY =
see .setrlimit
inf
-
RLIM_SAVED_CUR =
see .setrlimit
v
-
RLIM_SAVED_MAX =
see .setrlimit
v
-
WNOHANG =
see .wait
INT2FIX(WNOHANG)
-
WUNTRACED =
see .wait
INT2FIX(WUNTRACED)
Class Attribute Summary
-
.euid ⇒ Integer
rw
mod_func
Returns the effective user ID for this process.
-
.euid=(user)
rw
mod_func
Sets the effective user ID for this process.
-
.gid ⇒ Integer
rw
mod_func
Returns the (real) group ID for this process.
-
.gid=(integer) ⇒ Integer
rw
mod_func
Sets the group ID for this process.
-
.groups ⇒ Array
rw
mod_func
Get an
::Array
of the group IDs in the supplemental group access list for this process. -
.groups=(array) ⇒ Array
rw
mod_func
Set the supplemental group access list to the given
::Array
of group IDs. -
.maxgroups ⇒ Integer
rw
mod_func
Returns the maximum number of gids allowed in the supplemental group access list.
-
.maxgroups=(integer) ⇒ Integer
rw
mod_func
Sets the maximum number of gids allowed in the supplemental group access list.
-
.pid ⇒ Integer
readonly
mod_func
Alias for $$.
-
.uid ⇒ Integer
rw
mod_func
Returns the (real) user ID of this process.
-
.uid=(user) ⇒ Numeric
rw
mod_func
Sets the (user) user ID for this process.
Class Method Summary
-
.abort
Alias for Kernel.abort.
-
.exec([env,] command... [,options])
Alias for Kernel.exec.
-
.exit(status = true)
Alias for Kernel.exit.
-
.exit!(status = false)
Alias for Kernel.exit!.
-
.fork ⇒ Integer?
Alias for Kernel.fork.
-
.last_status ⇒ Process?
Returns the status of the last executed child process in the current thread.
-
.spawn([env,] command... [,options]) ⇒ pid
Alias for Kernel.spawn.
-
.argv0 ⇒ frozen_string
mod_func
Returns the name of the script being executed.
-
.clock_getres(clock_id [, unit]) ⇒ Numeric
mod_func
Returns the time resolution returned by POSIX clock_getres() function.
-
.clock_gettime(clock_id [, unit]) ⇒ Numeric
mod_func
Returns a time returned by POSIX clock_gettime() function.
-
.daemon ⇒ 0
mod_func
Detach the process from controlling terminal and run in the background as system daemon.
-
.detach(pid) ⇒ Thread
mod_func
Some operating systems retain the status of terminated child processes until the parent collects that status (normally using some variant of
wait()
). -
.egid ⇒ Integer
mod_func
Returns the effective group ID for this process.
-
.egid=(integer) ⇒ Integer
mod_func
Sets the effective group ID for this process.
-
.getpgid(pid) ⇒ Integer
mod_func
Returns the process group ID for the given process id.
-
.getpgrp ⇒ Integer
mod_func
Returns the process group ID for this process.
-
.getpriority(kind, integer) ⇒ Integer
mod_func
Gets the scheduling priority for specified process, process group, or user.
-
.getrlimit(resource) ⇒ Array, max_limit
mod_func
Gets the resource limit of the process.
-
.getsid ⇒ Integer
mod_func
Returns the session ID for the given process id.
-
.initgroups(username, gid) ⇒ Array
mod_func
Initializes the supplemental group access list by reading the system group database and using all groups of which the given user is a member.
-
.kill(signal, pid, ...) ⇒ Integer
mod_func
Sends the given signal to the specified process id(s) if pid is positive.
-
.ppid ⇒ Integer
mod_func
Returns the process id of the parent of this process.
-
.setpgid(pid, integer) ⇒ 0
mod_func
Sets the process group ID of pid (0 indicates this process) to integer.
-
.setpgrp ⇒ 0
mod_func
Equivalent to
setpgid(0,0)
. -
.setpriority(kind, integer, priority) ⇒ 0
mod_func
See #getpriority.
-
.setproctitle(string) ⇒ String
mod_func
Sets the process title that appears on the ps(1) command.
-
.setrlimit(resource, cur_limit, max_limit) ⇒ nil
mod_func
Sets the resource limit of the process.
-
.setsid ⇒ Integer
mod_func
Establishes this process as a new session and process group leader, with no controlling tty.
-
.times ⇒ Process
mod_func
Returns a
Tms
structure (seeProcess::Tms
) that contains user and system CPU times for this process, and also for children processes. -
.wait ⇒ Integer
(also: .waitpid)
mod_func
Waits for a child process to exit, returns its process id, and sets
$?
to aStatus
object containing information on that process. - .wait2(pid = -1, flags = 0) ⇒ Array, status (also: .waitpid2) mod_func
-
.waitall ⇒ Array, ...
mod_func
Waits for all children, returning an array of pid/status pairs (where status is a
Status
object). -
.waitpid(pid = -1, flags = 0) ⇒ Integer
mod_func
Alias for .wait.
-
.waitpid2(pid = -1, flags = 0) ⇒ Array, status
mod_func
Alias for .wait2.
Class Attribute Details
Returns the effective user ID for this process.
Process.euid #=> 501
# File 'process.c', line 7001
static VALUE proc_geteuid(VALUE obj) { rb_uid_t euid = geteuid(); return UIDT2NUM(euid); }
.euid=(user) (rw, mod_func)
Sets the effective user ID for this process. Not available on all platforms.
# File 'process.c', line 7040
static VALUE proc_seteuid_m(VALUE mod, VALUE euid) { check_uid_switch(); proc_seteuid(OBJ2UID(euid)); return euid; }
Returns the (real) group ID for this process.
Process.gid #=> 500
# File 'process.c', line 6435
static VALUE proc_getgid(VALUE obj) { rb_gid_t gid = getgid(); return GIDT2NUM(gid); }
.gid=(integer) ⇒ Integer (rw, mod_func)
Sets the group ID for this process.
# File 'process.c', line 6451
static VALUE proc_setgid(VALUE obj, VALUE id) { rb_gid_t gid; check_gid_switch(); gid = OBJ2GID(id); #if defined(HAVE_SETRESGID) if (setresgid(gid, -1, -1) < 0) rb_sys_fail(0); #elif defined HAVE_SETREGID if (setregid(gid, -1) < 0) rb_sys_fail(0); #elif defined HAVE_SETRGID if (setrgid(gid) < 0) rb_sys_fail(0); #elif defined HAVE_SETGID { if (getegid() == gid) { if (setgid(gid) < 0) rb_sys_fail(0); } else { rb_notimplement(); } } #endif return GIDT2NUM(gid); }
.groups ⇒ Array (rw, mod_func)
Get an ::Array
of the group IDs in the supplemental group access list for this process.
Process.groups #=> [27, 6, 10, 11]
Note that this method is just a wrapper of getgroups(2). This means that the following characteristics of the result completely depend on your system:
-
the result is sorted
-
the result includes effective GIDs
-
the result does not include duplicated GIDs
You can make sure to get a sorted unique ::Process::GID
list of the current process by this expression:
Process.groups.uniq.sort
# File 'process.c', line 6555
static VALUE proc_getgroups(VALUE obj) { VALUE ary, tmp; int i, ngroups; rb_gid_t *groups; ngroups = getgroups(0, NULL); if (ngroups == -1) rb_sys_fail(0); groups = ALLOCV_N(rb_gid_t, tmp, ngroups); ngroups = getgroups(ngroups, groups); if (ngroups == -1) rb_sys_fail(0); ary = rb_ary_new(); for (i = 0; i < ngroups; i++) rb_ary_push(ary, GIDT2NUM(groups[i])); ALLOCV_END(tmp); return ary; }
.groups=(array) ⇒ Array (rw, mod_func)
# File 'process.c', line 6599
static VALUE proc_setgroups(VALUE obj, VALUE ary) { int ngroups, i; rb_gid_t *groups; VALUE tmp; PREPARE_GETGRNAM; Check_Type(ary, T_ARRAY); ngroups = RARRAY_LENINT(ary); if (ngroups > maxgroups()) rb_raise(rb_eArgError, "too many groups, %d max", maxgroups()); groups = ALLOCV_N(rb_gid_t, tmp, ngroups); for (i = 0; i < ngroups; i++) { VALUE g = RARRAY_AREF(ary, i); groups[i] = OBJ2GID1(g); } FINISH_GETGRNAM; if (setgroups(ngroups, groups) == -1) /* ngroups <= maxgroups */ rb_sys_fail(0); ALLOCV_END(tmp); return proc_getgroups(obj); }
.maxgroups ⇒ Integer (rw, mod_func)
Returns the maximum number of gids allowed in the supplemental group access list.
Process.maxgroups #=> 32
# File 'process.c', line 6675
static VALUE proc_getmaxgroups(VALUE obj) { return INT2FIX(maxgroups()); }
.maxgroups=(integer) ⇒ Integer (rw, mod_func)
Sets the maximum number of gids allowed in the supplemental group access list.
# File 'process.c', line 6693
static VALUE proc_setmaxgroups(VALUE obj, VALUE val) { int ngroups = FIX2INT(val); int ngroups_max = get_sc_ngroups_max(); if (ngroups <= 0) rb_raise(rb_eArgError, "maxgroups %d shold be positive", ngroups); if (ngroups > RB_MAX_GROUPS) ngroups = RB_MAX_GROUPS; if (ngroups_max > 0 && ngroups > ngroups_max) ngroups = ngroups_max; _maxgroups = ngroups; return INT2FIX(_maxgroups); }
.pid ⇒ Integer (readonly, mod_func)
Alias for $$.
Returns the (real) user ID of this process.
Process.uid #=> 501
# File 'process.c', line 6028
static VALUE proc_getuid(VALUE obj) { rb_uid_t uid = getuid(); return UIDT2NUM(uid); }
.uid=(user) ⇒ Numeric (rw, mod_func)
Sets the (user) user ID for this process. Not available on all platforms.
# File 'process.c', line 6045
static VALUE proc_setuid(VALUE obj, VALUE id) { rb_uid_t uid; check_uid_switch(); uid = OBJ2UID(id); #if defined(HAVE_SETRESUID) if (setresuid(uid, -1, -1) < 0) rb_sys_fail(0); #elif defined HAVE_SETREUID if (setreuid(uid, -1) < 0) rb_sys_fail(0); #elif defined HAVE_SETRUID if (setruid(uid) < 0) rb_sys_fail(0); #elif defined HAVE_SETUID { if (geteuid() == uid) { if (setuid(uid) < 0) rb_sys_fail(0); } else { rb_notimplement(); } } #endif return id; }
Class Method Details
.abort
.abort([msg])
Alias for Kernel.abort.
.argv0 ⇒ frozen_string
(mod_func)
Returns the name of the script being executed. The value is not affected by assigning a new value to $0.
This method first appeared in Ruby 2.1 to serve as a global variable free means to get the script name.
# File 'ruby.c', line 2177
static VALUE proc_argv0(VALUE process) { return rb_orig_progname; }
.clock_getres(clock_id [, unit]) ⇒ Numeric (mod_func)
Returns the time resolution returned by POSIX clock_getres() function.
clock_id
specifies a kind of clock. See the document of .clock_gettime for details.
clock_id
can be a symbol as .clock_gettime. However the result may not be accurate. For example, Process.clock_getres(:GETTIMEOFDAY_BASED_CLOCK_REALTIME) returns 1.0e-06 which means 1 microsecond, but actual resolution can be more coarse.
If the given clock_id
is not supported, Errno::EINVAL
is raised.
unit
specifies a type of the return value. clock_getres
accepts unit
as .clock_gettime. The default value, :float_second
, is also same as .clock_gettime.
clock_getres
also accepts :hertz
as unit
. :hertz
means a the reciprocal of :float_second
.
:hertz
can be used to obtain the exact value of the clock ticks per second for times() function and CLOCKS_PER_SEC for clock() function.
Process.clock_getres(:TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID, :hertz
) returns the clock ticks per second.
Process.clock_getres(:CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID, :hertz
) returns CLOCKS_PER_SEC.
p Process.clock_getres(Process::CLOCK_MONOTONIC)
#=> 1.0e-09
# File 'process.c', line 8188
VALUE rb_clock_getres(int argc, VALUE *argv) { struct timetick tt; timetick_int_t numerators[2]; timetick_int_t denominators[2]; int num_numerators = 0; int num_denominators = 0; VALUE unit = (rb_check_arity(argc, 1, 2) == 2) ? argv[1] : Qnil; VALUE clk_id = argv[0]; if (SYMBOL_P(clk_id)) { #ifdef RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME if (clk_id == RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME) { tt.giga_count = 0; tt.count = 1000; denominators[num_denominators++] = 1000000000; goto success; } #endif #ifdef RUBY_TIME_BASED_CLOCK_REALTIME if (clk_id == RUBY_TIME_BASED_CLOCK_REALTIME) { tt.giga_count = 1; tt.count = 0; denominators[num_denominators++] = 1000000000; goto success; } #endif #ifdef RUBY_TIMES_BASED_CLOCK_MONOTONIC if (clk_id == RUBY_TIMES_BASED_CLOCK_MONOTONIC) { tt.count = 1; tt.giga_count = 0; denominators[num_denominators++] = get_clk_tck(); goto success; } #endif #ifdef RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID if (clk_id == RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) { tt.giga_count = 0; tt.count = 1000; denominators[num_denominators++] = 1000000000; goto success; } #endif #ifdef RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID if (clk_id == RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID) { tt.count = 1; tt.giga_count = 0; denominators[num_denominators++] = get_clk_tck(); goto success; } #endif #ifdef RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID if (clk_id == RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) { tt.count = 1; tt.giga_count = 0; denominators[num_denominators++] = CLOCKS_PER_SEC; goto success; } #endif #ifdef RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC if (clk_id == RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC) { const mach_timebase_info_data_t *info = get_mach_timebase_info(); tt.count = 1; tt.giga_count = 0; numerators[num_numerators++] = info->numer; denominators[num_denominators++] = info->denom; denominators[num_denominators++] = 1000000000; goto success; } #endif } else { #if defined(HAVE_CLOCK_GETRES) struct timespec ts; clockid_t c = NUM2CLOCKID(clk_id); int ret = clock_getres(c, &ts); if (ret == -1) rb_sys_fail("clock_getres"); tt.count = (int32_t)ts.tv_nsec; tt.giga_count = ts.tv_sec; denominators[num_denominators++] = 1000000000; goto success; #endif } /* EINVAL emulates clock_getres behavior when clock_id is invalid. */ rb_syserr_fail(EINVAL, 0); success: if (unit == ID2SYM(id_hertz)) { return timetick2dblnum_reciprocal(&tt, numerators, num_numerators, denominators, num_denominators); } else { return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit); } }
.clock_gettime(clock_id [, unit]) ⇒ Numeric (mod_func)
Returns a time returned by POSIX clock_gettime() function.
p Process.clock_gettime(Process::CLOCK_MONOTONIC)
#=> 896053.968060096
clock_id
specifies a kind of clock. It is specified as a constant which begins with Process::CLOCK_
such as CLOCK_REALTIME and CLOCK_MONOTONIC.
The supported constants depends on OS and version. Ruby provides following types of clock_id
if available.
- CLOCK_REALTIME
-
SUSv2 to 4, Linux 2.5.63, FreeBSD 3.0, NetBSD 2.0, OpenBSD 2.1, macOS 10.12
- CLOCK_MONOTONIC
-
SUSv3 to 4, Linux 2.5.63, FreeBSD 3.0, NetBSD 2.0, OpenBSD 3.4, macOS 10.12
- CLOCK_PROCESS_CPUTIME_ID
-
SUSv3 to 4, Linux 2.5.63, OpenBSD 5.4, macOS 10.12
- CLOCK_THREAD_CPUTIME_ID
-
SUSv3 to 4, Linux 2.5.63, FreeBSD 7.1, OpenBSD 5.4, macOS 10.12
- CLOCK_VIRTUAL
-
FreeBSD 3.0, OpenBSD 2.1
- CLOCK_PROF
-
FreeBSD 3.0, OpenBSD 2.1
- CLOCK_REALTIME_FAST
-
FreeBSD 8.1
- CLOCK_REALTIME_PRECISE
-
FreeBSD 8.1
- CLOCK_REALTIME_COARSE
-
Linux 2.6.32
- CLOCK_REALTIME_ALARM
-
Linux 3.0
- CLOCK_MONOTONIC_FAST
-
FreeBSD 8.1
- CLOCK_MONOTONIC_PRECISE
-
FreeBSD 8.1
- CLOCK_MONOTONIC_COARSE
-
Linux 2.6.32
- CLOCK_MONOTONIC_RAW
-
Linux 2.6.28, macOS 10.12
- CLOCK_MONOTONIC_RAW_APPROX
-
macOS 10.12
- CLOCK_BOOTTIME
-
Linux 2.6.39
- CLOCK_BOOTTIME_ALARM
-
Linux 3.0
- CLOCK_UPTIME
-
FreeBSD 7.0, OpenBSD 5.5
- CLOCK_UPTIME_FAST
-
FreeBSD 8.1
- CLOCK_UPTIME_RAW
-
macOS 10.12
- CLOCK_UPTIME_RAW_APPROX
-
macOS 10.12
- CLOCK_UPTIME_PRECISE
-
FreeBSD 8.1
- CLOCK_SECOND
-
FreeBSD 8.1
Note that SUS stands for Single Unix Specification. SUS contains POSIX and clock_gettime is defined in the POSIX part. SUS defines CLOCK_REALTIME mandatory but CLOCK_MONOTONIC, CLOCK_PROCESS_CPUTIME_ID and CLOCK_THREAD_CPUTIME_ID are optional.
Also, several symbols are accepted as clock_id
. There are emulations for clock_gettime().
For example, CLOCK_REALTIME is defined as :GETTIMEOFDAY_BASED_CLOCK_REALTIME
when clock_gettime() is not available.
Emulations for CLOCK_REALTIME:
- :GETTIMEOFDAY_BASED_CLOCK_REALTIME
-
Use gettimeofday() defined by SUS. (SUSv4 obsoleted it, though.) The resolution is 1 microsecond.
- :TIME_BASED_CLOCK_REALTIME
-
Use time() defined by ISO C. The resolution is 1 second.
Emulations for CLOCK_MONOTONIC:
- :MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
-
Use mach_absolute_time(), available on Darwin. The resolution is CPU dependent.
- :TIMES_BASED_CLOCK_MONOTONIC
-
Use the result value of times() defined by POSIX. POSIX defines it as “times() shall return the elapsed real time, in clock ticks, since an arbitrary point in the past (for example, system start-up time)”. For example, GNU/Linux returns a value based on jiffies and it is monotonic. However, 4.4BSD uses gettimeofday() and it is not monotonic. (FreeBSD uses clock_gettime(CLOCK_MONOTONIC) instead, though.) The resolution is the clock tick. “getconf CLK_TCK” command shows the clock ticks per second. (The clock ticks per second is defined by HZ macro in older systems.) If it is 100 and clock_t is 32 bits integer type, the resolution is 10 millisecond and cannot represent over 497 days.
Emulations for CLOCK_PROCESS_CPUTIME_ID:
- :GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID
-
Use getrusage() defined by SUS. getrusage() is used with RUSAGE_SELF to obtain the time only for the calling process (excluding the time for child processes). The result is addition of user time (ru_utime) and system time (ru_stime). The resolution is 1 microsecond.
- :TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID
-
Use times() defined by POSIX. The result is addition of user time (tms_utime) and system time (tms_stime). tms_cutime and tms_cstime are ignored to exclude the time for child processes. The resolution is the clock tick. “getconf CLK_TCK” command shows the clock ticks per second. (The clock ticks per second is defined by HZ macro in older systems.) If it is 100, the resolution is 10 millisecond.
- :CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID
-
Use clock() defined by ISO C. The resolution is 1/CLOCKS_PER_SEC. CLOCKS_PER_SEC is the C-level macro defined by time.h. SUS defines CLOCKS_PER_SEC is 1000000. Non-Unix systems may define it a different value, though. If CLOCKS_PER_SEC is 1000000 as SUS, the resolution is 1 microsecond. If CLOCKS_PER_SEC is 1000000 and clock_t is 32 bits integer type, it cannot represent over 72 minutes.
If the given clock_id
is not supported, Errno::EINVAL
is raised.
unit
specifies a type of the return value.
- :float_second
-
number of seconds as a float (default)
- :float_millisecond
-
number of milliseconds as a float
- :float_microsecond
-
number of microseconds as a float
- :second
-
number of seconds as an integer
- :millisecond
-
number of milliseconds as an integer
- :microsecond
-
number of microseconds as an integer
- :nanosecond
-
number of nanoseconds as an integer
The underlying function, clock_gettime(), returns a number of nanoseconds. ::Float
object (IEEE 754 double) is not enough to represent the return value for CLOCK_REALTIME. If the exact nanoseconds value is required, use :nanoseconds
as the unit
.
The origin (zero) of the returned value varies. For example, system start up time, process start up time, the Epoch, etc.
The origin in CLOCK_REALTIME is defined as the Epoch (1970-01-01 00:00:00 UTC). But some systems count leap seconds and others doesn’t. So the result can be interpreted differently across systems. Time.now is recommended over CLOCK_REALTIME.
# File 'process.c', line 7991
VALUE rb_clock_gettime(int argc, VALUE *argv) { int ret; struct timetick tt; timetick_int_t numerators[2]; timetick_int_t denominators[2]; int num_numerators = 0; int num_denominators = 0; VALUE unit = (rb_check_arity(argc, 1, 2) == 2) ? argv[1] : Qnil; VALUE clk_id = argv[0]; if (SYMBOL_P(clk_id)) { /* * Non-clock_gettime clocks are provided by symbol clk_id. */ #ifdef HAVE_GETTIMEOFDAY /* * GETTIMEOFDAY_BASED_CLOCK_REALTIME is used for * CLOCK_REALTIME if clock_gettime is not available. */ #define RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME ID2SYM(id_GETTIMEOFDAY_BASED_CLOCK_REALTIME) if (clk_id == RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME) { struct timeval tv; ret = gettimeofday(&tv, 0); if (ret != 0) rb_sys_fail("gettimeofday"); tt.giga_count = tv.tv_sec; tt.count = (int32_t)tv.tv_usec * 1000; denominators[num_denominators++] = 1000000000; goto success; } #endif #define RUBY_TIME_BASED_CLOCK_REALTIME ID2SYM(id_TIME_BASED_CLOCK_REALTIME) if (clk_id == RUBY_TIME_BASED_CLOCK_REALTIME) { time_t t; t = time(NULL); if (t == (time_t)-1) rb_sys_fail("time"); tt.giga_count = t; tt.count = 0; denominators[num_denominators++] = 1000000000; goto success; } #ifdef HAVE_TIMES #define RUBY_TIMES_BASED_CLOCK_MONOTONIC \ ID2SYM(id_TIMES_BASED_CLOCK_MONOTONIC) if (clk_id == RUBY_TIMES_BASED_CLOCK_MONOTONIC) { struct tms buf; clock_t c; unsigned_clock_t uc; c = times(&buf); if (c == (clock_t)-1) rb_sys_fail("times"); uc = (unsigned_clock_t)c; tt.count = (int32_t)(uc % 1000000000); tt.giga_count = (uc / 1000000000); denominators[num_denominators++] = get_clk_tck(); goto success; } #endif #ifdef RUSAGE_SELF #define RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID \ ID2SYM(id_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) if (clk_id == RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID) { struct rusage usage; int32_t usec; ret = getrusage(RUSAGE_SELF, &usage); if (ret != 0) rb_sys_fail("getrusage"); tt.giga_count = usage.ru_utime.tv_sec + usage.ru_stime.tv_sec; usec = (int32_t)(usage.ru_utime.tv_usec + usage.ru_stime.tv_usec); if (1000000 <= usec) { tt.giga_count++; usec -= 1000000; } tt.count = usec * 1000; denominators[num_denominators++] = 1000000000; goto success; } #endif #ifdef HAVE_TIMES #define RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID \ ID2SYM(id_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID) if (clk_id == RUBY_TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID) { struct tms buf; unsigned_clock_t utime, stime; if (times(&buf) == (clock_t)-1) rb_sys_fail("times"); utime = (unsigned_clock_t)buf.tms_utime; stime = (unsigned_clock_t)buf.tms_stime; tt.count = (int32_t)((utime % 1000000000) + (stime % 1000000000)); tt.giga_count = (utime / 1000000000) + (stime / 1000000000); if (1000000000 <= tt.count) { tt.count -= 1000000000; tt.giga_count++; } denominators[num_denominators++] = get_clk_tck(); goto success; } #endif #define RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID \ ID2SYM(id_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) if (clk_id == RUBY_CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID) { clock_t c; unsigned_clock_t uc; errno = 0; c = clock(); if (c == (clock_t)-1) rb_sys_fail("clock"); uc = (unsigned_clock_t)c; tt.count = (int32_t)(uc % 1000000000); tt.giga_count = uc / 1000000000; denominators[num_denominators++] = CLOCKS_PER_SEC; goto success; } #ifdef __APPLE__ #define RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC ID2SYM(id_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC) if (clk_id == RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC) { const mach_timebase_info_data_t *info = get_mach_timebase_info(); uint64_t t = mach_absolute_time(); tt.count = (int32_t)(t % 1000000000); tt.giga_count = t / 1000000000; numerators[num_numerators++] = info->numer; denominators[num_denominators++] = info->denom; denominators[num_denominators++] = 1000000000; goto success; } #endif } else { #if defined(HAVE_CLOCK_GETTIME) struct timespec ts; clockid_t c; c = NUM2CLOCKID(clk_id); ret = clock_gettime(c, &ts); if (ret == -1) rb_sys_fail("clock_gettime"); tt.count = (int32_t)ts.tv_nsec; tt.giga_count = ts.tv_sec; denominators[num_denominators++] = 1000000000; goto success; #endif } /* EINVAL emulates clock_gettime behavior when clock_id is invalid. */ rb_syserr_fail(EINVAL, 0); success: return make_clock_result(&tt, numerators, num_numerators, denominators, num_denominators, unit); }
.daemon ⇒ 0
(mod_func)
.daemon(nochdir = nil, noclose = nil) ⇒ 0
0
(mod_func)
.daemon(nochdir = nil, noclose = nil) ⇒ 0
Detach the process from controlling terminal and run in the background as system daemon. Unless the argument nochdir is true (i.e. non false), it changes the current working directory to the root (“/”). Unless the argument noclose is true, daemon() will redirect standard input, standard output and standard error to /dev/null. Return zero on success, or raise one of Errno.*
.
# File 'process.c', line 6733
static VALUE proc_daemon(int argc, VALUE *argv) { int n, nochdir = FALSE, noclose = FALSE; switch (rb_check_arity(argc, 0, 2)) { case 2: noclose = RTEST(argv[1]); case 1: nochdir = RTEST(argv[0]); } prefork(); n = rb_daemon(nochdir, noclose); if (n < 0) rb_sys_fail("daemon"); return INT2FIX(n); }
.detach(pid) ⇒ Thread (mod_func)
Some operating systems retain the status of terminated child processes until the parent collects that status (normally using some variant of wait()
). If the parent never collects this status, the child stays around as a zombie process. detach
prevents this by setting up a separate Ruby thread whose sole job is to reap the status of the process pid when it terminates. Use detach
only when you do not intend to explicitly wait for the child to terminate.
The waiting thread returns the exit status of the detached process when it terminates, so you can use Thread#join to know the result. If specified pid is not a valid child process ID, the thread returns nil
immediately.
The waiting thread has .pid method which returns the pid.
In this first example, we don’t reap the first child process, so it appears as a zombie in the process status display.
p1 = fork { sleep 0.1 }
p2 = fork { sleep 0.2 }
Process.waitpid(p2)
sleep 2
system("ps -ho pid,state -p #{p1}")
produces:
27389 Z
In the next example, detach
is used to reap the child automatically.
p1 = fork { sleep 0.1 }
p2 = fork { sleep 0.2 }
Process.detach(p1)
Process.waitpid(p2)
sleep 2
system("ps -ho pid,state -p #{p1}")
(produces no output)
# File 'process.c', line 1481
static VALUE proc_detach(VALUE obj, VALUE pid) { return rb_detach_process(NUM2PIDT(pid)); }
Returns the effective group ID for this process. Not available on all platforms.
Process.egid #=> 500
# File 'process.c', line 7125
static VALUE proc_getegid(VALUE obj) { rb_gid_t egid = getegid(); return GIDT2NUM(egid); }
.egid=(integer) ⇒ Integer (mod_func)
Sets the effective group ID for this process. Not available on all platforms.
# File 'process.c', line 7142
static VALUE proc_setegid(VALUE obj, VALUE egid) { #if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID) rb_gid_t gid; #endif check_gid_switch(); #if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID) gid = OBJ2GID(egid); #endif #if defined(HAVE_SETRESGID) if (setresgid(-1, gid, -1) < 0) rb_sys_fail(0); #elif defined HAVE_SETREGID if (setregid(-1, gid) < 0) rb_sys_fail(0); #elif defined HAVE_SETEGID if (setegid(gid) < 0) rb_sys_fail(0); #elif defined HAVE_SETGID if (gid == getgid()) { if (setgid(gid) < 0) rb_sys_fail(0); } else { rb_notimplement(); } #else rb_notimplement(); #endif return egid; }
.exec([env,] command... [,options])
Alias for Kernel.exec.
.exit(status = true)
.exit(status = true)
Alias for Kernel.exit.
.exit!(status = false)
Alias for Kernel.exit!.
Alias for Kernel.fork.
.getpgid(pid) ⇒ Integer (mod_func)
Returns the process group ID for the given process id. Not available on all platforms.
Process.getpgid(Process.ppid()) #=> 25527
# File 'process.c', line 4947
static VALUE proc_getpgid(VALUE obj, VALUE pid) { rb_pid_t i; i = getpgid(NUM2PIDT(pid)); if (i < 0) rb_sys_fail(0); return PIDT2NUM(i); }
.getpgrp ⇒ Integer (mod_func)
Returns the process group ID for this process. Not available on all platforms.
Process.getpgid(0) #=> 25527
Process.getpgrp #=> 25527
# File 'process.c', line 4888
static VALUE proc_getpgrp(void) { rb_pid_t pgrp; #if defined(HAVE_GETPGRP) && defined(GETPGRP_VOID) pgrp = getpgrp(); if (pgrp < 0) rb_sys_fail(0); return PIDT2NUM(pgrp); #else /* defined(HAVE_GETPGID) */ pgrp = getpgid(0); if (pgrp < 0) rb_sys_fail(0); return PIDT2NUM(pgrp); #endif }
.getpriority(kind, integer) ⇒ Integer (mod_func)
Gets the scheduling priority for specified process, process group, or user. kind indicates the kind of entity to find: one of PRIO_PGRP, PRIO_USER, or PRIO_PROCESS. integer is an id indicating the particular process, process group, or user (an id of 0 means current). Lower priorities are more favorable for scheduling. Not available on all platforms.
Process.getpriority(Process::PRIO_USER, 0) #=> 19
Process.getpriority(Process::PRIO_PROCESS, 0) #=> 19
# File 'process.c', line 5093
static VALUE proc_getpriority(VALUE obj, VALUE which, VALUE who) { int prio, iwhich, iwho; iwhich = NUM2INT(which); iwho = NUM2INT(who); errno = 0; prio = getpriority(iwhich, iwho); if (errno) rb_sys_fail(0); return INT2FIX(prio); }
.getrlimit(resource) ⇒ Array, max_limit
(mod_func)
Gets the resource limit of the process. cur_limit means current (soft) limit and max_limit means maximum (hard) limit.
resource indicates the kind of resource to limit. It is specified as a symbol such as :CORE
, a string such as "CORE"
or a constant such as RLIMIT_CORE. See .setrlimit for details.
cur_limit and max_limit may be RLIM_INFINITY, RLIM_SAVED_MAX or RLIM_SAVED_CUR. See .setrlimit and the system getrlimit(2) manual for details.
# File 'process.c', line 5384
static VALUE proc_getrlimit(VALUE obj, VALUE resource) { struct rlimit rlim; if (getrlimit(rlimit_resource_type(resource), &rlim) < 0) { rb_sys_fail("getrlimit"); } return rb_assoc_new(RLIM2NUM(rlim.rlim_cur), RLIM2NUM(rlim.rlim_max)); }
Returns the session ID for the given process id. If not given, return current process sid. Not available on all platforms.
Process.getsid() #=> 27422
Process.getsid(0) #=> 27422
Process.getsid(Process.pid()) #=> 27422
# File 'process.c', line 4999
static VALUE proc_getsid(int argc, VALUE *argv) { rb_pid_t sid; rb_pid_t pid = 0; if (rb_check_arity(argc, 0, 1) == 1 && !NIL_P(argv[0])) pid = NUM2PIDT(argv[0]); sid = getsid(pid); if (sid < 0) rb_sys_fail(0); return PIDT2NUM(sid); }
.initgroups(username, gid) ⇒ Array (mod_func)
Initializes the supplemental group access list by reading the system group database and using all groups of which the given user is a member. The group with the specified gid is also added to the list. Returns the resulting ::Array
of the gids of all the groups in the supplementary group access list. Not available on all platforms.
Process.groups #=> [0, 1, 2, 3, 4, 6, 10, 11, 20, 26, 27]
Process.initgroups( "mgranger", 30 ) #=> [30, 6, 10, 11]
Process.groups #=> [30, 6, 10, 11]
# File 'process.c', line 6652
static VALUE proc_initgroups(VALUE obj, VALUE uname, VALUE base_grp) { if (initgroups(StringValueCStr(uname), OBJ2GID(base_grp)) != 0) { rb_sys_fail(0); } return proc_getgroups(obj); }
.kill(signal, pid, ...) ⇒ Integer (mod_func)
Sends the given signal to the specified process id(s) if pid is positive. If pid is zero signal is sent to all processes whose group ID is equal to the group ID of the process. signal may be an integer signal number or a POSIX signal name (either with or without a SIG
prefix). If signal is negative (or starts with a minus sign), kills process groups instead of processes. Not all signals are available on all platforms. The keys and values of Signal.list are known signal names and numbers, respectively.
pid = fork do
Signal.trap("HUP") { puts "Ouch!"; exit }
# ... do some work ...
end
# ...
Process.kill("HUP", pid)
Process.wait
produces:
Ouch!
If signal is an integer but wrong for signal, Errno::EINVAL
or ::RangeError
will be raised. Otherwise unless signal is a ::String
or a ::Symbol
, and a known signal name, ::ArgumentError
will be raised.
Also, Errno::ESRCH
or ::RangeError
for invalid pid, Errno::EPERM
when failed because of no privilege, will be raised. In these cases, signals may have been sent to preceding processes.
# File 'signal.c', line 453
VALUE rb_f_kill(int argc, const VALUE *argv) { #ifndef HAVE_KILLPG #define killpg(pg, sig) kill(-(pg), (sig)) #endif int sig; int i; VALUE str; rb_check_arity(argc, 2, UNLIMITED_ARGUMENTS); if (FIXNUM_P(argv[0])) { sig = FIX2INT(argv[0]); } else { str = argv[0]; sig = signm2signo(&str, TRUE, FALSE, NULL); } if (argc <= 1) return INT2FIX(0); if (sig < 0) { sig = -sig; for (i=1; i<argc; i++) { if (killpg(NUM2PIDT(argv[i]), sig) < 0) rb_sys_fail(0); } } else { const rb_pid_t self = (GET_THREAD() == GET_VM()->main_thread) ? getpid() : -1; int wakeup = 0; for (i=1; i<argc; i++) { rb_pid_t pid = NUM2PIDT(argv[i]); if ((sig != 0) && (self != -1) && (pid == self)) { int t; /* * When target pid is self, many caller assume signal will be * delivered immediately and synchronously. */ switch (sig) { case SIGSEGV: #ifdef SIGBUS case SIGBUS: #endif #ifdef SIGKILL case SIGKILL: #endif #ifdef SIGILL case SIGILL: #endif #ifdef SIGFPE case SIGFPE: #endif #ifdef SIGSTOP case SIGSTOP: #endif kill(pid, sig); break; default: t = signal_ignored(sig); if (t) { if (t < 0 && kill(pid, sig)) rb_sys_fail(0); break; } signal_enque(sig); wakeup = 1; } } else if (kill(pid, sig) < 0) { rb_sys_fail(0); } } if (wakeup) { rb_threadptr_check_signal(GET_VM()->main_thread); } } rb_thread_execute_interrupts(rb_thread_current()); return INT2FIX(i-1); }
.last_status ⇒ Process
?
Returns the status of the last executed child process in the current thread.
Process.wait Process.spawn("ruby", "-e", "exit 13")
Process.last_status #=> #<Process::Status: pid 4825 exit 13>
If no child process has ever been executed in the current thread, this returns nil
.
Process.last_status #=> nil
# File 'process.c', line 543
static VALUE proc_s_last_status(VALUE mod) { return rb_last_status_get(); }
.ppid ⇒ Integer (mod_func)
# File 'process.c', line 483
static VALUE get_ppid(void) { return PIDT2NUM(getppid()); }
.setpgid(pid, integer) ⇒ 0
(mod_func)
Sets the process group ID of pid (0 indicates this process) to integer. Not available on all platforms.
# File 'process.c', line 4970
static VALUE proc_setpgid(VALUE obj, VALUE pid, VALUE pgrp) { rb_pid_t ipid, ipgrp; ipid = NUM2PIDT(pid); ipgrp = NUM2PIDT(pgrp); if (setpgid(ipid, ipgrp) < 0) rb_sys_fail(0); return INT2FIX(0); }
.setpgrp ⇒ 0
(mod_func)
Equivalent to setpgid(0,0)
. Not available on all platforms.
# File 'process.c', line 4917
static VALUE proc_setpgrp(void) { /* check for posix setpgid() first; this matches the posix */ /* getpgrp() above. It appears that configure will set SETPGRP_VOID */ /* even though setpgrp(0,0) would be preferred. The posix call avoids */ /* this confusion. */ #ifdef HAVE_SETPGID if (setpgid(0,0) < 0) rb_sys_fail(0); #elif defined(HAVE_SETPGRP) && defined(SETPGRP_VOID) if (setpgrp() < 0) rb_sys_fail(0); #endif return INT2FIX(0); }
.setpriority(kind, integer, priority) ⇒ 0
(mod_func)
See #getpriority.
Process.setpriority(Process::PRIO_USER, 0, 19) #=> 0
Process.setpriority(Process::PRIO_PROCESS, 0, 19) #=> 0
Process.getpriority(Process::PRIO_USER, 0) #=> 19
Process.getpriority(Process::PRIO_PROCESS, 0) #=> 19
# File 'process.c', line 5124
static VALUE proc_setpriority(VALUE obj, VALUE which, VALUE who, VALUE prio) { int iwhich, iwho, iprio; iwhich = NUM2INT(which); iwho = NUM2INT(who); iprio = NUM2INT(prio); if (setpriority(iwhich, iwho, iprio) < 0) rb_sys_fail(0); return INT2FIX(0); }
.setproctitle(string) ⇒ String (mod_func)
Sets the process title that appears on the ps(1) command. Not necessarily effective on all platforms. No exception will be raised regardless of the result, nor will ::NotImplementedError
be raised even if the platform does not support the feature.
Calling this method does not affect the value of $0.
Process.setproctitle('myapp: worker #%d' % worker_id)
This method first appeared in Ruby 2.1 to serve as a global variable free means to change the process title.
# File 'ruby.c', line 2202
static VALUE proc_setproctitle(VALUE process, VALUE title) { return ruby_setproctitle(title); }
.setrlimit(resource, cur_limit, max_limit) ⇒ nil
(mod_func)
.setrlimit(resource, cur_limit) ⇒ nil
nil
(mod_func)
.setrlimit(resource, cur_limit) ⇒ nil
Sets the resource limit of the process. cur_limit means current (soft) limit and max_limit means maximum (hard) limit.
If max_limit is not given, cur_limit is used.
resource indicates the kind of resource to limit. It should be a symbol such as :CORE
, a string such as "CORE"
or a constant such as RLIMIT_CORE. The available resources are OS dependent. Ruby may support following resources.
- AS
-
total available memory (bytes) (SUSv3, NetBSD, FreeBSD, OpenBSD but 4.4BSD-Lite)
- CORE
-
core size (bytes) (SUSv3)
- CPU
-
CPU time (seconds) (SUSv3)
- DATA
-
data segment (bytes) (SUSv3)
- FSIZE
-
file size (bytes) (SUSv3)
- MEMLOCK
-
total size for mlock(2) (bytes) (4.4BSD, GNU/Linux)
- MSGQUEUE
-
allocation for POSIX message queues (bytes) (GNU/Linux)
- NICE
-
ceiling on process’s nice(2) value (number) (GNU/Linux)
- NOFILE
-
file descriptors (number) (SUSv3)
- NPROC
-
number of processes for the user (number) (4.4BSD, GNU/Linux)
- RSS
-
resident memory size (bytes) (4.2BSD, GNU/Linux)
- RTPRIO
-
ceiling on the process’s real-time priority (number) (GNU/Linux)
- RTTIME
-
CPU time for real-time process (us) (GNU/Linux)
- SBSIZE
-
all socket buffers (bytes) (NetBSD, FreeBSD)
- SIGPENDING
-
number of queued signals allowed (signals) (GNU/Linux)
- STACK
-
stack size (bytes) (SUSv3)
cur_limit and max_limit may be :INFINITY
, "INFINITY"
or RLIM_INFINITY, which means that the resource is not limited. They may be RLIM_SAVED_MAX, RLIM_SAVED_CUR and corresponding symbols and strings too. See system setrlimit(2) manual for details.
The following example raises the soft limit of core size to the hard limit to try to make core dump possible.
Process.setrlimit(:CORE, Process.getrlimit(:CORE)[1])
# File 'process.c', line 5450
static VALUE proc_setrlimit(int argc, VALUE *argv, VALUE obj) { VALUE resource, rlim_cur, rlim_max; struct rlimit rlim; rb_check_arity(argc, 2, 3); resource = argv[0]; rlim_cur = argv[1]; if (argc < 3 || NIL_P(rlim_max = argv[2])) rlim_max = rlim_cur; rlim.rlim_cur = rlimit_resource_value(rlim_cur); rlim.rlim_max = rlimit_resource_value(rlim_max); if (setrlimit(rlimit_resource_type(resource), &rlim) < 0) { rb_sys_fail("setrlimit"); } return Qnil; }
.setsid ⇒ Integer (mod_func)
Establishes this process as a new session and process group leader, with no controlling tty. Returns the session id. Not available on all platforms.
Process.setsid #=> 27422
# File 'process.c', line 5033
static VALUE proc_setsid(void) { rb_pid_t pid; pid = setsid(); if (pid < 0) rb_sys_fail(0); return PIDT2NUM(pid); }
.spawn([env,] command... [,options]) ⇒ pid
Alias for Kernel.spawn.
.times ⇒ Process
(mod_func)
Returns a Tms
structure (see Process::Tms
) that contains user and system CPU times for this process, and also for children processes.
t = Process.times
[ t.utime, t.stime, t.cutime, t.cstime ] #=> [0.0, 0.02, 0.00, 0.00]
# File 'process.c', line 7620
VALUE rb_proc_times(VALUE obj) { VALUE utime, stime, cutime, cstime, ret; #if defined(RUSAGE_SELF) && defined(RUSAGE_CHILDREN) struct rusage usage_s, usage_c; if (getrusage(RUSAGE_SELF, &usage_s) != 0 || getrusage(RUSAGE_CHILDREN, &usage_c) != 0) rb_sys_fail("getrusage"); utime = DBL2NUM((double)usage_s.ru_utime.tv_sec + (double)usage_s.ru_utime.tv_usec/1e6); stime = DBL2NUM((double)usage_s.ru_stime.tv_sec + (double)usage_s.ru_stime.tv_usec/1e6); cutime = DBL2NUM((double)usage_c.ru_utime.tv_sec + (double)usage_c.ru_utime.tv_usec/1e6); cstime = DBL2NUM((double)usage_c.ru_stime.tv_sec + (double)usage_c.ru_stime.tv_usec/1e6); #else const double hertz = (double)get_clk_tck(); struct tms buf; times(&buf); utime = DBL2NUM(buf.tms_utime / hertz); stime = DBL2NUM(buf.tms_stime / hertz); cutime = DBL2NUM(buf.tms_cutime / hertz); cstime = DBL2NUM(buf.tms_cstime / hertz); #endif ret = rb_struct_new(rb_cProcessTms, utime, stime, cutime, cstime); RB_GC_GUARD(utime); RB_GC_GUARD(stime); RB_GC_GUARD(cutime); RB_GC_GUARD(cstime); return ret; }
.wait ⇒ Integer (mod_func)
.wait(pid = -1, flags = 0) ⇒ Integer
.waitpid(pid = -1, flags = 0) ⇒ Integer
Also known as: .waitpid
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 WNOHANG (do not block if no child available) or 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 ::SystemCallError
if there are no child processes. Not available on all platforms.
include Process
fork { exit 99 } #=> 27429
wait #=> 27429
$?.exitstatus #=> 99
pid = fork { sleep 3 } #=> 27440
Time.now #=> 2008-03-08 19:56:16 +0900
waitpid(pid, Process::WNOHANG) #=> nil
Time.now #=> 2008-03-08 19:56:16 +0900
waitpid(pid, 0) #=> 27440
Time.now #=> 2008-03-08 19:56:19 +0900
# File 'process.c', line 1309
static VALUE proc_wait(int argc, VALUE *argv) { rb_pid_t pid; int flags, status; flags = 0; if (rb_check_arity(argc, 0, 2) == 0) { pid = -1; } else { VALUE vflags; pid = NUM2PIDT(argv[0]); if (argc == 2 && !NIL_P(vflags = argv[1])) { flags = NUM2UINT(vflags); } } if ((pid = rb_waitpid(pid, &status, flags)) < 0) rb_sys_fail(0); if (pid == 0) { rb_last_status_clear(); return Qnil; } return PIDT2NUM(pid); }
.wait2(pid = -1, flags = 0) ⇒ Array, status
(mod_func)
.waitpid2(pid = -1, flags = 0) ⇒ Array, status
Also known as: .waitpid2
status
(mod_func)
.waitpid2(pid = -1, flags = 0) ⇒ Array, status
Waits for a child process to exit (see .waitpid for exact semantics) and returns an array containing the process id and the exit status (a ::Process::Status
object) of that child. Raises a ::SystemCallError
if there are no child processes.
Process.fork { exit 99 } #=> 27437
pid, status = Process.wait2
pid #=> 27437
status.exitstatus #=> 99
# File 'process.c', line 1352
static VALUE proc_wait2(int argc, VALUE *argv) { VALUE pid = proc_wait(argc, argv); if (NIL_P(pid)) return Qnil; return rb_assoc_new(pid, rb_last_status_get()); }
.waitall ⇒ Array, ... (mod_func)
Waits for all children, returning an array of pid/status pairs (where status is a ::Process::Status
object).
fork { sleep 0.2; exit 2 } #=> 27432
fork { sleep 0.1; exit 1 } #=> 27433
fork { exit 0 } #=> 27434
p Process.waitall
produces:
[[30982, #<Process::Status: pid 30982 exit 0>],
[30979, #<Process::Status: pid 30979 exit 1>],
[30976, #<Process::Status: pid 30976 exit 2>]]
# File 'process.c', line 1381
static VALUE proc_waitall(void) { VALUE result; rb_pid_t pid; int status; result = rb_ary_new(); rb_last_status_clear(); for (pid = -1;;) { pid = rb_waitpid(-1, &status, 0); if (pid == -1) { int e = errno; if (e == ECHILD) break; rb_syserr_fail(e, 0); } rb_ary_push(result, rb_assoc_new(PIDT2NUM(pid), rb_last_status_get())); } return result; }
Alias for .wait.
Alias for .wait2.