123456789_123456789_123456789_123456789_123456789_

Module: Process

Relationships & Source Files
Namespace Children
Modules:
GID, Sys, UID
Classes:
Defined in: ruby.c,
process.c,
signal.c

Constant Summary

Class Attribute Summary

Class Method Summary

Class Attribute Details

.euidFixnum (rw, mod_func) UID.eidFixnum Sys.geteuidFixnum

Returns the effective user ID for this process.

Process.euid   #=> 501

.euid=(user) (rw, mod_func)

Sets the effective user ID for this process. Not available on all platforms.

.gidFixnum (rw, mod_func) GID.ridFixnum Sys.getgidFixnum

Returns the (real) group ID for this process.

Process.gid   #=> 500

.gid=(fixnum) ⇒ Fixnum (rw, mod_func)

Sets the group ID for this process.

.groupsArray (rw, mod_func)

Get an ::Array of the gids of groups in the supplemental group access list for this process.

Process.groups   #=> [27, 6, 10, 11]

.groups=(array) ⇒ Array (rw, mod_func)

Set the supplemental group access list to the given ::Array of group IDs.

Process.groups   #=> [0, 1, 2, 3, 4, 6, 10, 11, 20, 26, 27]
Process.groups = [27, 6, 10, 11]   #=> [27, 6, 10, 11]
Process.groups   #=> [27, 6, 10, 11]

.maxgroupsFixnum (rw, mod_func)

Returns the maximum number of gids allowed in the supplemental group access list.

Process.maxgroups   #=> 32

.maxgroups=(fixnum) ⇒ Fixnum (rw, mod_func)

Sets the maximum number of gids allowed in the supplemental group access list.

.pidFixnum (readonly, mod_func)

Alias for $$.

.uidFixnum (rw, mod_func) UID.ridFixnum Sys.getuidFixnum

Returns the (real) user ID of this process.

Process.uid   #=> 501

.uid=(user) ⇒ Numeric (rw, mod_func)

Sets the (user) user ID for this process. Not available on all platforms.

Class Method Details

.abort .abort([msg])

Alias for Kernel.abort.

.argv0frozen_string (mod_func)

Returns the name of the script being executed. The value is not affected by assigning a new value to $0.

This method first appeared in Ruby 2.1 to serve as a global variable free means to get the script name.

.clock_getres(clock_id [, unit]) ⇒ Numeric (mod_func)

Returns the time resolution returned by POSIX clock_getres() function.

clock_id specifies a kind of clock. See the document of .clock_gettime for details.

clock_id can be a symbol as .clock_gettime. However the result may not be accurate. For example, Process.clock_getres(:GETTIMEOFDAY_BASED_CLOCK_REALTIME) returns 1.0e-06 which means 1 microsecond, but actual resolution can be more coarse.

If the given clock_id is not supported, Errno::EINVAL is raised.

unit specifies a type of the return value. clock_getres accepts unit as .clock_gettime. The default value, :float_second, is also same as .clock_gettime.

clock_getres also accepts :hertz as unit. :hertz means a the reciprocal of :float_second.

:hertz can be used to obtain the exact value of the clock ticks per second for times() function and CLOCKS_PER_SEC for clock() function.

Process.clock_getres(:TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID, :hertz) returns the clock ticks per second.

Process.clock_getres(:CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID, :hertz) returns CLOCKS_PER_SEC.

p Process.clock_getres(Process::CLOCK_MONOTONIC)
#=> 1.0e-09

.clock_gettime(clock_id [, unit]) ⇒ Numeric (mod_func)

Returns a time returned by POSIX clock_gettime() function.

p Process.clock_gettime(Process::CLOCK_MONOTONIC)
#=> 896053.968060096

clock_id specifies a kind of clock. It is specifed as a constant which begins with Process::CLOCK_ such as CLOCK_REALTIME and CLOCK_MONOTONIC.

The supported constants depends on OS and version. Ruby provides following types of clock_id if available.

CLOCK_REALTIME

SUSv2 to 4, Linux 2.5.63, FreeBSD 3.0, NetBSD 2.0, OpenBSD 2.1

CLOCK_MONOTONIC

SUSv3 to 4, Linux 2.5.63, FreeBSD 3.0, NetBSD 2.0, OpenBSD 3.4

CLOCK_PROCESS_CPUTIME_ID

SUSv3 to 4, Linux 2.5.63, OpenBSD 5.4

CLOCK_THREAD_CPUTIME_ID

SUSv3 to 4, Linux 2.5.63, FreeBSD 7.1, OpenBSD 5.4

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

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_PRECISE

FreeBSD 8.1

CLOCK_SECOND

FreeBSD 8.1

Note that SUS stands for Single Unix Specification. SUS contains POSIX and clock_gettime is defined in the POSIX part. SUS defines CLOCK_REALTIME mandatory but CLOCK_MONOTONIC, CLOCK_PROCESS_CPUTIME_ID and CLOCK_THREAD_CPUTIME_ID are optional.

Also, several symbols are accepted as clock_id. There are emulations for clock_gettime().

For example, CLOCK_REALTIME is defined as :GETTIMEOFDAY_BASED_CLOCK_REALTIME when clock_gettime() is not available.

Emulations for CLOCK_REALTIME:

:GETTIMEOFDAY_BASED_CLOCK_REALTIME

Use gettimeofday() defined by SUS. (SUSv4 obsoleted it, though.) The resolution is 1 microsecond.

:TIME_BASED_CLOCK_REALTIME

Use time() defined by ISO C. The resolution is 1 second.

Emulations for CLOCK_MONOTONIC:

:MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC

Use mach_absolute_time(), available on Darwin. The resolution is CPU dependent.

:TIMES_BASED_CLOCK_MONOTONIC

Use the result value of times() defined by POSIX. POSIX defines it as “times() shall return the elapsed real time, in clock ticks, since an arbitrary point in the past (for example, system start-up time)”. For example, GNU/Linux returns a value based on jiffies and it is monotonic. However, 4.4BSD uses gettimeofday() and it is not monotonic. (FreeBSD uses clock_gettime(CLOCK_MONOTONIC) instead, though.) The resolution is the clock tick. “getconf CLK_TCK” command shows the clock ticks per second. (The clock ticks per second is defined by HZ macro in older systems.) If it is 100 and clock_t is 32 bits integer type, the resolution is 10 millisecond and cannot represent over 497 days.

Emulations for CLOCK_PROCESS_CPUTIME_ID:

:GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID

Use getrusage() defined by SUS. getrusage() is used with RUSAGE_SELF to obtain the time only for the calling process (excluding the time for child processes). The result is addition of user time (ru_utime) and system time (ru_stime). The resolution is 1 microsecond.

:TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID

Use times() defined by POSIX. The result is addition of user time (tms_utime) and system time (tms_stime). tms_cutime and tms_cstime are ignored to exclude the time for child processes. The resolution is the clock tick. “getconf CLK_TCK” command shows the clock ticks per second. (The clock ticks per second is defined by HZ macro in older systems.) If it is 100, the resolution is 10 millisecond.

:CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID

Use clock() defined by ISO C. The resolution is 1/CLOCKS_PER_SEC. CLOCKS_PER_SEC is the C-level macro defined by time.h. SUS defines CLOCKS_PER_SEC is 1000000. Non-Unix systems may define it a different value, though. If CLOCKS_PER_SEC is 1000000 as SUS, the resolution is 1 microsecond. If CLOCKS_PER_SEC is 1000000 and clock_t is 32 bits integer type, it cannot represent over 72 minutes.

If the given clock_id is not supported, Errno::EINVAL is raised.

unit specifies a type of the return value.

:float_second

number of seconds as a float (default)

:float_millisecond

number of milliseconds as a float

:float_microsecond

number of microseconds as a float

:second

number of seconds as an integer

:millisecond

number of milliseconds as an integer

:microsecond

number of microseconds as an integer

:nanosecond

number of nanoseconds as an integer

The underlying function, clock_gettime(), returns a number of nanoseconds. ::Float object (IEEE 754 double) is not enough to represent the return value for CLOCK_REALTIME. If the exact nanoseconds value is required, use :nanoseconds as the unit.

The origin (zero) of the returned value varies. For example, system start up time, process start up time, the Epoch, etc.

The origin in CLOCK_REALTIME is defined as the Epoch (1970-01-01 00:00:00 UTC). But some systems count leap seconds and others doesn't. So the result can be interpreted differently across systems. Time.now is recommended over CLOCK_REALTIME.

.daemon0 (mod_func) .daemon(nochdir = nil, noclose = nil) ⇒ 0

Detach the process from controlling terminal and run in the background as system daemon. Unless the argument nochdir is true (i.e. non false), it changes the current working directory to the root (“/”). Unless the argument noclose is true, daemon() will redirect standard input, standard output and standard error to /dev/null. Return zero on success, or raise one of Errno.*.

.detach(pid) ⇒ Thread (mod_func)

Some operating systems retain the status of terminated child processes until the parent collects that status (normally using some variant of wait(). If the parent never collects this status, the child stays around as a zombie process. detach prevents this by setting up a separate Ruby thread whose sole job is to reap the status of the process pid when it terminates. Use detach only when you do not intent to explicitly wait for the child to terminate.

The waiting thread returns the exit status of the detached process when it terminates, so you can use Thread#join to know the result. If specified pid is not a valid child process ID, the thread returns nil immediately.

The waiting thread has .pid method which returns the pid.

In this first example, we don't reap the first child process, so it appears as a zombie in the process status display.

p1 = fork { sleep 0.1 }
p2 = fork { sleep 0.2 }
Process.waitpid(p2)
sleep 2
system("ps -ho pid,state -p #{p1}")

produces:

27389 Z

In the next example, detach is used to reap the child automatically.

p1 = fork { sleep 0.1 }
p2 = fork { sleep 0.2 }
Process.detach(p1)
Process.waitpid(p2)
sleep 2
system("ps -ho pid,state -p #{p1}")

(produces no output)

.egidFixnum (mod_func) GID.eidFixnum Sys.geteidFixnum

Returns the effective group ID for this process. Not available on all platforms.

Process.egid   #=> 500

.egid=(fixnum) ⇒ Fixnum (mod_func)

Sets the effective group ID for this process. Not available on all platforms.

.exec([env,] command... [,options])

Alias for Kernel.exec.

.exit(status = true) .exit(status = true)

Alias for Kernel.exit.

.exit!(status = false)

Alias for Kernel.exit!.

.forkFixnum? .forkFixnum?

Alias for Kernel.fork.

.getpgid(pid) ⇒ Integer (mod_func)

Returns the process group ID for the given process id. Not available on all platforms.

Process.getpgid(Process.ppid())   #=> 25527

.getpgrpInteger (mod_func)

Returns the process group ID for this process. Not available on all platforms.

Process.getpgid(0)   #=> 25527
Process.getpgrp      #=> 25527

.getpriority(kind, integer) ⇒ Fixnum (mod_func)

Gets the scheduling priority for specified process, process group, or user. kind indicates the kind of entity to find: one of PRIO_PGRP, PRIO_USER, or PRIO_PROCESS. integer is an id indicating the particular process, process group, or user (an id of 0 means current). Lower priorities are more favorable for scheduling. Not available on all platforms.

Process.getpriority(Process::PRIO_USER, 0)      #=> 19
Process.getpriority(Process::PRIO_PROCESS, 0)   #=> 19

.getrlimit(resource) ⇒ Array, max_limit (mod_func)

Gets the resource limit of the process. cur_limit means current (soft) limit and max_limit means maximum (hard) limit.

resource indicates the kind of resource to limit. It is specified as a symbol such as :CORE, a string such as "CORE" or a constant such as RLIMIT_CORE. See .setrlimit for details.

cur_limit and max_limit may be RLIM_INFINITY, RLIM_SAVED_MAX or RLIM_SAVED_CUR. See .setrlimit and the system getrlimit(2) manual for details.

.getsidInteger (mod_func) .getsid(pid) ⇒ Integer

Returns the session ID for for the given process id. If not give, return current process sid. Not available on all platforms.

Process.getsid()                #=> 27422
Process.getsid(0)               #=> 27422
Process.getsid(Process.pid())   #=> 27422

.initgroups(username, gid) ⇒ Array (mod_func)

Initializes the supplemental group access list by reading the system group database and using all groups of which the given user is a member. The group with the specified gid is also added to the list. Returns the resulting ::Array of the gids of all the groups in the supplementary group access list. Not available on all platforms.

Process.groups   #=> [0, 1, 2, 3, 4, 6, 10, 11, 20, 26, 27]
Process.initgroups( "mgranger", 30 )   #=> [30, 6, 10, 11]
Process.groups   #=> [30, 6, 10, 11]

.kill(signal, pid, ...) ⇒ Fixnum (mod_func)

Sends the given signal to the specified process id(s) if pid is positive. If pid is zero signal is sent to all processes whose group ID is equal to the group ID of the process. signal may be an integer signal number or a POSIX signal name (either with or without a SIG prefix). If signal is negative (or starts with a minus sign), kills process groups instead of processes. Not all signals are available on all platforms. The keys and values of Signal.list are known signal names and numbers, respectively.

pid = fork do
   Signal.trap("HUP") { puts "Ouch!"; exit }
   # ... do some work ...
end
# ...
Process.kill("HUP", pid)
Process.wait

produces:

Ouch!

If signal is an integer but wrong for signal, Errno::EINVAL or ::RangeError will be raised. Otherwise unless signal is a ::String or a ::Symbol, and a known signal name, ::ArgumentError will be raised.

Also, Errno::ESRCH or ::RangeError for invalid pid, Errno::EPERM when failed because of no privilege, will be raised. In these cases, signals may have been sent to preceding processes.

.ppidFixnum (mod_func)

Returns the process id of the parent of this process. Returns untrustworthy value on Win32/64. Not available on all platforms.

puts "I am #{Process.pid}"
Process.fork { puts "Dad is #{Process.ppid}" }

produces:

I am 27417
Dad is 27417

.setpgid(pid, integer) ⇒ 0 (mod_func)

Sets the process group ID of pid (0 indicates this process) to integer. Not available on all platforms.

.setpgrp0 (mod_func)

Equivalent to setpgid(0,0). Not available on all platforms.

.setpriority(kind, integer, priority) ⇒ 0 (mod_func)

See #getpriority.

Process.setpriority(Process::PRIO_USER, 0, 19)      #=> 0
Process.setpriority(Process::PRIO_PROCESS, 0, 19)   #=> 0
Process.getpriority(Process::PRIO_USER, 0)          #=> 19
Process.getpriority(Process::PRIO_PROCESS, 0)       #=> 19

.setproctitle(string) ⇒ String (mod_func)

Sets the process title that appears on the ps(1) command. Not necessarily effective on all platforms. No exception will be raised regardless of the result, nor will ::NotImplementedError be raised even if the platform does not support the feature.

Calling this method does not affect the value of $0.

Process.setproctitle('myapp: worker #%d' % worker_id)

This method first appeared in Ruby 2.1 to serve as a global variable free means to change the process title.

.setrlimit(resource, cur_limit, max_limit) ⇒ nil (mod_func) .setrlimit(resource, cur_limit) ⇒ nil

Sets the resource limit of the process. cur_limit means current (soft) limit and max_limit means maximum (hard) limit.

If max_limit is not given, cur_limit is used.

resource indicates the kind of resource to limit. It should be a symbol such as :CORE, a string such as "CORE" or a constant such as RLIMIT_CORE. The available resources are OS dependent. Ruby may support following resources.

AS

total available memory (bytes) (SUSv3, NetBSD, FreeBSD, OpenBSD but 4.4BSD-Lite)

CORE

core size (bytes) (SUSv3)

CPU

CPU time (seconds) (SUSv3)

DATA

data segment (bytes) (SUSv3)

FSIZE

file size (bytes) (SUSv3)

MEMLOCK

total size for mlock(2) (bytes) (4.4BSD, GNU/Linux)

MSGQUEUE

allocation for POSIX message queues (bytes) (GNU/Linux)

NICE

ceiling on process's nice(2) value (number) (GNU/Linux)

NOFILE

file descriptors (number) (SUSv3)

NPROC

number of processes for the user (number) (4.4BSD, GNU/Linux)

RSS

resident memory size (bytes) (4.2BSD, GNU/Linux)

RTPRIO

ceiling on the process's real-time priority (number) (GNU/Linux)

RTTIME

CPU time for real-time process (us) (GNU/Linux)

SBSIZE

all socket buffers (bytes) (NetBSD, FreeBSD)

SIGPENDING

number of queued signals allowed (signals) (GNU/Linux)

STACK

stack size (bytes) (SUSv3)

cur_limit and max_limit may be :INFINITY, "INFINITY" or RLIM_INFINITY, which means that the resource is not limited. They may be RLIM_SAVED_MAX, RLIM_SAVED_CUR and corresponding symbols and strings too. See system setrlimit(2) manual for details.

The following example raises the soft limit of core size to the hard limit to try to make core dump possible.

Process.setrlimit(:CORE, Process.getrlimit(:CORE)[1])

.setsidFixnum (mod_func)

Establishes this process as a new session and process group leader, with no controlling tty. Returns the session id. Not available on all platforms.

Process.setsid   #=> 27422

.spawn([env,] command... [,options]) ⇒ pid

Alias for Kernel.spawn.

.timesProcess (mod_func)

Returns a Tms structure (see Process::Tms) that contains user and system CPU times for this process, and also for children processes.

t = Process.times
[ t.utime, t.stime, t.cutime, t.cstime ]   #=> [0.0, 0.02, 0.00, 0.00]

.waitFixnum (mod_func) .wait(pid = -1, flags = 0) ⇒ Fixnum .waitpid(pid = -1, flags = 0) ⇒ Fixnum
Also known as: .waitpid

Waits for a child process to exit, returns its process id, and sets $? to a ::Process::Status object containing information on that process. Which child it waits on depends on the value of pid:

> 0

Waits for the child whose process ID equals pid.

0

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

-1

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

< -1

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

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

Calling this method raises a ::SystemCallError if there are no child processes. Not available on all platforms.

include Process
fork { exit 99 }                 #=> 27429
wait                             #=> 27429
$?.exitstatus                    #=> 99

pid = fork { sleep 3 }           #=> 27440
Time.now                         #=> 2008-03-08 19:56:16 +0900
waitpid(pid, Process::WNOHANG)   #=> nil
Time.now                         #=> 2008-03-08 19:56:16 +0900
waitpid(pid, 0)                  #=> 27440
Time.now                         #=> 2008-03-08 19:56:19 +0900

.wait2(pid = -1, flags = 0) ⇒ Array, status (mod_func) .waitpid2(pid = -1, flags = 0) ⇒ Array, status
Also known as: .waitpid2

Waits for a child process to exit (see .waitpid for exact semantics) and returns an array containing the process id and the exit status (a ::Process::Status object) of that child. Raises a ::SystemCallError if there are no child processes.

Process.fork { exit 99 }   #=> 27437
pid, status = Process.wait2
pid                        #=> 27437
status.exitstatus          #=> 99

.waitallArray, ... (mod_func)

Waits for all children, returning an array of pid/status pairs (where status is a ::Process::Status object).

fork { sleep 0.2; exit 2 }   #=> 27432
fork { sleep 0.1; exit 1 }   #=> 27433
fork {            exit 0 }   #=> 27434
p Process.waitall

produces:

[[30982, #<Process::Status: pid 30982 exit 0>],
 [30979, #<Process::Status: pid 30979 exit 1>],
 [30976, #<Process::Status: pid 30976 exit 2>]]

.waitFixnum (mod_func) .wait(pid = -1, flags = 0) ⇒ Fixnum .waitpid(pid = -1, flags = 0) ⇒ Fixnum

Alias for .wait.

.wait2(pid = -1, flags = 0) ⇒ Array, status (mod_func) .waitpid2(pid = -1, flags = 0) ⇒ Array, status

Alias for .wait2.