Module: Process
- Defined in:
- process.c,
process.c
Overview
The module contains several groups of functionality for handling OS processes:
-
Low-level property introspection and management of the current process, like Process.argv0, Process.pid;
-
Low-level introspection of other processes, like Process.getpgid, Process.getpriority;
-
Management of the current process: Process.abort, Process.exit, Process.daemon, etc. (for convenience, most of those are also available as global functions and module functions of Kernel);
-
Creation and management of child processes: Process.fork, Process.spawn, and related methods;
-
Management of low-level system clock: Process.times and Process.clock_gettime, which could be important for proper benchmarking and other elapsed time measurement tasks.
Defined Under Namespace
Modules: GID, Sys, UID Classes: Status, Waiter
Constant Summary collapse
- WNOHANG =
see Process.wait
INT2FIX(0)
- WUNTRACED =
see Process.wait
INT2FIX(0)
- PRIO_PROCESS =
see Process.setpriority
INT2FIX(PRIO_PROCESS)
- PRIO_PGRP =
see Process.setpriority
INT2FIX(PRIO_PGRP)
- PRIO_USER =
see Process.setpriority
INT2FIX(PRIO_USER)
- RLIM_SAVED_MAX =
see Process.setrlimit
v
- RLIM_INFINITY =
see Process.setrlimit
inf
- RLIM_SAVED_CUR =
see Process.setrlimit
v
- 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)
- CLOCK_REALTIME =
see Process.clock_gettime
RUBY_GETTIMEOFDAY_BASED_CLOCK_REALTIME
- CLOCK_MONOTONIC =
see Process.clock_gettime
RUBY_MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
- CLOCK_PROCESS_CPUTIME_ID =
see Process.clock_gettime
RUBY_GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID
- CLOCK_THREAD_CPUTIME_ID =
see Process.clock_gettime
CLOCKID2NUM(CLOCK_THREAD_CPUTIME_ID)
- CLOCK_VIRTUAL =
see Process.clock_gettime
CLOCKID2NUM(CLOCK_VIRTUAL)
- CLOCK_PROF =
see Process.clock_gettime
CLOCKID2NUM(CLOCK_PROF)
- CLOCK_REALTIME_FAST =
see Process.clock_gettime
CLOCKID2NUM(CLOCK_REALTIME_FAST)
- CLOCK_REALTIME_PRECISE =
see Process.clock_gettime
CLOCKID2NUM(CLOCK_REALTIME_PRECISE)
- CLOCK_REALTIME_COARSE =
see Process.clock_gettime
CLOCKID2NUM(CLOCK_REALTIME_COARSE)
- CLOCK_REALTIME_ALARM =
see Process.clock_gettime
CLOCKID2NUM(CLOCK_REALTIME_ALARM)
- CLOCK_MONOTONIC_FAST =
see Process.clock_gettime
CLOCKID2NUM(CLOCK_MONOTONIC_FAST)
- CLOCK_MONOTONIC_PRECISE =
see Process.clock_gettime
CLOCKID2NUM(CLOCK_MONOTONIC_PRECISE)
- CLOCK_MONOTONIC_RAW =
see Process.clock_gettime
CLOCKID2NUM(CLOCK_MONOTONIC_RAW)
- CLOCK_MONOTONIC_RAW_APPROX =
see Process.clock_gettime
CLOCKID2NUM(CLOCK_MONOTONIC_RAW_APPROX)
- CLOCK_MONOTONIC_COARSE =
see Process.clock_gettime
CLOCKID2NUM(CLOCK_MONOTONIC_COARSE)
- CLOCK_BOOTTIME =
see Process.clock_gettime
CLOCKID2NUM(CLOCK_BOOTTIME)
- CLOCK_BOOTTIME_ALARM =
see Process.clock_gettime
CLOCKID2NUM(CLOCK_BOOTTIME_ALARM)
- CLOCK_UPTIME =
see Process.clock_gettime
CLOCKID2NUM(CLOCK_UPTIME)
- CLOCK_UPTIME_FAST =
see Process.clock_gettime
CLOCKID2NUM(CLOCK_UPTIME_FAST)
- CLOCK_UPTIME_PRECISE =
see Process.clock_gettime
CLOCKID2NUM(CLOCK_UPTIME_PRECISE)
- CLOCK_UPTIME_RAW =
see Process.clock_gettime
CLOCKID2NUM(CLOCK_UPTIME_RAW)
- CLOCK_UPTIME_RAW_APPROX =
see Process.clock_gettime
CLOCKID2NUM(CLOCK_UPTIME_RAW_APPROX)
- CLOCK_SECOND =
see Process.clock_gettime
CLOCKID2NUM(CLOCK_SECOND)
- CLOCK_TAI =
see Process.clock_gettime
CLOCKID2NUM(CLOCK_TAI)
Class Method Summary collapse
-
.abort(*a, _) ⇒ Object
Terminate execution immediately, effectively by calling
Kernel.exit(false)
. -
.argv0 ⇒ Object
Returns the name of the script being executed.
-
.clock_getres(clock_id[, unit]) ⇒ Numeric
Returns an estimate of the resolution of a
clock_id
using the POSIXclock_getres()
function. -
.clock_gettime(clock_id[, unit]) ⇒ Numeric
Returns a time returned by POSIX clock_gettime() function.
-
.daemon(*args) ⇒ Object
Detach the process from controlling terminal and run in the background as system daemon.
-
.detach(pid) ⇒ Object
Some operating systems retain the status of terminated child processes until the parent collects that status (normally using some variant of
wait()
). -
.egid ⇒ Object
Returns the effective group ID for this process.
- .egid= ⇒ Object
-
.euid ⇒ Object
Returns the effective user ID for this process.
-
.euid=(user) ⇒ Object
Sets the effective user ID for this process.
-
.exec([env,][,options]) ⇒ Object
Replaces the current process by running the given external command, which can take one of the following forms:.
-
.exit(*a, _) ⇒ Object
Initiates the termination of the Ruby script by raising the SystemExit exception.
-
.exit!(status = false) ⇒ Object
Exits the process immediately.
-
.fork ⇒ Object
Creates a subprocess.
-
.getpgid(pid) ⇒ Integer
Returns the process group ID for the given process id.
-
.getpgrp ⇒ Integer
Returns the process group ID for this process.
-
.getpriority(kind, integer) ⇒ Integer
Gets the scheduling priority for specified process, process group, or user.
-
.getrlimit(resource) ⇒ Array
Gets the resource limit of the process.
-
.getsid(*args) ⇒ Object
Returns the session ID for the given process id.
-
.gid ⇒ Object
Returns the (real) group ID for this process.
-
.gid=(integer) ⇒ Integer
Sets the group ID for this process.
-
.groups ⇒ Array
Get an Array of the group IDs in the supplemental group access list for this process.
-
.groups=(array) ⇒ Array
Set the supplemental group access list to the given Array of group IDs.
-
.initgroups(username, gid) ⇒ Array
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
Sends the given signal to the specified process id(s) if pid is positive.
-
.last_status ⇒ Process::Status?
Returns the status of the last executed child process in the current thread.
-
.maxgroups ⇒ Integer
Returns the maximum number of gids allowed in the supplemental group access list.
-
.maxgroups=(integer) ⇒ Integer
Sets the maximum number of gids allowed in the supplemental group access list.
-
.pid ⇒ Integer
Returns the process id of this process.
-
.ppid ⇒ Integer
Returns the process id of the parent of this process.
-
.setpgid(pid, integer) ⇒ 0
Sets the process group ID of pid (0 indicates this process) to integer.
-
.setpgrp ⇒ 0
Equivalent to
setpgid(0,0)
. -
.setpriority(kind, integer, priority) ⇒ 0
See Process.getpriority.
-
.setproctitle(string) ⇒ String
Sets the process title that appears on the ps(1) command.
-
.setrlimit(*args) ⇒ Object
Sets the resource limit of the process.
-
.setsid ⇒ Integer
Establishes this process as a new session and process group leader, with no controlling tty.
-
.spawn(*args) ⇒ Object
spawn executes specified command and return its pid.
-
.times ⇒ aProcessTms
Returns a
Tms
structure (see Process::Tms) that contains user and system CPU times for this process, and also for children processes. -
.uid ⇒ Object
Returns the (real) user ID of this process.
-
.uid=(user) ⇒ Numeric
Sets the (user) user ID for this process.
-
.wait(*v, _) ⇒ Object
Waits for a child process to exit, returns its process id, and sets
$?
to a Process::Status object containing information on that process. -
.wait2(*args) ⇒ Object
Waits for a child process to exit (see Process::waitpid for exact semantics) and returns an array containing the process id and the exit status (a Process::Status object) of that child.
-
.waitall ⇒ Array
Waits for all children, returning an array of pid/status pairs (where status is a Process::Status object).
-
.waitpid(*v, _) ⇒ Object
Waits for a child process to exit, returns its process id, and sets
$?
to a Process::Status object containing information on that process. -
.waitpid2(*args) ⇒ Object
Waits for a child process to exit (see Process::waitpid for exact semantics) and returns an array containing the process id and the exit status (a Process::Status object) of that child.
Instance Method Summary collapse
-
#argv0 ⇒ Object
private
Returns the name of the script being executed.
-
#clock_getres(clock_id[, unit]) ⇒ Numeric
private
Returns an estimate of the resolution of a
clock_id
using the POSIXclock_getres()
function. -
#clock_gettime(clock_id[, unit]) ⇒ Numeric
private
Returns a time returned by POSIX clock_gettime() function.
-
#daemon(*args) ⇒ Object
private
Detach the process from controlling terminal and run in the background as system daemon.
-
#detach(pid) ⇒ Object
private
Some operating systems retain the status of terminated child processes until the parent collects that status (normally using some variant of
wait()
). -
#egid ⇒ Object
private
Returns the effective group ID for this process.
- #egid= ⇒ Object private
-
#euid ⇒ Object
private
Returns the effective user ID for this process.
-
#euid=(user) ⇒ Object
private
Sets the effective user ID for this process.
-
#getpgid(pid) ⇒ Integer
private
Returns the process group ID for the given process id.
-
#getpgrp ⇒ Integer
private
Returns the process group ID for this process.
-
#getpriority(kind, integer) ⇒ Integer
private
Gets the scheduling priority for specified process, process group, or user.
-
#getrlimit(resource) ⇒ Array
private
Gets the resource limit of the process.
-
#getsid(*args) ⇒ Object
private
Returns the session ID for the given process id.
-
#gid ⇒ Object
private
Returns the (real) group ID for this process.
-
#gid=(integer) ⇒ Integer
private
Sets the group ID for this process.
-
#groups ⇒ Array
private
Get an Array of the group IDs in the supplemental group access list for this process.
-
#groups=(array) ⇒ Array
private
Set the supplemental group access list to the given Array of group IDs.
-
#initgroups(username, gid) ⇒ Array
private
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
private
Sends the given signal to the specified process id(s) if pid is positive.
-
#maxgroups ⇒ Integer
private
Returns the maximum number of gids allowed in the supplemental group access list.
-
#maxgroups=(integer) ⇒ Integer
private
Sets the maximum number of gids allowed in the supplemental group access list.
-
#pid ⇒ Integer
private
Returns the process id of this process.
-
#ppid ⇒ Integer
private
Returns the process id of the parent of this process.
-
#setpgid(pid, integer) ⇒ 0
private
Sets the process group ID of pid (0 indicates this process) to integer.
-
#setpgrp ⇒ 0
private
Equivalent to
setpgid(0,0)
. -
#setpriority(kind, integer, priority) ⇒ 0
private
See Process.getpriority.
-
#setproctitle(string) ⇒ String
private
Sets the process title that appears on the ps(1) command.
-
#setrlimit(*args) ⇒ Object
private
Sets the resource limit of the process.
-
#setsid ⇒ Integer
private
Establishes this process as a new session and process group leader, with no controlling tty.
-
#times ⇒ aProcessTms
private
Returns a
Tms
structure (see Process::Tms) that contains user and system CPU times for this process, and also for children processes. -
#uid ⇒ Object
private
Returns the (real) user ID of this process.
-
#uid=(user) ⇒ Numeric
private
Sets the (user) user ID for this process.
-
#wait(*v, _) ⇒ Object
private
Waits for a child process to exit, returns its process id, and sets
$?
to a Process::Status object containing information on that process. -
#wait2(*args) ⇒ Object
private
Waits for a child process to exit (see Process::waitpid for exact semantics) and returns an array containing the process id and the exit status (a Process::Status object) of that child.
-
#waitall ⇒ Array
private
Waits for all children, returning an array of pid/status pairs (where status is a Process::Status object).
-
#waitpid(*v, _) ⇒ Object
private
Waits for a child process to exit, returns its process id, and sets
$?
to a Process::Status object containing information on that process. -
#waitpid2(*args) ⇒ Object
private
Waits for a child process to exit (see Process::waitpid for exact semantics) and returns an array containing the process id and the exit status (a Process::Status object) of that child.
Class Method Details
.abort ⇒ Object .Kernel::abort([msg]) ⇒ Object .abort([msg]) ⇒ Object
Terminate execution immediately, effectively by calling Kernel.exit(false)
. If msg is given, it is written to STDERR prior to terminating.
4531 4532 4533 4534 4535 4536 |
# File 'process.c', line 4531
static VALUE
f_abort(int c, const VALUE *a, VALUE _)
{
rb_f_abort(c, a);
UNREACHABLE_RETURN(Qnil);
}
|
.argv0 ⇒ Object
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.
2355 2356 2357 2358 2359 |
# File 'ruby.c', line 2355
static VALUE
proc_argv0(VALUE process)
{
return rb_orig_progname;
}
|
.clock_getres(clock_id[, unit]) ⇒ Numeric
Returns an estimate of the resolution of a clock_id
using the POSIX clock_getres()
function.
Note the reported resolution is often inaccurate on most platforms due to underlying bugs for this function and therefore the reported resolution often differs from the actual resolution of the clock in practice. Inaccurate reported resolutions have been observed for various clocks including CLOCK_MONOTONIC and CLOCK_MONOTONIC_RAW when using Linux, macOS, BSD or AIX platforms, when using ARM processors, or when using virtualization.
clock_id
specifies a kind of clock. See the document of Process.clock_gettime
for details. clock_id
can be a symbol as for Process.clock_gettime
.
If the given clock_id
is not supported, Errno::EINVAL is raised.
unit
specifies the type of the return value. Process.clock_getres
accepts unit
as Process.clock_gettime
. The default value, :float_second
, is also the same as Process.clock_gettime
.
Process.clock_getres
also accepts :hertz
as unit
. :hertz
means the reciprocal of :float_second
.
:hertz
can be used to obtain the exact value of the clock ticks per second for the times() function and CLOCKS_PER_SEC for the 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
8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 |
# File 'process.c', line 8450
static VALUE
rb_clock_getres(int argc, VALUE *argv, VALUE _)
{
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
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 Process::CLOCK_REALTIME and Process::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, FreeBSD 9.3, 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
- CLOCK_TAI
-
Linux 3.10
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, Process::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.
8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 |
# File 'process.c', line 8249
static VALUE
rb_clock_gettime(int argc, VALUE *argv, VALUE _)
{
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 .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::*.
6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 |
# File 'process.c', line 6988
static VALUE
proc_daemon(int argc, VALUE *argv, VALUE _)
{
int n, nochdir = FALSE, noclose = FALSE;
switch (rb_check_arity(argc, 0, 2)) {
case 2: noclose = TO_BOOL(argv[1], "noclose");
case 1: nochdir = TO_BOOL(argv[0], "nochdir");
}
prefork();
n = rb_daemon(nochdir, noclose);
if (n < 0) rb_sys_fail("daemon");
return INT2FIX(n);
}
|
.detach(pid) ⇒ Object
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. 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, Process::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)
1701 1702 1703 1704 1705 |
# File 'process.c', line 1701
static VALUE
proc_detach(VALUE obj, VALUE pid)
{
return rb_detach_process(NUM2PIDT(pid));
}
|
.egid ⇒ Integer .Process::GID.eid ⇒ Integer .Process::Sys.geteid ⇒ Integer
Returns the effective group ID for this process. Not available on all platforms.
Process.egid #=> 500
7380 7381 7382 7383 7384 7385 7386 |
# File 'process.c', line 7380
static VALUE
proc_getegid(VALUE obj)
{
rb_gid_t egid = getegid();
return GIDT2NUM(egid);
}
|
.egid= ⇒ Object
.euid ⇒ Integer .Process::UID.eid ⇒ Integer .Process::Sys.geteuid ⇒ Integer
Returns the effective user ID for this process.
Process.euid #=> 501
7256 7257 7258 7259 7260 7261 |
# File 'process.c', line 7256
static VALUE
proc_geteuid(VALUE obj)
{
rb_uid_t euid = geteuid();
return UIDT2NUM(euid);
}
|
.euid=(user) ⇒ Object
Sets the effective user ID for this process. Not available on all platforms.
7295 7296 7297 7298 7299 7300 7301 |
# File 'process.c', line 7295
static VALUE
proc_seteuid_m(VALUE mod, VALUE euid)
{
check_uid_switch();
proc_seteuid(OBJ2UID(euid));
return euid;
}
|
.exec([env,][,options]) ⇒ Object
Replaces the current process by running the given external command, which can take one of the following forms:
exec(commandline)
-
command line string which is passed to the standard shell
exec(cmdname, arg1, ...)
-
command name and one or more arguments (no shell)
exec([cmdname, argv0], arg1, ...)
-
command name, argv and zero or more arguments (no shell)
In the first form, the string is taken as a command line that is subject to shell expansion before being executed.
The standard shell always means
"/bin/sh"
on Unix-like systems, same asENV["RUBYSHELL"]
(orENV["COMSPEC"]
on Windows NT series), and similar.If the string from the first form (
exec("command")
) follows these simple rules:-
no meta characters
-
no shell reserved word and no special built-in
-
Ruby invokes the command directly without shell
You can force shell invocation by adding “;” to the string (because “;” is a meta character).
Note that this behavior is observable by pid obtained (return value of spawn() and IO#pid for IO.popen) is the pid of the invoked command, not shell.
In the second form (
exec("command1", "arg1", ...)
), the first is taken as a command name and the rest are passed as parameters to command with no shell expansion.In the third form (
exec(["command", "argv0"], "arg1", ...)
), starting a two-element array at the beginning of the command, the first element is the command to be executed, and the second argument is used as theargv[0]
value, which may show up in process listings.In order to execute the command, one of the
exec(2)
system calls are used, so the running command may inherit some of the environment of the original program (including open file descriptors).This behavior is modified by the given
env
andoptions
parameters. See ::spawn for details.If the command fails to execute (typically Errno::ENOENT when it was not found) a SystemCallError exception is raised.
This method modifies process attributes according to given
options
beforeexec(2)
system call. See ::spawn for more details about the givenoptions
.The modified attributes may be retained when
exec(2)
system call fails.For example, hard resource limits are not restorable.
Consider to create a child process using ::spawn or Kernel#system if this is not acceptable.
exec "echo *" # echoes list of files in current directory # never get here exec "echo", "*" # echoes an asterisk # never get here
-
3215 3216 3217 3218 3219 3220 |
# File 'process.c', line 3215
static VALUE
f_exec(int c, const VALUE *a, VALUE _)
{
rb_f_exec(c, a);
UNREACHABLE_RETURN(Qnil);
}
|
.exit(status = true) ⇒ Object .Kernel::exit(status = true) ⇒ Object .Process::exit(status = true) ⇒ Object
Initiates the termination of the Ruby script by raising the SystemExit exception. This exception may be caught. The optional parameter is used to return a status code to the invoking environment. true
and FALSE
of status means success and failure respectively. The interpretation of other integer values are system dependent.
begin
exit
puts "never get here"
rescue SystemExit
puts "rescued a SystemExit exception"
end
puts "after begin block"
produces:
rescued a SystemExit exception
after begin block
Just prior to termination, Ruby executes any at_exit
functions (see Kernel::at_exit) and runs any object finalizers (see ObjectSpace::define_finalizer).
at_exit { puts "at_exit function" }
ObjectSpace.define_finalizer("string", proc { puts "in finalizer" })
exit
produces:
at_exit function
in finalizer
4486 4487 4488 4489 4490 4491 |
# File 'process.c', line 4486
static VALUE
f_exit(int c, const VALUE *a, VALUE _)
{
rb_f_exit(c, a);
UNREACHABLE_RETURN(Qnil);
}
|
.exit!(status = false) ⇒ Object
Exits the process immediately. No exit handlers are run. status is returned to the underlying system as the exit status.
Process.exit!(true)
4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 |
# File 'process.c', line 4399
static VALUE
rb_f_exit_bang(int argc, VALUE *argv, VALUE obj)
{
int istatus;
if (rb_check_arity(argc, 0, 1) == 1) {
istatus = exit_status_code(argv[0]);
}
else {
istatus = EXIT_FAILURE;
}
_exit(istatus);
UNREACHABLE_RETURN(Qnil);
}
|
.fork { ... } ⇒ Integer? .fork { ... } ⇒ Integer?
Creates a subprocess. If a block is specified, that block is run in the subprocess, and the subprocess terminates with a status of zero. Otherwise, the fork
call returns twice, once in the parent, returning the process ID of the child, and once in the child, returning nil. The child process can exit using Kernel.exit! to avoid running any at_exit
functions. The parent process should use Process.wait to collect the termination statuses of its children or use Process.detach to register disinterest in their status; otherwise, the operating system may accumulate zombie processes.
The thread calling fork is the only thread in the created child process. fork doesn’t copy other threads.
If fork is not usable, Process.respond_to?(:fork) returns false.
Note that fork(2) is not available on some platforms like Windows and NetBSD 4. Therefore you should use spawn() instead of fork().
4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 |
# File 'process.c', line 4337
static VALUE
rb_f_fork(VALUE obj)
{
rb_pid_t pid;
switch (pid = rb_fork_ruby(NULL)) {
case 0:
rb_thread_atfork();
if (rb_block_given_p()) {
int status;
rb_protect(rb_yield, Qundef, &status);
ruby_stop(status);
}
return Qnil;
case -1:
rb_sys_fail("fork(2)");
return Qnil;
default:
return PIDT2NUM(pid);
}
}
|
.getpgid(pid) ⇒ Integer
5199 5200 5201 5202 5203 5204 5205 5206 5207 |
# File 'process.c', line 5199
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
5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 |
# File 'process.c', line 5140
static VALUE
proc_getpgrp(VALUE _)
{
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
Gets the scheduling priority for specified process, process group, or user. kind indicates the kind of entity to find: one of Process::PRIO_PGRP, Process::PRIO_USER, or Process::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
5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 |
# File 'process.c', line 5345
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
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 Process::RLIMIT_CORE. See Process.setrlimit for details.
cur_limit and max_limit may be Process::RLIM_INFINITY, Process::RLIM_SAVED_MAX or Process::RLIM_SAVED_CUR. See Process.setrlimit and the system getrlimit(2) manual for details.
5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 |
# File 'process.c', line 5636
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));
}
|
.getsid ⇒ Integer .getsid(pid) ⇒ Integer
5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 |
# File 'process.c', line 5251
static VALUE
proc_getsid(int argc, VALUE *argv, VALUE _)
{
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);
}
|
.gid ⇒ Integer .Process::GID.rid ⇒ Integer .Process::Sys.getgid ⇒ Integer
Returns the (real) group ID for this process.
Process.gid #=> 500
6690 6691 6692 6693 6694 6695 |
# File 'process.c', line 6690
static VALUE
proc_getgid(VALUE obj)
{
rb_gid_t gid = getgid();
return GIDT2NUM(gid);
}
|
.gid=(integer) ⇒ Integer
Sets the group ID for this process.
6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 |
# File 'process.c', line 6706
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
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 GID list of the current process by this expression:
Process.groups.uniq.sort
6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 |
# File 'process.c', line 6810
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
6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 |
# File 'process.c', line 6854
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);
}
|
.initgroups(username, gid) ⇒ Array
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]
6907 6908 6909 6910 6911 6912 6913 6914 |
# File 'process.c', line 6907
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
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. If pid is negative, results are dependent on the operating system. 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.
8602 8603 8604 8605 8606 |
# File 'process.c', line 8602
static VALUE
proc_rb_f_kill(int c, const VALUE *v, VALUE _)
{
return rb_f_kill(c, v);
}
|
.last_status ⇒ Process::Status?
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
616 617 618 619 620 |
# File 'process.c', line 616
static VALUE
proc_s_last_status(VALUE mod)
{
return rb_last_status_get();
}
|
.maxgroups ⇒ Integer
Returns the maximum number of gids allowed in the supplemental group access list.
Process.maxgroups #=> 32
6930 6931 6932 6933 6934 |
# File 'process.c', line 6930
static VALUE
proc_getmaxgroups(VALUE obj)
{
return INT2FIX(maxgroups());
}
|
.maxgroups=(integer) ⇒ Integer
Sets the maximum number of gids allowed in the supplemental group access list.
6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 |
# File 'process.c', line 6948
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 should 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
Returns the process id of this process. Not available on all platforms.
Process.pid #=> 27415
505 506 507 508 509 |
# File 'process.c', line 505
static VALUE
proc_get_pid(VALUE _)
{
return get_pid();
}
|
.ppid ⇒ Integer
533 534 535 536 537 |
# File 'process.c', line 533
static VALUE
proc_get_ppid(VALUE _)
{
return get_ppid();
}
|
.setpgid(pid, integer) ⇒ 0
Sets the process group ID of pid (0 indicates this process) to integer. Not available on all platforms.
5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 |
# File 'process.c', line 5222
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
Equivalent to setpgid(0,0)
. Not available on all platforms.
5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 |
# File 'process.c', line 5169
static VALUE
proc_setpgrp(VALUE _)
{
/* 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
See Process.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
5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 |
# File 'process.c', line 5376
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
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.
2380 2381 2382 2383 2384 |
# File 'ruby.c', line 2380
static VALUE
proc_setproctitle(VALUE process, VALUE title)
{
return ruby_setproctitle(title);
}
|
.setrlimit(resource, cur_limit, max_limit) ⇒ nil .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 Process::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 Process::RLIM_INFINITY, which means that the resource is not limited. They may be Process::RLIM_SAVED_MAX, Process::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])
5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 |
# File 'process.c', line 5702
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
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
5285 5286 5287 5288 5289 5290 5291 5292 5293 |
# File 'process.c', line 5285
static VALUE
proc_setsid(VALUE _)
{
rb_pid_t pid;
pid = setsid();
if (pid < 0) rb_sys_fail(0);
return PIDT2NUM(pid);
}
|
.spawn([env,][,options]) ⇒ Object .spawn([env,][,options]) ⇒ Object
spawn executes specified command and return its pid.
pid = spawn("tar xf ruby-2.0.0-p195.tar.bz2")
Process.wait pid
pid = spawn(RbConfig.ruby, "-eputs'Hello, world!'")
Process.wait pid
This method is similar to Kernel#system but it doesn’t wait for the command to finish.
The parent process should use Process.wait to collect the termination status of its child or use Process.detach to register disinterest in their status; otherwise, the operating system may accumulate zombie processes.
spawn has bunch of options to specify process attributes:
env: hash
name => val : set the environment variable
name => nil : unset the environment variable
the keys and the values except for +nil+ must be strings.
command...:
commandline : command line string which is passed to the standard shell
cmdname, arg1, ... : command name and one or more arguments (This form does not use the shell. See below for caveats.)
[cmdname, argv0], arg1, ... : command name, argv[0] and zero or more arguments (no shell)
options: hash
clearing environment variables:
:unsetenv_others => true : clear environment variables except specified by env
:unsetenv_others => false : don't clear (default)
process group:
:pgroup => true or 0 : make a new process group
:pgroup => pgid : join the specified process group
:pgroup => nil : don't change the process group (default)
create new process group: Windows only
:new_pgroup => true : the new process is the root process of a new process group
:new_pgroup => false : don't create a new process group (default)
resource limit: resourcename is core, cpu, data, etc. See Process.setrlimit.
:rlimit_resourcename => limit
:rlimit_resourcename => [cur_limit, max_limit]
umask:
:umask => int
redirection:
key:
FD : single file descriptor in child process
[FD, FD, ...] : multiple file descriptor in child process
value:
FD : redirect to the file descriptor in parent process
string : redirect to file with open(string, "r" or "w")
[string] : redirect to file with open(string, File::RDONLY)
[string, open_mode] : redirect to file with open(string, open_mode, 0644)
[string, open_mode, perm] : redirect to file with open(string, open_mode, perm)
[:child, FD] : redirect to the redirected file descriptor
:close : close the file descriptor in child process
FD is one of follows
:in : the file descriptor 0 which is the standard input
:out : the file descriptor 1 which is the standard output
:err : the file descriptor 2 which is the standard error
integer : the file descriptor of specified the integer
io : the file descriptor specified as io.fileno
file descriptor inheritance: close non-redirected non-standard fds (3, 4, 5, ...) or not
:close_others => false : inherit
current directory:
:chdir => str
The cmdname, arg1, ...
form does not use the shell. However, on different OSes, different things are provided as built-in commands. An example of this is ‘echo’, which is a built-in on Windows, but is a normal program on Linux and Mac OS X. This means that Process.spawn 'echo', '%Path%'
will display the contents of the %Path%
environment variable on Windows, but Process.spawn 'echo', '$PATH'
prints the literal $PATH
.
If a hash is given as env
, the environment is updated by env
before exec(2)
in the child process. If a pair in env
has nil as the value, the variable is deleted.
# set FOO as BAR and unset BAZ.
pid = spawn({"FOO"=>"BAR", "BAZ"=>nil}, command)
If a hash is given as options
, it specifies process group, create new process group, resource limit, current directory, umask and redirects for the child process. Also, it can be specified to clear environment variables.
The :unsetenv_others
key in options
specifies to clear environment variables, other than specified by env
.
pid = spawn(command, :unsetenv_others=>true) # no environment variable
pid = spawn({"FOO"=>"BAR"}, command, :unsetenv_others=>true) # FOO only
The :pgroup
key in options
specifies a process group. The corresponding value should be true, zero, a positive integer, or nil. true and zero cause the process to be a process leader of a new process group. A non-zero positive integer causes the process to join the provided process group. The default value, nil, causes the process to remain in the same process group.
pid = spawn(command, :pgroup=>true) # process leader
pid = spawn(command, :pgroup=>10) # belongs to the process group 10
The :new_pgroup
key in options
specifies to pass CREATE_NEW_PROCESS_GROUP
flag to CreateProcessW()
that is Windows API. This option is only for Windows. true means the new process is the root process of the new process group. The new process has CTRL+C disabled. This flag is necessary for Process.kill(:SIGINT, pid)
on the subprocess. :new_pgroup is false by default.
pid = spawn(command, :new_pgroup=>true) # new process group
pid = spawn(command, :new_pgroup=>false) # same process group
The :rlimit_
foo key specifies a resource limit. foo should be one of resource types such as core
. The corresponding value should be an integer or an array which have one or two integers: same as cur_limit and max_limit arguments for Process.setrlimit.
cur, max = Process.getrlimit(:CORE)
pid = spawn(command, :rlimit_core=>[0,max]) # disable core temporary.
pid = spawn(command, :rlimit_core=>max) # enable core dump
pid = spawn(command, :rlimit_core=>0) # never dump core.
The :umask
key in options
specifies the umask.
pid = spawn(command, :umask=>077)
The :in, :out, :err, an integer, an IO and an array key specifies a redirection. The redirection maps a file descriptor in the child process.
For example, stderr can be merged into stdout as follows:
pid = spawn(command, :err=>:out)
pid = spawn(command, 2=>1)
pid = spawn(command, STDERR=>:out)
pid = spawn(command, STDERR=>STDOUT)
The hash keys specifies a file descriptor in the child process started by #spawn. :err, 2 and STDERR specifies the standard error stream (stderr).
The hash values specifies a file descriptor in the parent process which invokes #spawn. :out, 1 and STDOUT specifies the standard output stream (stdout).
In the above example, the standard output in the child process is not specified. So it is inherited from the parent process.
The standard input stream (stdin) can be specified by :in, 0 and STDIN.
A filename can be specified as a hash value.
pid = spawn(command, :in=>"/dev/null") # read mode
pid = spawn(command, :out=>"/dev/null") # write mode
pid = spawn(command, :err=>"log") # write mode
pid = spawn(command, [:out, :err]=>"/dev/null") # write mode
pid = spawn(command, 3=>"/dev/null") # read mode
For stdout and stderr (and combination of them), it is opened in write mode. Otherwise read mode is used.
For specifying flags and permission of file creation explicitly, an array is used instead.
pid = spawn(command, :in=>["file"]) # read mode is assumed
pid = spawn(command, :in=>["file", "r"])
pid = spawn(command, :out=>["log", "w"]) # 0644 assumed
pid = spawn(command, :out=>["log", "w", 0600])
pid = spawn(command, :out=>["log", File::WRONLY|File::EXCL|File::CREAT, 0600])
The array specifies a filename, flags and permission. The flags can be a string or an integer. If the flags is omitted or nil, File::RDONLY is assumed. The permission should be an integer. If the permission is omitted or nil, 0644 is assumed.
If an array of IOs and integers are specified as a hash key, all the elements are redirected.
# stdout and stderr is redirected to log file.
# The file "log" is opened just once.
pid = spawn(command, [:out, :err]=>["log", "w"])
Another way to merge multiple file descriptors is [:child, fd]. [:child, fd] means the file descriptor in the child process. This is different from fd. For example, :err=>:out means redirecting child stderr to parent stdout. But :err=>[:child, :out] means redirecting child stderr to child stdout. They differ if stdout is redirected in the child process as follows.
# stdout and stderr is redirected to log file.
# The file "log" is opened just once.
pid = spawn(command, :out=>["log", "w"], :err=>[:child, :out])
[:child, :out] can be used to merge stderr into stdout in IO.popen. In this case, IO.popen redirects stdout to a pipe in the child process and [:child, :out] refers the redirected stdout.
io = IO.popen(["sh", "-c", "echo out; echo err >&2", :err=>[:child, :out]])
p io.read #=> "out\nerr\n"
The :chdir
key in options
specifies the current directory.
pid = spawn(command, :chdir=>"/var/tmp")
spawn closes all non-standard unspecified descriptors by default. The “standard” descriptors are 0, 1 and 2. This behavior is specified by :close_others option. :close_others doesn’t affect the standard descriptors which are closed only if :close is specified explicitly.
pid = spawn(command, :close_others=>true) # close 3,4,5,... (default)
pid = spawn(command, :close_others=>false) # don't close 3,4,5,...
:close_others is false by default for spawn and IO.popen.
Note that fds which close-on-exec flag is already set are closed regardless of :close_others option.
So IO.pipe and spawn can be used as IO.popen.
# similar to r = IO.popen(command)
r, w = IO.pipe
pid = spawn(command, :out=>w) # r, w is closed in the child process.
w.close
:close is specified as a hash value to close a fd individually.
f = open(foo)
system(command, f=>:close) # don't inherit f.
If a file descriptor need to be inherited, io=>io can be used.
# valgrind has --log-fd option for log destination.
# log_w=>log_w indicates log_w.fileno inherits to child process.
log_r, log_w = IO.pipe
pid = spawn("valgrind", "--log-fd=#{log_w.fileno}", "echo", "a", log_w=>log_w)
log_w.close
p log_r.read
It is also possible to exchange file descriptors.
pid = spawn(command, :out=>:err, :err=>:out)
The hash keys specify file descriptors in the child process. The hash values specifies file descriptors in the parent process. So the above specifies exchanging stdout and stderr. Internally, spawn
uses an extra file descriptor to resolve such cyclic file descriptor mapping.
See Kernel.exec for the standard shell.
5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 |
# File 'process.c', line 5059
static VALUE
rb_f_spawn(int argc, VALUE *argv, VALUE _)
{
rb_pid_t pid;
char errmsg[CHILD_ERRMSG_BUFLEN] = { '\0' };
VALUE execarg_obj, fail_str;
struct rb_execarg *eargp;
execarg_obj = rb_execarg_new(argc, argv, TRUE, FALSE);
eargp = rb_execarg_get(execarg_obj);
fail_str = eargp->use_shell ? eargp->invoke.sh.shell_script : eargp->invoke.cmd.command_name;
pid = rb_execarg_spawn(execarg_obj, errmsg, sizeof(errmsg));
if (pid == -1) {
int err = errno;
rb_exec_fail(eargp, err, errmsg);
RB_GC_GUARD(execarg_obj);
rb_syserr_fail_str(err, fail_str);
}
#if defined(HAVE_WORKING_FORK) || defined(HAVE_SPAWNV)
return PIDT2NUM(pid);
#else
return Qnil;
#endif
}
|
.times ⇒ aProcessTms
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]
7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 |
# File 'process.c', line 7877
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;
}
|
.uid ⇒ Integer .Process::UID.rid ⇒ Integer .Process::Sys.getuid ⇒ Integer
Returns the (real) user ID of this process.
Process.uid #=> 501
6287 6288 6289 6290 6291 6292 |
# File 'process.c', line 6287
static VALUE
proc_getuid(VALUE obj)
{
rb_uid_t uid = getuid();
return UIDT2NUM(uid);
}
|
.uid=(user) ⇒ Numeric
Sets the (user) user ID for this process. Not available on all platforms.
6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 |
# File 'process.c', line 6304
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;
}
|
.wait ⇒ Integer .wait(pid = -1, flags = 0) ⇒ Integer .waitpid(pid = -1, flags = 0) ⇒ Integer
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 Process::WNOHANG (do not block if no child available) or Process::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
1550 1551 1552 1553 1554 |
# File 'process.c', line 1550
static VALUE
proc_m_wait(int c, VALUE *v, VALUE _)
{
return proc_wait(c, v);
}
|
.wait2(pid = -1, flags = 0) ⇒ Array .waitpid2(pid = -1, flags = 0) ⇒ Array
Waits for a child process to exit (see Process::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
1573 1574 1575 1576 1577 1578 1579 |
# File 'process.c', line 1573
static VALUE
proc_wait2(int argc, VALUE *argv, VALUE _)
{
VALUE pid = proc_wait(argc, argv);
if (NIL_P(pid)) return Qnil;
return rb_assoc_new(pid, rb_last_status_get());
}
|
.waitall ⇒ Array
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>]]
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 |
# File 'process.c', line 1602
static VALUE
proc_waitall(VALUE _)
{
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;
}
|
.wait ⇒ Integer .wait(pid = -1, flags = 0) ⇒ Integer .waitpid(pid = -1, flags = 0) ⇒ Integer
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 Process::WNOHANG (do not block if no child available) or Process::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
1550 1551 1552 1553 1554 |
# File 'process.c', line 1550
static VALUE
proc_m_wait(int c, VALUE *v, VALUE _)
{
return proc_wait(c, v);
}
|
.wait2(pid = -1, flags = 0) ⇒ Array .waitpid2(pid = -1, flags = 0) ⇒ Array
Waits for a child process to exit (see Process::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
1573 1574 1575 1576 1577 1578 1579 |
# File 'process.c', line 1573
static VALUE
proc_wait2(int argc, VALUE *argv, VALUE _)
{
VALUE pid = proc_wait(argc, argv);
if (NIL_P(pid)) return Qnil;
return rb_assoc_new(pid, rb_last_status_get());
}
|
Instance Method Details
#argv0 ⇒ Object (private)
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.
2355 2356 2357 2358 2359 |
# File 'ruby.c', line 2355
static VALUE
proc_argv0(VALUE process)
{
return rb_orig_progname;
}
|
#clock_getres(clock_id[, unit]) ⇒ Numeric (private)
Returns an estimate of the resolution of a clock_id
using the POSIX clock_getres()
function.
Note the reported resolution is often inaccurate on most platforms due to underlying bugs for this function and therefore the reported resolution often differs from the actual resolution of the clock in practice. Inaccurate reported resolutions have been observed for various clocks including CLOCK_MONOTONIC and CLOCK_MONOTONIC_RAW when using Linux, macOS, BSD or AIX platforms, when using ARM processors, or when using virtualization.
clock_id
specifies a kind of clock. See the document of Process.clock_gettime
for details. clock_id
can be a symbol as for Process.clock_gettime
.
If the given clock_id
is not supported, Errno::EINVAL is raised.
unit
specifies the type of the return value. Process.clock_getres
accepts unit
as Process.clock_gettime
. The default value, :float_second
, is also the same as Process.clock_gettime
.
Process.clock_getres
also accepts :hertz
as unit
. :hertz
means the reciprocal of :float_second
.
:hertz
can be used to obtain the exact value of the clock ticks per second for the times() function and CLOCKS_PER_SEC for the 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
8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 |
# File 'process.c', line 8450
static VALUE
rb_clock_getres(int argc, VALUE *argv, VALUE _)
{
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 (private)
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 Process::CLOCK_REALTIME and Process::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, FreeBSD 9.3, 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
- CLOCK_TAI
-
Linux 3.10
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, Process::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.
8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 |
# File 'process.c', line 8249
static VALUE
rb_clock_gettime(int argc, VALUE *argv, VALUE _)
{
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 (private) #daemon(nochdir = nil, noclose = nil) ⇒ 0 (private)
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::*.
6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 |
# File 'process.c', line 6988
static VALUE
proc_daemon(int argc, VALUE *argv, VALUE _)
{
int n, nochdir = FALSE, noclose = FALSE;
switch (rb_check_arity(argc, 0, 2)) {
case 2: noclose = TO_BOOL(argv[1], "noclose");
case 1: nochdir = TO_BOOL(argv[0], "nochdir");
}
prefork();
n = rb_daemon(nochdir, noclose);
if (n < 0) rb_sys_fail("daemon");
return INT2FIX(n);
}
|
#detach(pid) ⇒ Object (private)
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. 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, Process::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)
1701 1702 1703 1704 1705 |
# File 'process.c', line 1701
static VALUE
proc_detach(VALUE obj, VALUE pid)
{
return rb_detach_process(NUM2PIDT(pid));
}
|
#egid ⇒ Integer (private) #Process::GID.eid ⇒ Integer (private) #Process::Sys.geteid ⇒ Integer (private)
7380 7381 7382 7383 7384 7385 7386 |
# File 'process.c', line 7380
static VALUE
proc_getegid(VALUE obj)
{
rb_gid_t egid = getegid();
return GIDT2NUM(egid);
}
|
#egid= ⇒ Object (private)
#euid ⇒ Integer (private) #Process::UID.eid ⇒ Integer (private) #Process::Sys.geteuid ⇒ Integer (private)
7256 7257 7258 7259 7260 7261 |
# File 'process.c', line 7256
static VALUE
proc_geteuid(VALUE obj)
{
rb_uid_t euid = geteuid();
return UIDT2NUM(euid);
}
|
#euid=(user) ⇒ Object (private)
Sets the effective user ID for this process. Not available on all platforms.
7295 7296 7297 7298 7299 7300 7301 |
# File 'process.c', line 7295
static VALUE
proc_seteuid_m(VALUE mod, VALUE euid)
{
check_uid_switch();
proc_seteuid(OBJ2UID(euid));
return euid;
}
|
#getpgid(pid) ⇒ Integer (private)
5199 5200 5201 5202 5203 5204 5205 5206 5207 |
# File 'process.c', line 5199
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 (private)
5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 |
# File 'process.c', line 5140
static VALUE
proc_getpgrp(VALUE _)
{
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 (private)
Gets the scheduling priority for specified process, process group, or user. kind indicates the kind of entity to find: one of Process::PRIO_PGRP, Process::PRIO_USER, or Process::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
5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 |
# File 'process.c', line 5345
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 (private)
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 Process::RLIMIT_CORE. See Process.setrlimit for details.
cur_limit and max_limit may be Process::RLIM_INFINITY, Process::RLIM_SAVED_MAX or Process::RLIM_SAVED_CUR. See Process.setrlimit and the system getrlimit(2) manual for details.
5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 |
# File 'process.c', line 5636
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));
}
|
#getsid ⇒ Integer (private) #getsid(pid) ⇒ Integer (private)
5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 |
# File 'process.c', line 5251
static VALUE
proc_getsid(int argc, VALUE *argv, VALUE _)
{
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);
}
|
#gid ⇒ Integer (private) #Process::GID.rid ⇒ Integer (private) #Process::Sys.getgid ⇒ Integer (private)
6690 6691 6692 6693 6694 6695 |
# File 'process.c', line 6690
static VALUE
proc_getgid(VALUE obj)
{
rb_gid_t gid = getgid();
return GIDT2NUM(gid);
}
|
#gid=(integer) ⇒ Integer (private)
Sets the group ID for this process.
6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 |
# File 'process.c', line 6706
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 (private)
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 GID list of the current process by this expression:
Process.groups.uniq.sort
6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 |
# File 'process.c', line 6810
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 (private)
6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 |
# File 'process.c', line 6854
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);
}
|
#initgroups(username, gid) ⇒ Array (private)
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]
6907 6908 6909 6910 6911 6912 6913 6914 |
# File 'process.c', line 6907
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 (private)
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. If pid is negative, results are dependent on the operating system. 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.
8602 8603 8604 8605 8606 |
# File 'process.c', line 8602
static VALUE
proc_rb_f_kill(int c, const VALUE *v, VALUE _)
{
return rb_f_kill(c, v);
}
|
#maxgroups ⇒ Integer (private)
6930 6931 6932 6933 6934 |
# File 'process.c', line 6930
static VALUE
proc_getmaxgroups(VALUE obj)
{
return INT2FIX(maxgroups());
}
|
#maxgroups=(integer) ⇒ Integer (private)
Sets the maximum number of gids allowed in the supplemental group access list.
6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 |
# File 'process.c', line 6948
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 should 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 (private)
505 506 507 508 509 |
# File 'process.c', line 505
static VALUE
proc_get_pid(VALUE _)
{
return get_pid();
}
|
#ppid ⇒ Integer (private)
533 534 535 536 537 |
# File 'process.c', line 533
static VALUE
proc_get_ppid(VALUE _)
{
return get_ppid();
}
|
#setpgid(pid, integer) ⇒ 0 (private)
Sets the process group ID of pid (0 indicates this process) to integer. Not available on all platforms.
5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 |
# File 'process.c', line 5222
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 (private)
Equivalent to setpgid(0,0)
. Not available on all platforms.
5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 |
# File 'process.c', line 5169
static VALUE
proc_setpgrp(VALUE _)
{
/* 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 (private)
See Process.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
5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 |
# File 'process.c', line 5376
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 (private)
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.
2380 2381 2382 2383 2384 |
# File 'ruby.c', line 2380
static VALUE
proc_setproctitle(VALUE process, VALUE title)
{
return ruby_setproctitle(title);
}
|
#setrlimit(resource, cur_limit, max_limit) ⇒ nil (private) #setrlimit(resource, cur_limit) ⇒ nil (private)
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 Process::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 Process::RLIM_INFINITY, which means that the resource is not limited. They may be Process::RLIM_SAVED_MAX, Process::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])
5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 |
# File 'process.c', line 5702
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 (private)
5285 5286 5287 5288 5289 5290 5291 5292 5293 |
# File 'process.c', line 5285
static VALUE
proc_setsid(VALUE _)
{
rb_pid_t pid;
pid = setsid();
if (pid < 0) rb_sys_fail(0);
return PIDT2NUM(pid);
}
|
#times ⇒ aProcessTms (private)
7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 |
# File 'process.c', line 7877
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;
}
|
#uid ⇒ Integer (private) #Process::UID.rid ⇒ Integer (private) #Process::Sys.getuid ⇒ Integer (private)
6287 6288 6289 6290 6291 6292 |
# File 'process.c', line 6287
static VALUE
proc_getuid(VALUE obj)
{
rb_uid_t uid = getuid();
return UIDT2NUM(uid);
}
|
#uid=(user) ⇒ Numeric (private)
Sets the (user) user ID for this process. Not available on all platforms.
6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 |
# File 'process.c', line 6304
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;
}
|
#wait ⇒ Integer (private) #wait(pid = -1, flags = 0) ⇒ Integer (private) #waitpid(pid = -1, flags = 0) ⇒ Integer (private)
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 Process::WNOHANG (do not block if no child available) or Process::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
1550 1551 1552 1553 1554 |
# File 'process.c', line 1550
static VALUE
proc_m_wait(int c, VALUE *v, VALUE _)
{
return proc_wait(c, v);
}
|
#wait2(pid = -1, flags = 0) ⇒ Array (private) #waitpid2(pid = -1, flags = 0) ⇒ Array (private)
Waits for a child process to exit (see Process::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
1573 1574 1575 1576 1577 1578 1579 |
# File 'process.c', line 1573
static VALUE
proc_wait2(int argc, VALUE *argv, VALUE _)
{
VALUE pid = proc_wait(argc, argv);
if (NIL_P(pid)) return Qnil;
return rb_assoc_new(pid, rb_last_status_get());
}
|
#waitall ⇒ Array (private)
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>]]
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 |
# File 'process.c', line 1602
static VALUE
proc_waitall(VALUE _)
{
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;
}
|
#wait ⇒ Integer (private) #wait(pid = -1, flags = 0) ⇒ Integer (private) #waitpid(pid = -1, flags = 0) ⇒ Integer (private)
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 Process::WNOHANG (do not block if no child available) or Process::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
1550 1551 1552 1553 1554 |
# File 'process.c', line 1550
static VALUE
proc_m_wait(int c, VALUE *v, VALUE _)
{
return proc_wait(c, v);
}
|
#wait2(pid = -1, flags = 0) ⇒ Array (private) #waitpid2(pid = -1, flags = 0) ⇒ Array (private)
Waits for a child process to exit (see Process::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
1573 1574 1575 1576 1577 1578 1579 |
# File 'process.c', line 1573
static VALUE
proc_wait2(int argc, VALUE *argv, VALUE _)
{
VALUE pid = proc_wait(argc, argv);
if (NIL_P(pid)) return Qnil;
return rb_assoc_new(pid, rb_last_status_get());
}
|