File: C:/Ruby27-x64/share/doc/ruby/html/Process.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>module Process - RDoc Documentation</title>
<script type="text/javascript">
var rdoc_rel_prefix = "./";
var index_rel_prefix = "./";
</script>
<script src="./js/navigation.js" defer></script>
<script src="./js/search.js" defer></script>
<script src="./js/search_index.js" defer></script>
<script src="./js/searcher.js" defer></script>
<script src="./js/darkfish.js" defer></script>
<link href="./css/fonts.css" rel="stylesheet">
<link href="./css/rdoc.css" rel="stylesheet">
<body id="top" role="document" class="module">
<nav role="navigation">
<div id="project-navigation">
<div id="home-section" role="region" title="Quick navigation" class="nav-section">
<h2>
<a href="./index.html" rel="home">Home</a>
</h2>
<div id="table-of-contents-navigation">
<a href="./table_of_contents.html#pages">Pages</a>
<a href="./table_of_contents.html#classes">Classes</a>
<a href="./table_of_contents.html#methods">Methods</a>
</div>
</div>
<div id="search-section" role="search" class="project-section initially-hidden">
<form action="#" method="get" accept-charset="utf-8">
<div id="search-field-wrapper">
<input id="search-field" role="combobox" aria-label="Search"
aria-autocomplete="list" aria-controls="search-results"
type="text" name="search" placeholder="Search" spellcheck="false"
title="Type to search, Up and Down to navigate, Enter to load">
</div>
<ul id="search-results" aria-label="Search Results"
aria-busy="false" aria-expanded="false"
aria-atomic="false" class="initially-hidden"></ul>
</form>
</div>
</div>
<div id="class-metadata">
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
<h3>Methods</h3>
<ul class="link-list" role="directory">
<li ><a href="#method-c-abort">::abort</a>
<li ><a href="#method-c-argv0">::argv0</a>
<li ><a href="#method-c-clock_getres">::clock_getres</a>
<li ><a href="#method-c-clock_gettime">::clock_gettime</a>
<li ><a href="#method-c-daemon">::daemon</a>
<li ><a href="#method-c-detach">::detach</a>
<li ><a href="#method-c-egid">::egid</a>
<li ><a href="#method-c-egid-3D">::egid=</a>
<li ><a href="#method-c-euid">::euid</a>
<li ><a href="#method-c-euid-3D">::euid=</a>
<li ><a href="#method-c-exec">::exec</a>
<li ><a href="#method-c-exit">::exit</a>
<li ><a href="#method-c-exit-21">::exit!</a>
<li ><a href="#method-c-fork">::fork</a>
<li ><a href="#method-c-getpgid">::getpgid</a>
<li ><a href="#method-c-getpgrp">::getpgrp</a>
<li ><a href="#method-c-getpriority">::getpriority</a>
<li ><a href="#method-c-getrlimit">::getrlimit</a>
<li ><a href="#method-c-getsid">::getsid</a>
<li ><a href="#method-c-gid">::gid</a>
<li ><a href="#method-c-gid-3D">::gid=</a>
<li ><a href="#method-c-groups">::groups</a>
<li ><a href="#method-c-groups-3D">::groups=</a>
<li ><a href="#method-c-initgroups">::initgroups</a>
<li ><a href="#method-c-kill">::kill</a>
<li ><a href="#method-c-last_status">::last_status</a>
<li ><a href="#method-c-maxgroups">::maxgroups</a>
<li ><a href="#method-c-maxgroups-3D">::maxgroups=</a>
<li ><a href="#method-c-pid">::pid</a>
<li ><a href="#method-c-ppid">::ppid</a>
<li ><a href="#method-c-setpgid">::setpgid</a>
<li ><a href="#method-c-setpgrp">::setpgrp</a>
<li ><a href="#method-c-setpriority">::setpriority</a>
<li ><a href="#method-c-setproctitle">::setproctitle</a>
<li ><a href="#method-c-setrlimit">::setrlimit</a>
<li ><a href="#method-c-setsid">::setsid</a>
<li ><a href="#method-c-spawn">::spawn</a>
<li ><a href="#method-c-times">::times</a>
<li ><a href="#method-c-uid">::uid</a>
<li ><a href="#method-c-uid-3D">::uid=</a>
<li ><a href="#method-c-wait">::wait</a>
<li ><a href="#method-c-wait2">::wait2</a>
<li ><a href="#method-c-waitall">::waitall</a>
<li ><a href="#method-c-waitpid">::waitpid</a>
<li ><a href="#method-c-waitpid2">::waitpid2</a>
</ul>
</div>
</div>
</nav>
<main role="main" aria-labelledby="module-Process">
<h1 id="module-Process" class="module">
module Process
</h1>
<section class="description">
<p>The module contains several groups of functionality for handling OS processes:</p>
<ul><li>
<p>Low-level property introspection and management of the current process, like <a href="Process.html#method-c-argv0"><code>Process.argv0</code></a>, <a href="Process.html#method-c-pid"><code>Process.pid</code></a>;</p>
</li><li>
<p>Low-level introspection of other processes, like <a href="Process.html#method-c-getpgid"><code>Process.getpgid</code></a>, <a href="Process.html#method-c-getpriority"><code>Process.getpriority</code></a>;</p>
</li><li>
<p>Management of the current process: <a href="Process.html#method-c-abort"><code>Process.abort</code></a>, <a href="Process.html#method-c-exit"><code>Process.exit</code></a>, <a href="Process.html#method-c-daemon"><code>Process.daemon</code></a>, etc. (for convenience, most of those are also available as global functions and module functions of <a href="Kernel.html"><code>Kernel</code></a>);</p>
</li><li>
<p>Creation and management of child processes: <a href="Process.html#method-c-fork"><code>Process.fork</code></a>, <a href="Process.html#method-c-spawn"><code>Process.spawn</code></a>, and related methods;</p>
</li><li>
<p>Management of low-level system clock: <a href="Process.html#method-c-times"><code>Process.times</code></a> and <a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a>, which could be important for proper benchmarking and other elapsed time measurement tasks.</p>
</li></ul>
</section>
<section id="5Buntitled-5D" class="documentation-section">
<section class="constants-list">
<header>
<h3>Constants</h3>
</header>
<dl>
<dt id="CLOCK_BOOTTIME">CLOCK_BOOTTIME
<dd><p>see <a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a></p>
<dt id="CLOCK_BOOTTIME_ALARM">CLOCK_BOOTTIME_ALARM
<dd><p>see <a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a></p>
<dt id="CLOCK_MONOTONIC">CLOCK_MONOTONIC
<dd><p>see <a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a></p>
<dt id="CLOCK_MONOTONIC_COARSE">CLOCK_MONOTONIC_COARSE
<dd><p>see <a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a></p>
<dt id="CLOCK_MONOTONIC_FAST">CLOCK_MONOTONIC_FAST
<dd><p>see <a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a></p>
<dt id="CLOCK_MONOTONIC_PRECISE">CLOCK_MONOTONIC_PRECISE
<dd><p>see <a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a></p>
<dt id="CLOCK_MONOTONIC_RAW">CLOCK_MONOTONIC_RAW
<dd><p>see <a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a></p>
<dt id="CLOCK_MONOTONIC_RAW_APPROX">CLOCK_MONOTONIC_RAW_APPROX
<dd><p>see <a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a></p>
<dt id="CLOCK_PROCESS_CPUTIME_ID">CLOCK_PROCESS_CPUTIME_ID
<dd><p>see <a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a></p>
<dt id="CLOCK_PROF">CLOCK_PROF
<dd><p>see <a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a></p>
<dt id="CLOCK_REALTIME">CLOCK_REALTIME
<dd><p>see <a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a></p>
<dt id="CLOCK_REALTIME_ALARM">CLOCK_REALTIME_ALARM
<dd><p>see <a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a></p>
<dt id="CLOCK_REALTIME_COARSE">CLOCK_REALTIME_COARSE
<dd><p>see <a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a></p>
<dt id="CLOCK_REALTIME_FAST">CLOCK_REALTIME_FAST
<dd><p>see <a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a></p>
<dt id="CLOCK_REALTIME_PRECISE">CLOCK_REALTIME_PRECISE
<dd><p>see <a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a></p>
<dt id="CLOCK_SECOND">CLOCK_SECOND
<dd><p>see <a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a></p>
<dt id="CLOCK_TAI">CLOCK_TAI
<dd><p>see <a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a></p>
<dt id="CLOCK_THREAD_CPUTIME_ID">CLOCK_THREAD_CPUTIME_ID
<dd><p>see <a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a></p>
<dt id="CLOCK_UPTIME">CLOCK_UPTIME
<dd><p>see <a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a></p>
<dt id="CLOCK_UPTIME_FAST">CLOCK_UPTIME_FAST
<dd><p>see <a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a></p>
<dt id="CLOCK_UPTIME_PRECISE">CLOCK_UPTIME_PRECISE
<dd><p>see <a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a></p>
<dt id="CLOCK_UPTIME_RAW">CLOCK_UPTIME_RAW
<dd><p>see <a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a></p>
<dt id="CLOCK_UPTIME_RAW_APPROX">CLOCK_UPTIME_RAW_APPROX
<dd><p>see <a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a></p>
<dt id="CLOCK_VIRTUAL">CLOCK_VIRTUAL
<dd><p>see <a href="Process.html#method-c-clock_gettime"><code>Process.clock_gettime</code></a></p>
<dt id="PRIO_PGRP">PRIO_PGRP
<dd><p>see <a href="Process.html#method-c-setpriority"><code>Process.setpriority</code></a></p>
<dt id="PRIO_PROCESS">PRIO_PROCESS
<dd><p>see <a href="Process.html#method-c-setpriority"><code>Process.setpriority</code></a></p>
<dt id="PRIO_USER">PRIO_USER
<dd><p>see <a href="Process.html#method-c-setpriority"><code>Process.setpriority</code></a></p>
<dt id="RLIMIT_AS">RLIMIT_AS
<dd><p>Maximum size of the process's virtual memory (address space) in bytes.</p>
<p>see the system getrlimit(2) manual for details.</p>
<dt id="RLIMIT_CORE">RLIMIT_CORE
<dd><p>Maximum size of the core file.</p>
<p>see the system getrlimit(2) manual for details.</p>
<dt id="RLIMIT_CPU">RLIMIT_CPU
<dd><p>CPU time limit in seconds.</p>
<p>see the system getrlimit(2) manual for details.</p>
<dt id="RLIMIT_DATA">RLIMIT_DATA
<dd><p>Maximum size of the process's data segment.</p>
<p>see the system getrlimit(2) manual for details.</p>
<dt id="RLIMIT_FSIZE">RLIMIT_FSIZE
<dd><p>Maximum size of files that the process may create.</p>
<p>see the system getrlimit(2) manual for details.</p>
<dt id="RLIMIT_MEMLOCK">RLIMIT_MEMLOCK
<dd><p>Maximum number of bytes of memory that may be locked into RAM.</p>
<p>see the system getrlimit(2) manual for details.</p>
<dt id="RLIMIT_MSGQUEUE">RLIMIT_MSGQUEUE
<dd><p>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.</p>
<p>see the system getrlimit(2) manual for details.</p>
<dt id="RLIMIT_NICE">RLIMIT_NICE
<dd><p>Specifies a ceiling to which the process's nice value can be raised.</p>
<p>see the system getrlimit(2) manual for details.</p>
<dt id="RLIMIT_NOFILE">RLIMIT_NOFILE
<dd><p>Specifies a value one greater than the maximum file descriptor number that can be opened by this process.</p>
<p>see the system getrlimit(2) manual for details.</p>
<dt id="RLIMIT_NPROC">RLIMIT_NPROC
<dd><p>The maximum number of processes that can be created for the real user ID of the calling process.</p>
<p>see the system getrlimit(2) manual for details.</p>
<dt id="RLIMIT_RSS">RLIMIT_RSS
<dd><p>Specifies the limit (in pages) of the process's resident set.</p>
<p>see the system getrlimit(2) manual for details.</p>
<dt id="RLIMIT_RTPRIO">RLIMIT_RTPRIO
<dd><p>Specifies a ceiling on the real-time priority that may be set for this process.</p>
<p>see the system getrlimit(2) manual for details.</p>
<dt id="RLIMIT_RTTIME">RLIMIT_RTTIME
<dd><p>Specifies limit on CPU time this process scheduled under a real-time scheduling policy can consume.</p>
<p>see the system getrlimit(2) manual for details.</p>
<dt id="RLIMIT_SBSIZE">RLIMIT_SBSIZE
<dd><p>Maximum size of the socket buffer.</p>
<dt id="RLIMIT_SIGPENDING">RLIMIT_SIGPENDING
<dd><p>Specifies a limit on the number of signals that may be queued for the real user ID of the calling process.</p>
<p>see the system getrlimit(2) manual for details.</p>
<dt id="RLIMIT_STACK">RLIMIT_STACK
<dd><p>Maximum size of the stack, in bytes.</p>
<p>see the system getrlimit(2) manual for details.</p>
<dt id="RLIM_INFINITY">RLIM_INFINITY
<dd><p>see <a href="Process.html#method-c-setrlimit"><code>Process.setrlimit</code></a></p>
<dt id="RLIM_SAVED_CUR">RLIM_SAVED_CUR
<dd><p>see <a href="Process.html#method-c-setrlimit"><code>Process.setrlimit</code></a></p>
<dt id="RLIM_SAVED_MAX">RLIM_SAVED_MAX
<dd><p>see <a href="Process.html#method-c-setrlimit"><code>Process.setrlimit</code></a></p>
<dt id="WNOHANG">WNOHANG
<dd><p>see <a href="Process.html#method-c-wait"><code>Process.wait</code></a></p>
<dt id="WUNTRACED">WUNTRACED
<dd><p>see <a href="Process.html#method-c-wait"><code>Process.wait</code></a></p>
</dl>
</section>
<section id="public-class-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Public Class Methods</h3>
</header>
<div id="method-c-abort" class="method-detail ">
<div class="method-heading">
<span class="method-name">abort</span><span
class="method-args">(*args)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<div class="method-source-code" id="abort-source">
<pre>static VALUE
f_abort(int c, const VALUE *a, VALUE _)
{
return rb_f_abort(c, a);
}</pre>
</div>
</div>
</div>
<div id="method-c-argv0" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
argv0 → frozen_string
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the name of the script being executed. The value is not affected by assigning a new value to $0.</p>
<p>This method first appeared in Ruby 2.1 to serve as a global variable free means to get the script name.</p>
<div class="method-source-code" id="argv0-source">
<pre>static VALUE
proc_argv0(VALUE process)
{
return rb_orig_progname;
}</pre>
</div>
</div>
</div>
<div id="method-c-clock_getres" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
clock_getres(clock_id [, unit]) → number
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the time resolution returned by POSIX <a href="Process.html#method-c-clock_getres"><code>clock_getres</code></a>() function.</p>
<p><code>clock_id</code> specifies a kind of clock. See the document of <code>Process.clock_gettime</code> for details.</p>
<p><code>clock_id</code> can be a symbol as <code>Process.clock_gettime</code>. However the result may not be accurate. For example, <code>Process.clock_getres(:GETTIMEOFDAY_BASED_CLOCK_REALTIME)</code> returns 1.0e-06 which means 1 microsecond, but actual resolution can be more coarse.</p>
<p>If the given <code>clock_id</code> is not supported, Errno::EINVAL is raised.</p>
<p><code>unit</code> specifies a type of the return value. <code>Process.clock_getres</code> accepts <code>unit</code> as <code>Process.clock_gettime</code>. The default value, <code>:float_second</code>, is also same as <code>Process.clock_gettime</code>.</p>
<p><code>Process.clock_getres</code> also accepts <code>:hertz</code> as <code>unit</code>. <code>:hertz</code> means a the reciprocal of <code>:float_second</code>.</p>
<p><code>:hertz</code> can be used to obtain the exact value of the clock ticks per second for times() function and CLOCKS_PER_SEC for clock() function.</p>
<p><code>Process.clock_getres(:TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID, :hertz)</code> returns the clock ticks per second.</p>
<p><code>Process.clock_getres(:CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID, :hertz)</code> returns CLOCKS_PER_SEC.</p>
<pre class="ruby"><span class="ruby-identifier">p</span> <span class="ruby-constant">Process</span>.<span class="ruby-identifier">clock_getres</span>(<span class="ruby-constant">Process</span><span class="ruby-operator">::</span><span class="ruby-constant">CLOCK_MONOTONIC</span>)
<span class="ruby-comment">#=> 1.0e-09</span>
</pre>
<div class="method-source-code" id="clock_getres-source">
<pre>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);
}
}</pre>
</div>
</div>
</div>
<div id="method-c-clock_gettime" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
clock_gettime(clock_id [, unit]) → number
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns a time returned by POSIX <a href="Process.html#method-c-clock_gettime"><code>clock_gettime</code></a>() function.</p>
<pre class="ruby"><span class="ruby-identifier">p</span> <span class="ruby-constant">Process</span>.<span class="ruby-identifier">clock_gettime</span>(<span class="ruby-constant">Process</span><span class="ruby-operator">::</span><span class="ruby-constant">CLOCK_MONOTONIC</span>)
<span class="ruby-comment">#=> 896053.968060096</span>
</pre>
<p><code>clock_id</code> specifies a kind of clock. It is specified as a constant which begins with <code>Process::CLOCK_</code> such as <a href="Process.html#CLOCK_REALTIME"><code>Process::CLOCK_REALTIME</code></a> and <a href="Process.html#CLOCK_MONOTONIC"><code>Process::CLOCK_MONOTONIC</code></a>.</p>
<p>The supported constants depends on OS and version. Ruby provides following types of <code>clock_id</code> if available.</p>
<dl class="rdoc-list label-list"><dt><a href="Process.html#CLOCK_REALTIME"><code>CLOCK_REALTIME</code></a>
<dd>
<p>SUSv2 to 4, Linux 2.5.63, FreeBSD 3.0, NetBSD 2.0, OpenBSD 2.1, macOS 10.12</p>
</dd><dt><a href="Process.html#CLOCK_MONOTONIC"><code>CLOCK_MONOTONIC</code></a>
<dd>
<p>SUSv3 to 4, Linux 2.5.63, FreeBSD 3.0, NetBSD 2.0, OpenBSD 3.4, macOS 10.12</p>
</dd><dt><a href="Process.html#CLOCK_PROCESS_CPUTIME_ID"><code>CLOCK_PROCESS_CPUTIME_ID</code></a>
<dd>
<p>SUSv3 to 4, Linux 2.5.63, FreeBSD 9.3, OpenBSD 5.4, macOS 10.12</p>
</dd><dt><a href="Process.html#CLOCK_THREAD_CPUTIME_ID"><code>CLOCK_THREAD_CPUTIME_ID</code></a>
<dd>
<p>SUSv3 to 4, Linux 2.5.63, FreeBSD 7.1, OpenBSD 5.4, macOS 10.12</p>
</dd><dt><a href="Process.html#CLOCK_VIRTUAL"><code>CLOCK_VIRTUAL</code></a>
<dd>
<p>FreeBSD 3.0, OpenBSD 2.1</p>
</dd><dt><a href="Process.html#CLOCK_PROF"><code>CLOCK_PROF</code></a>
<dd>
<p>FreeBSD 3.0, OpenBSD 2.1</p>
</dd><dt><a href="Process.html#CLOCK_REALTIME_FAST"><code>CLOCK_REALTIME_FAST</code></a>
<dd>
<p>FreeBSD 8.1</p>
</dd><dt><a href="Process.html#CLOCK_REALTIME_PRECISE"><code>CLOCK_REALTIME_PRECISE</code></a>
<dd>
<p>FreeBSD 8.1</p>
</dd><dt><a href="Process.html#CLOCK_REALTIME_COARSE"><code>CLOCK_REALTIME_COARSE</code></a>
<dd>
<p>Linux 2.6.32</p>
</dd><dt><a href="Process.html#CLOCK_REALTIME_ALARM"><code>CLOCK_REALTIME_ALARM</code></a>
<dd>
<p>Linux 3.0</p>
</dd><dt><a href="Process.html#CLOCK_MONOTONIC_FAST"><code>CLOCK_MONOTONIC_FAST</code></a>
<dd>
<p>FreeBSD 8.1</p>
</dd><dt><a href="Process.html#CLOCK_MONOTONIC_PRECISE"><code>CLOCK_MONOTONIC_PRECISE</code></a>
<dd>
<p>FreeBSD 8.1</p>
</dd><dt><a href="Process.html#CLOCK_MONOTONIC_COARSE"><code>CLOCK_MONOTONIC_COARSE</code></a>
<dd>
<p>Linux 2.6.32</p>
</dd><dt><a href="Process.html#CLOCK_MONOTONIC_RAW"><code>CLOCK_MONOTONIC_RAW</code></a>
<dd>
<p>Linux 2.6.28, macOS 10.12</p>
</dd><dt><a href="Process.html#CLOCK_MONOTONIC_RAW_APPROX"><code>CLOCK_MONOTONIC_RAW_APPROX</code></a>
<dd>
<p>macOS 10.12</p>
</dd><dt><a href="Process.html#CLOCK_BOOTTIME"><code>CLOCK_BOOTTIME</code></a>
<dd>
<p>Linux 2.6.39</p>
</dd><dt><a href="Process.html#CLOCK_BOOTTIME_ALARM"><code>CLOCK_BOOTTIME_ALARM</code></a>
<dd>
<p>Linux 3.0</p>
</dd><dt><a href="Process.html#CLOCK_UPTIME"><code>CLOCK_UPTIME</code></a>
<dd>
<p>FreeBSD 7.0, OpenBSD 5.5</p>
</dd><dt><a href="Process.html#CLOCK_UPTIME_FAST"><code>CLOCK_UPTIME_FAST</code></a>
<dd>
<p>FreeBSD 8.1</p>
</dd><dt><a href="Process.html#CLOCK_UPTIME_RAW"><code>CLOCK_UPTIME_RAW</code></a>
<dd>
<p>macOS 10.12</p>
</dd><dt><a href="Process.html#CLOCK_UPTIME_RAW_APPROX"><code>CLOCK_UPTIME_RAW_APPROX</code></a>
<dd>
<p>macOS 10.12</p>
</dd><dt><a href="Process.html#CLOCK_UPTIME_PRECISE"><code>CLOCK_UPTIME_PRECISE</code></a>
<dd>
<p>FreeBSD 8.1</p>
</dd><dt><a href="Process.html#CLOCK_SECOND"><code>CLOCK_SECOND</code></a>
<dd>
<p>FreeBSD 8.1</p>
</dd><dt><a href="Process.html#CLOCK_TAI"><code>CLOCK_TAI</code></a>
<dd>
<p>Linux 3.10</p>
</dd></dl>
<p>Note that SUS stands for Single Unix Specification. SUS contains POSIX and <a href="Process.html#method-c-clock_gettime"><code>clock_gettime</code></a> is defined in the POSIX part. SUS defines <a href="Process.html#CLOCK_REALTIME"><code>CLOCK_REALTIME</code></a> mandatory but <a href="Process.html#CLOCK_MONOTONIC"><code>CLOCK_MONOTONIC</code></a>, <a href="Process.html#CLOCK_PROCESS_CPUTIME_ID"><code>CLOCK_PROCESS_CPUTIME_ID</code></a> and <a href="Process.html#CLOCK_THREAD_CPUTIME_ID"><code>CLOCK_THREAD_CPUTIME_ID</code></a> are optional.</p>
<p>Also, several symbols are accepted as <code>clock_id</code>. There are emulations for <a href="Process.html#method-c-clock_gettime"><code>clock_gettime</code></a>().</p>
<p>For example, <a href="Process.html#CLOCK_REALTIME"><code>Process::CLOCK_REALTIME</code></a> is defined as <code>:GETTIMEOFDAY_BASED_CLOCK_REALTIME</code> when <a href="Process.html#method-c-clock_gettime"><code>clock_gettime</code></a>() is not available.</p>
<p>Emulations for <code>CLOCK_REALTIME</code>:</p>
<dl class="rdoc-list label-list"><dt>:GETTIMEOFDAY_BASED_CLOCK_REALTIME
<dd>
<p>Use gettimeofday() defined by SUS. (SUSv4 obsoleted it, though.) The resolution is 1 microsecond.</p>
</dd><dt>:TIME_BASED_CLOCK_REALTIME
<dd>
<p>Use time() defined by ISO C. The resolution is 1 second.</p>
</dd></dl>
<p>Emulations for <code>CLOCK_MONOTONIC</code>:</p>
<dl class="rdoc-list label-list"><dt>:MACH_ABSOLUTE_TIME_BASED_CLOCK_MONOTONIC
<dd>
<p>Use mach_absolute_time(), available on Darwin. The resolution is CPU dependent.</p>
</dd><dt>:TIMES_BASED_CLOCK_MONOTONIC
<dd>
<p>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 <a href="Process.html#method-c-clock_gettime"><code>clock_gettime</code></a>(<a href="Process.html#CLOCK_MONOTONIC"><code>CLOCK_MONOTONIC</code></a>) 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.</p>
</dd></dl>
<p>Emulations for <code>CLOCK_PROCESS_CPUTIME_ID</code>:</p>
<dl class="rdoc-list label-list"><dt>:GETRUSAGE_BASED_CLOCK_PROCESS_CPUTIME_ID
<dd>
<p>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.</p>
</dd><dt>:TIMES_BASED_CLOCK_PROCESS_CPUTIME_ID
<dd>
<p>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.</p>
</dd><dt>:CLOCK_BASED_CLOCK_PROCESS_CPUTIME_ID
<dd>
<p>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.</p>
</dd></dl>
<p>If the given <code>clock_id</code> is not supported, Errno::EINVAL is raised.</p>
<p><code>unit</code> specifies a type of the return value.</p>
<dl class="rdoc-list label-list"><dt>:float_second
<dd>
<p>number of seconds as a float (default)</p>
</dd><dt>:float_millisecond
<dd>
<p>number of milliseconds as a float</p>
</dd><dt>:float_microsecond
<dd>
<p>number of microseconds as a float</p>
</dd><dt>:second
<dd>
<p>number of seconds as an integer</p>
</dd><dt>:millisecond
<dd>
<p>number of milliseconds as an integer</p>
</dd><dt>:microsecond
<dd>
<p>number of microseconds as an integer</p>
</dd><dt>:nanosecond
<dd>
<p>number of nanoseconds as an integer</p>
</dd></dl>
<p>The underlying function, <a href="Process.html#method-c-clock_gettime"><code>clock_gettime</code></a>(), returns a number of nanoseconds. <a href="Float.html"><code>Float</code></a> object (IEEE 754 double) is not enough to represent the return value for <a href="Process.html#CLOCK_REALTIME"><code>CLOCK_REALTIME</code></a>. If the exact nanoseconds value is required, use <code>:nanoseconds</code> as the <code>unit</code>.</p>
<p>The origin (zero) of the returned value varies. For example, system start up time, process start up time, the Epoch, etc.</p>
<p>The origin in <a href="Process.html#CLOCK_REALTIME"><code>CLOCK_REALTIME</code></a> 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. <a href="Time.html#method-c-now"><code>Time.now</code></a> is recommended over <a href="Process.html#CLOCK_REALTIME"><code>CLOCK_REALTIME</code></a>.</p>
<div class="method-source-code" id="clock_gettime-source">
<pre>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);
}</pre>
</div>
</div>
</div>
<div id="method-c-daemon" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
daemon() → 0
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
daemon(nochdir=nil,noclose=nil) → 0
</span>
</div>
<div class="method-description">
<p>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::*.</p>
<div class="method-source-code" id="daemon-source">
<pre>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);
}</pre>
</div>
</div>
</div>
<div id="method-c-detach" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
detach(pid) → thread
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Some operating systems retain the status of terminated child processes until the parent collects that status (normally using some variant of <code>wait()</code>). If the parent never collects this status, the child stays around as a <em>zombie</em> process. <a href="Process.html#method-c-detach"><code>Process::detach</code></a> prevents this by setting up a separate Ruby thread whose sole job is to reap the status of the process <em>pid</em> when it terminates. Use detach only when you do not intend to explicitly wait for the child to terminate.</p>
<p>The waiting thread returns the exit status of the detached process when it terminates, so you can use <a href="Thread.html#method-i-join"><code>Thread#join</code></a> to know the result. If specified <em>pid</em> is not a valid child process ID, the thread returns <code>nil</code> immediately.</p>
<p>The waiting thread has pid method which returns the pid.</p>
<p>In this first example, we don't reap the first child process, so it appears as a zombie in the process status display.</p>
<pre class="ruby"><span class="ruby-identifier">p1</span> = <span class="ruby-identifier">fork</span> { <span class="ruby-identifier">sleep</span> <span class="ruby-value">0.1</span> }
<span class="ruby-identifier">p2</span> = <span class="ruby-identifier">fork</span> { <span class="ruby-identifier">sleep</span> <span class="ruby-value">0.2</span> }
<span class="ruby-constant">Process</span>.<span class="ruby-identifier">waitpid</span>(<span class="ruby-identifier">p2</span>)
<span class="ruby-identifier">sleep</span> <span class="ruby-value">2</span>
<span class="ruby-identifier">system</span>(<span class="ruby-node">"ps -ho pid,state -p #{p1}"</span>)
</pre>
<p><em>produces:</em></p>
<pre>27389 Z</pre>
<p>In the next example, <a href="Process.html#method-c-detach"><code>Process::detach</code></a> is used to reap the child automatically.</p>
<pre class="ruby"><span class="ruby-identifier">p1</span> = <span class="ruby-identifier">fork</span> { <span class="ruby-identifier">sleep</span> <span class="ruby-value">0.1</span> }
<span class="ruby-identifier">p2</span> = <span class="ruby-identifier">fork</span> { <span class="ruby-identifier">sleep</span> <span class="ruby-value">0.2</span> }
<span class="ruby-constant">Process</span>.<span class="ruby-identifier">detach</span>(<span class="ruby-identifier">p1</span>)
<span class="ruby-constant">Process</span>.<span class="ruby-identifier">waitpid</span>(<span class="ruby-identifier">p2</span>)
<span class="ruby-identifier">sleep</span> <span class="ruby-value">2</span>
<span class="ruby-identifier">system</span>(<span class="ruby-node">"ps -ho pid,state -p #{p1}"</span>)
</pre>
<p><em>(produces no output)</em></p>
<div class="method-source-code" id="detach-source">
<pre>static VALUE
proc_detach(VALUE obj, VALUE pid)
{
return rb_detach_process(NUM2PIDT(pid));
}</pre>
</div>
</div>
</div>
<div id="method-c-egid" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
egid → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
Process::GID.eid → integer
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
Process::Sys.geteid → integer
</span>
</div>
<div class="method-description">
<p>Returns the effective group ID for this process. Not available on all platforms.</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">egid</span> <span class="ruby-comment">#=> 500</span>
</pre>
<div class="method-source-code" id="egid-source">
<pre>static VALUE
proc_getegid(VALUE obj)
{
rb_gid_t egid = getegid();
return GIDT2NUM(egid);
}</pre>
</div>
</div>
</div>
<div id="method-c-egid-3D" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
egid = integer → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Sets the effective group ID for this process. Not available on all platforms.</p>
<div class="method-source-code" id="egid-3D-source">
<pre>static VALUE
proc_setegid(VALUE obj, VALUE egid)
{
#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
rb_gid_t gid;
#endif
check_gid_switch();
#if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
gid = OBJ2GID(egid);
#endif
#if defined(HAVE_SETRESGID)
if (setresgid(-1, gid, -1) < 0) rb_sys_fail(0);
#elif defined HAVE_SETREGID
if (setregid(-1, gid) < 0) rb_sys_fail(0);
#elif defined HAVE_SETEGID
if (setegid(gid) < 0) rb_sys_fail(0);
#elif defined HAVE_SETGID
if (gid == getgid()) {
if (setgid(gid) < 0) rb_sys_fail(0);
}
else {
rb_notimplement();
}
#else
rb_notimplement();
#endif
return egid;
}</pre>
</div>
</div>
</div>
<div id="method-c-euid" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
euid → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
Process::UID.eid → integer
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
Process::Sys.geteuid → integer
</span>
</div>
<div class="method-description">
<p>Returns the effective user ID for this process.</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">euid</span> <span class="ruby-comment">#=> 501</span>
</pre>
<div class="method-source-code" id="euid-source">
<pre>static VALUE
proc_geteuid(VALUE obj)
{
rb_uid_t euid = geteuid();
return UIDT2NUM(euid);
}</pre>
</div>
</div>
</div>
<div id="method-c-euid-3D" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
euid= user
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Sets the effective user ID for this process. Not available on all platforms.</p>
<div class="method-source-code" id="euid-3D-source">
<pre>static VALUE
proc_seteuid_m(VALUE mod, VALUE euid)
{
check_uid_switch();
proc_seteuid(OBJ2UID(euid));
return euid;
}</pre>
</div>
</div>
</div>
<div id="method-c-exec" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
exec([env,] command... [,options])
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Replaces the current process by running the given external <em>command</em>, which can take one of the following forms:</p>
<dl class="rdoc-list label-list"><dt><code>exec(commandline)</code>
<dd>
<p>command line string which is passed to the standard shell</p>
</dd><dt><code>exec(cmdname, arg1, ...)</code>
<dd>
<p>command name and one or more arguments (no shell)</p>
</dd><dt><code>exec([cmdname, argv0], arg1, ...)</code>
<dd>
<p>command name, <a href="0">argv</a> and zero or more arguments (no shell)</p>
</dd></dl>
<p>In the first form, the string is taken as a command line that is subject to shell expansion before being executed.</p>
<p>The standard shell always means <code>"/bin/sh"</code> on Unix-like systems, same as <code>ENV["RUBYSHELL"]</code> (or <code>ENV["COMSPEC"]</code> on Windows NT series), and similar.</p>
<p>If the string from the first form (<code>exec("command")</code>) follows these simple rules:</p>
<ul><li>
<p>no meta characters</p>
</li><li>
<p>no shell reserved word and no special built-in</p>
</li><li>
<p>Ruby invokes the command directly without shell</p>
</li></ul>
<p>You can force shell invocation by adding “;” to the string (because “;” is a meta character).</p>
<p>Note that this behavior is observable by pid obtained (return value of spawn() and <a href="IO.html#method-i-pid"><code>IO#pid</code></a> for <a href="IO.html#method-c-popen"><code>IO.popen</code></a>) is the pid of the invoked command, not shell.</p>
<p>In the second form (<code>exec("command1", "arg1", ...)</code>), the first is taken as a command name and the rest are passed as parameters to command with no shell expansion.</p>
<p>In the third form (<code>exec(["command", "argv0"], "arg1", ...)</code>), 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 the <code>argv[0]</code> value, which may show up in process listings.</p>
<p>In order to execute the command, one of the <code>exec(2)</code> system calls are used, so the running command may inherit some of the environment of the original program (including open file descriptors).</p>
<p>This behavior is modified by the given <code>env</code> and <code>options</code> parameters. See <a href="Process.html#method-c-spawn"><code>::spawn</code></a> for details.</p>
<p>If the command fails to execute (typically Errno::ENOENT when it was not found) a <a href="SystemCallError.html"><code>SystemCallError</code></a> exception is raised.</p>
<p>This method modifies process attributes according to given <code>options</code> before <code>exec(2)</code> system call. See <a href="Process.html#method-c-spawn"><code>::spawn</code></a> for more details about the given <code>options</code>.</p>
<p>The modified attributes may be retained when <code>exec(2)</code> system call fails.</p>
<p>For example, hard resource limits are not restorable.</p>
<p>Consider to create a child process using <a href="Process.html#method-c-spawn"><code>::spawn</code></a> or <a href="Kernel.html#method-i-system"><code>Kernel#system</code></a> if this is not acceptable.</p>
<pre class="ruby"><span class="ruby-identifier">exec</span> <span class="ruby-string">"echo *"</span> <span class="ruby-comment"># echoes list of files in current directory</span>
<span class="ruby-comment"># never get here</span>
<span class="ruby-identifier">exec</span> <span class="ruby-string">"echo"</span>, <span class="ruby-string">"*"</span> <span class="ruby-comment"># echoes an asterisk</span>
<span class="ruby-comment"># never get here</span>
</pre>
<div class="method-source-code" id="exec-source">
<pre>static VALUE
f_exec(int c, const VALUE *a, VALUE _)
{
return rb_f_exec(c, a);
}</pre>
</div>
</div>
</div>
<div id="method-c-exit" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
exit(status=true)
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
Kernel::exit(status=true)
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
Process::exit(status=true)
</span>
</div>
<div class="method-description">
<p>Initiates the termination of the Ruby script by raising the <a href="SystemExit.html"><code>SystemExit</code></a> exception. This exception may be caught. The optional parameter is used to return a status code to the invoking environment. <code>true</code> and <code>FALSE</code> of <em>status</em> means success and failure respectively. The interpretation of other integer values are system dependent.</p>
<pre class="ruby"><span class="ruby-keyword">begin</span>
<span class="ruby-identifier">exit</span>
<span class="ruby-identifier">puts</span> <span class="ruby-string">"never get here"</span>
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">SystemExit</span>
<span class="ruby-identifier">puts</span> <span class="ruby-string">"rescued a SystemExit exception"</span>
<span class="ruby-keyword">end</span>
<span class="ruby-identifier">puts</span> <span class="ruby-string">"after begin block"</span>
</pre>
<p><em>produces:</em></p>
<pre>rescued a SystemExit exception
after begin block</pre>
<p>Just prior to termination, Ruby executes any <code>at_exit</code> functions (see Kernel::at_exit) and runs any object finalizers (see <a href="ObjectSpace.html#method-c-define_finalizer"><code>ObjectSpace::define_finalizer</code></a>).</p>
<pre class="ruby"><span class="ruby-identifier">at_exit</span> { <span class="ruby-identifier">puts</span> <span class="ruby-string">"at_exit function"</span> }
<span class="ruby-constant">ObjectSpace</span>.<span class="ruby-identifier">define_finalizer</span>(<span class="ruby-string">"string"</span>, <span class="ruby-identifier">proc</span> { <span class="ruby-identifier">puts</span> <span class="ruby-string">"in finalizer"</span> })
<span class="ruby-identifier">exit</span>
</pre>
<p><em>produces:</em></p>
<pre>at_exit function
in finalizer</pre>
<div class="method-source-code" id="exit-source">
<pre>static VALUE
f_exit(int c, const VALUE *a, VALUE _)
{
return rb_f_exit(c, a);
}</pre>
</div>
</div>
</div>
<div id="method-c-exit-21" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
exit!(status=false)
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Exits the process immediately. No exit handlers are run. <em>status</em> is returned to the underlying system as the exit status.</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">exit!</span>(<span class="ruby-keyword">true</span>)
</pre>
<div class="method-source-code" id="exit-21-source">
<pre>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);
}</pre>
</div>
</div>
</div>
<div id="method-c-fork" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
fork [{ block }] → integer or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
fork [{ block }] → integer or nil
</span>
</div>
<div class="method-description">
<p>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 <code>fork</code> call returns twice, once in the parent, returning the process ID of the child, and once in the child, returning <em>nil</em>. The child process can exit using <a href="Kernel.html#method-i-exit-21"><code>Kernel.exit!</code></a> to avoid running any <code>at_exit</code> functions. The parent process should use <a href="Process.html#method-c-wait"><code>Process.wait</code></a> to collect the termination statuses of its children or use <a href="Process.html#method-c-detach"><code>Process.detach</code></a> to register disinterest in their status; otherwise, the operating system may accumulate zombie processes.</p>
<p>The thread calling fork is the only thread in the created child process. fork doesn't copy other threads.</p>
<p>If fork is not usable, Process.respond_to?(:fork) returns false.</p>
<p>Note that fork(2) is not available on some platforms like Windows and NetBSD 4. Therefore you should use spawn() instead of fork().</p>
<div class="method-source-code" id="fork-source">
<pre>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);
}
}</pre>
</div>
</div>
</div>
<div id="method-c-getpgid" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
getpgid(pid) → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the process group ID for the given process id. Not available on all platforms.</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">getpgid</span>(<span class="ruby-constant">Process</span>.<span class="ruby-identifier">ppid</span>()) <span class="ruby-comment">#=> 25527</span>
</pre>
<div class="method-source-code" id="getpgid-source">
<pre>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);
}</pre>
</div>
</div>
</div>
<div id="method-c-getpgrp" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
getpgrp → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the process group ID for this process. Not available on all platforms.</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">getpgid</span>(<span class="ruby-value">0</span>) <span class="ruby-comment">#=> 25527</span>
<span class="ruby-constant">Process</span>.<span class="ruby-identifier">getpgrp</span> <span class="ruby-comment">#=> 25527</span>
</pre>
<div class="method-source-code" id="getpgrp-source">
<pre>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
}</pre>
</div>
</div>
</div>
<div id="method-c-getpriority" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
getpriority(kind, integer) → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Gets the scheduling priority for specified process, process group, or user. <em>kind</em> indicates the kind of entity to find: one of <a href="Process.html#PRIO_PGRP"><code>Process::PRIO_PGRP</code></a>, <a href="Process.html#PRIO_USER"><code>Process::PRIO_USER</code></a>, or <a href="Process.html#PRIO_PROCESS"><code>Process::PRIO_PROCESS</code></a>. <em>integer</em> is an id indicating the particular process, process group, or user (an id of 0 means <em>current</em>). Lower priorities are more favorable for scheduling. Not available on all platforms.</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">getpriority</span>(<span class="ruby-constant">Process</span><span class="ruby-operator">::</span><span class="ruby-constant">PRIO_USER</span>, <span class="ruby-value">0</span>) <span class="ruby-comment">#=> 19</span>
<span class="ruby-constant">Process</span>.<span class="ruby-identifier">getpriority</span>(<span class="ruby-constant">Process</span><span class="ruby-operator">::</span><span class="ruby-constant">PRIO_PROCESS</span>, <span class="ruby-value">0</span>) <span class="ruby-comment">#=> 19</span>
</pre>
<div class="method-source-code" id="getpriority-source">
<pre>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);
}</pre>
</div>
</div>
</div>
<div id="method-c-getrlimit" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
getrlimit(resource) → [cur_limit, max_limit]
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Gets the resource limit of the process. <em>cur_limit</em> means current (soft) limit and <em>max_limit</em> means maximum (hard) limit.</p>
<p><em>resource</em> indicates the kind of resource to limit. It is specified as a symbol such as <code>:CORE</code>, a string such as <code>"CORE"</code> or a constant such as <a href="Process.html#RLIMIT_CORE"><code>Process::RLIMIT_CORE</code></a>. See <a href="Process.html#method-c-setrlimit"><code>Process.setrlimit</code></a> for details.</p>
<p><em>cur_limit</em> and <em>max_limit</em> may be <a href="Process.html#RLIM_INFINITY"><code>Process::RLIM_INFINITY</code></a>, <a href="Process.html#RLIM_SAVED_MAX"><code>Process::RLIM_SAVED_MAX</code></a> or <a href="Process.html#RLIM_SAVED_CUR"><code>Process::RLIM_SAVED_CUR</code></a>. See <a href="Process.html#method-c-setrlimit"><code>Process.setrlimit</code></a> and the system getrlimit(2) manual for details.</p>
<div class="method-source-code" id="getrlimit-source">
<pre>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));
}</pre>
</div>
</div>
</div>
<div id="method-c-getsid" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
getsid() → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
getsid(pid) → integer
</span>
</div>
<div class="method-description">
<p>Returns the session ID for the given process id. If not given, return current process sid. Not available on all platforms.</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">getsid</span>() <span class="ruby-comment">#=> 27422</span>
<span class="ruby-constant">Process</span>.<span class="ruby-identifier">getsid</span>(<span class="ruby-value">0</span>) <span class="ruby-comment">#=> 27422</span>
<span class="ruby-constant">Process</span>.<span class="ruby-identifier">getsid</span>(<span class="ruby-constant">Process</span>.<span class="ruby-identifier">pid</span>()) <span class="ruby-comment">#=> 27422</span>
</pre>
<div class="method-source-code" id="getsid-source">
<pre>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);
}</pre>
</div>
</div>
</div>
<div id="method-c-gid" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
gid → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
Process::GID.rid → integer
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
Process::Sys.getgid → integer
</span>
</div>
<div class="method-description">
<p>Returns the (real) group ID for this process.</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">gid</span> <span class="ruby-comment">#=> 500</span>
</pre>
<div class="method-source-code" id="gid-source">
<pre>static VALUE
proc_getgid(VALUE obj)
{
rb_gid_t gid = getgid();
return GIDT2NUM(gid);
}</pre>
</div>
</div>
</div>
<div id="method-c-gid-3D" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
gid= integer → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Sets the group ID for this process.</p>
<div class="method-source-code" id="gid-3D-source">
<pre>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);
}</pre>
</div>
</div>
</div>
<div id="method-c-groups" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
groups → array
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Get an <a href="Array.html"><code>Array</code></a> of the group IDs in the supplemental group access list for this process.</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">groups</span> <span class="ruby-comment">#=> [27, 6, 10, 11]</span>
</pre>
<p>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:</p>
<ul><li>
<p>the result is sorted</p>
</li><li>
<p>the result includes effective GIDs</p>
</li><li>
<p>the result does not include duplicated GIDs</p>
</li></ul>
<p>You can make sure to get a sorted unique <a href="Process/GID.html"><code>GID</code></a> list of the current process by this expression:</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">groups</span>.<span class="ruby-identifier">uniq</span>.<span class="ruby-identifier">sort</span>
</pre>
<div class="method-source-code" id="groups-source">
<pre>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;
}</pre>
</div>
</div>
</div>
<div id="method-c-groups-3D" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
groups= array → array
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p><a href="Set.html"><code>Set</code></a> the supplemental group access list to the given <a href="Array.html"><code>Array</code></a> of group IDs.</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">groups</span> <span class="ruby-comment">#=> [0, 1, 2, 3, 4, 6, 10, 11, 20, 26, 27]</span>
<span class="ruby-constant">Process</span>.<span class="ruby-identifier">groups</span> = [<span class="ruby-value">27</span>, <span class="ruby-value">6</span>, <span class="ruby-value">10</span>, <span class="ruby-value">11</span>] <span class="ruby-comment">#=> [27, 6, 10, 11]</span>
<span class="ruby-constant">Process</span>.<span class="ruby-identifier">groups</span> <span class="ruby-comment">#=> [27, 6, 10, 11]</span>
</pre>
<div class="method-source-code" id="groups-3D-source">
<pre>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);
}</pre>
</div>
</div>
</div>
<div id="method-c-initgroups" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
initgroups(username, gid) → array
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>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 <em>gid</em> is also added to the list. Returns the resulting <a href="Array.html"><code>Array</code></a> of the gids of all the groups in the supplementary group access list. Not available on all platforms.</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">groups</span> <span class="ruby-comment">#=> [0, 1, 2, 3, 4, 6, 10, 11, 20, 26, 27]</span>
<span class="ruby-constant">Process</span>.<span class="ruby-identifier">initgroups</span>( <span class="ruby-string">"mgranger"</span>, <span class="ruby-value">30</span> ) <span class="ruby-comment">#=> [30, 6, 10, 11]</span>
<span class="ruby-constant">Process</span>.<span class="ruby-identifier">groups</span> <span class="ruby-comment">#=> [30, 6, 10, 11]</span>
</pre>
<div class="method-source-code" id="initgroups-source">
<pre>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);
}</pre>
</div>
</div>
</div>
<div id="method-c-kill" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
kill(signal, pid, ...) → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Sends the given signal to the specified process id(s) if <em>pid</em> is positive. If <em>pid</em> is zero, <em>signal</em> is sent to all processes whose group ID is equal to the group ID of the process. If <em>pid</em> is negative, results are dependent on the operating system. <em>signal</em> may be an integer signal number or a POSIX signal name (either with or without a <code>SIG</code> prefix). If <em>signal</em> 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 <a href="Signal.html#method-c-list"><code>Signal.list</code></a> are known signal names and numbers, respectively.</p>
<pre class="ruby"><span class="ruby-identifier">pid</span> = <span class="ruby-identifier">fork</span> <span class="ruby-keyword">do</span>
<span class="ruby-constant">Signal</span>.<span class="ruby-identifier">trap</span>(<span class="ruby-string">"HUP"</span>) { <span class="ruby-identifier">puts</span> <span class="ruby-string">"Ouch!"</span>; <span class="ruby-identifier">exit</span> }
<span class="ruby-comment"># ... do some work ...</span>
<span class="ruby-keyword">end</span>
<span class="ruby-comment"># ...</span>
<span class="ruby-constant">Process</span>.<span class="ruby-identifier">kill</span>(<span class="ruby-string">"HUP"</span>, <span class="ruby-identifier">pid</span>)
<span class="ruby-constant">Process</span>.<span class="ruby-identifier">wait</span>
</pre>
<p><em>produces:</em></p>
<pre class="ruby"><span class="ruby-identifier">Ouch!</span>
</pre>
<p>If <em>signal</em> is an integer but wrong for signal, Errno::EINVAL or <a href="RangeError.html"><code>RangeError</code></a> will be raised. Otherwise unless <em>signal</em> is a <a href="String.html"><code>String</code></a> or a <a href="Symbol.html"><code>Symbol</code></a>, and a known signal name, <a href="ArgumentError.html"><code>ArgumentError</code></a> will be raised.</p>
<p>Also, Errno::ESRCH or <a href="RangeError.html"><code>RangeError</code></a> for invalid <em>pid</em>, Errno::EPERM when failed because of no privilege, will be raised. In these cases, signals may have been sent to preceding processes.</p>
<div class="method-source-code" id="kill-source">
<pre>static VALUE
proc_rb_f_kill(int c, const VALUE *v, VALUE _)
{
return rb_f_kill(c, v);
}</pre>
</div>
</div>
</div>
<div id="method-c-last_status" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
last_status → Process::Status or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the status of the last executed child process in the current thread.</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">wait</span> <span class="ruby-constant">Process</span>.<span class="ruby-identifier">spawn</span>(<span class="ruby-string">"ruby"</span>, <span class="ruby-string">"-e"</span>, <span class="ruby-string">"exit 13"</span>)
<span class="ruby-constant">Process</span>.<span class="ruby-identifier">last_status</span> <span class="ruby-comment">#=> #<Process::Status: pid 4825 exit 13></span>
</pre>
<p>If no child process has ever been executed in the current thread, this returns <code>nil</code>.</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">last_status</span> <span class="ruby-comment">#=> nil</span>
</pre>
<div class="method-source-code" id="last_status-source">
<pre>static VALUE
proc_s_last_status(VALUE mod)
{
return rb_last_status_get();
}</pre>
</div>
</div>
</div>
<div id="method-c-maxgroups" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
maxgroups → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the maximum number of gids allowed in the supplemental group access list.</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">maxgroups</span> <span class="ruby-comment">#=> 32</span>
</pre>
<div class="method-source-code" id="maxgroups-source">
<pre>static VALUE
proc_getmaxgroups(VALUE obj)
{
return INT2FIX(maxgroups());
}</pre>
</div>
</div>
</div>
<div id="method-c-maxgroups-3D" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
maxgroups= integer → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Sets the maximum number of gids allowed in the supplemental group access list.</p>
<div class="method-source-code" id="maxgroups-3D-source">
<pre>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);
}</pre>
</div>
</div>
</div>
<div id="method-c-pid" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
pid → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the process id of this process. Not available on all platforms.</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">pid</span> <span class="ruby-comment">#=> 27415</span>
</pre>
<div class="method-source-code" id="pid-source">
<pre>static VALUE
proc_get_pid(VALUE _)
{
return get_pid();
}</pre>
</div>
</div>
</div>
<div id="method-c-ppid" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
ppid → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the process id of the parent of this process. Returns untrustworthy value on Win32/64. Not available on all platforms.</p>
<pre class="ruby"><span class="ruby-identifier">puts</span> <span class="ruby-node">"I am #{Process.pid}"</span>
<span class="ruby-constant">Process</span>.<span class="ruby-identifier">fork</span> { <span class="ruby-identifier">puts</span> <span class="ruby-node">"Dad is #{Process.ppid}"</span> }
</pre>
<p><em>produces:</em></p>
<pre class="ruby"><span class="ruby-constant">I</span> <span class="ruby-identifier">am</span> <span class="ruby-value">27417</span>
<span class="ruby-constant">Dad</span> <span class="ruby-identifier">is</span> <span class="ruby-value">27417</span>
</pre>
<div class="method-source-code" id="ppid-source">
<pre>static VALUE
proc_get_ppid(VALUE _)
{
return get_ppid();
}</pre>
</div>
</div>
</div>
<div id="method-c-setpgid" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
setpgid(pid, integer) → 0
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Sets the process group ID of <em>pid</em> (0 indicates this process) to <em>integer</em>. Not available on all platforms.</p>
<div class="method-source-code" id="setpgid-source">
<pre>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);
}</pre>
</div>
</div>
</div>
<div id="method-c-setpgrp" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
setpgrp → 0
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Equivalent to <code>setpgid(0,0)</code>. Not available on all platforms.</p>
<div class="method-source-code" id="setpgrp-source">
<pre>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);
}</pre>
</div>
</div>
</div>
<div id="method-c-setpriority" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
setpriority(kind, integer, priority) → 0
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>See <a href="Process.html#method-c-getpriority"><code>Process.getpriority</code></a>.</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">setpriority</span>(<span class="ruby-constant">Process</span><span class="ruby-operator">::</span><span class="ruby-constant">PRIO_USER</span>, <span class="ruby-value">0</span>, <span class="ruby-value">19</span>) <span class="ruby-comment">#=> 0</span>
<span class="ruby-constant">Process</span>.<span class="ruby-identifier">setpriority</span>(<span class="ruby-constant">Process</span><span class="ruby-operator">::</span><span class="ruby-constant">PRIO_PROCESS</span>, <span class="ruby-value">0</span>, <span class="ruby-value">19</span>) <span class="ruby-comment">#=> 0</span>
<span class="ruby-constant">Process</span>.<span class="ruby-identifier">getpriority</span>(<span class="ruby-constant">Process</span><span class="ruby-operator">::</span><span class="ruby-constant">PRIO_USER</span>, <span class="ruby-value">0</span>) <span class="ruby-comment">#=> 19</span>
<span class="ruby-constant">Process</span>.<span class="ruby-identifier">getpriority</span>(<span class="ruby-constant">Process</span><span class="ruby-operator">::</span><span class="ruby-constant">PRIO_PROCESS</span>, <span class="ruby-value">0</span>) <span class="ruby-comment">#=> 19</span>
</pre>
<div class="method-source-code" id="setpriority-source">
<pre>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);
}</pre>
</div>
</div>
</div>
<div id="method-c-setproctitle" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
setproctitle(string) → string
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>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 <a href="NotImplementedError.html"><code>NotImplementedError</code></a> be raised even if the platform does not support the feature.</p>
<p>Calling this method does not affect the value of $0.</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">setproctitle</span>(<span class="ruby-string">'myapp: worker #%d'</span> <span class="ruby-operator">%</span> <span class="ruby-identifier">worker_id</span>)
</pre>
<p>This method first appeared in Ruby 2.1 to serve as a global variable free means to change the process title.</p>
<div class="method-source-code" id="setproctitle-source">
<pre>static VALUE
proc_setproctitle(VALUE process, VALUE title)
{
return ruby_setproctitle(title);
}</pre>
</div>
</div>
</div>
<div id="method-c-setrlimit" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
setrlimit(resource, cur_limit, max_limit) → nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
setrlimit(resource, cur_limit) → nil
</span>
</div>
<div class="method-description">
<p>Sets the resource limit of the process. <em>cur_limit</em> means current (soft) limit and <em>max_limit</em> means maximum (hard) limit.</p>
<p>If <em>max_limit</em> is not given, <em>cur_limit</em> is used.</p>
<p><em>resource</em> indicates the kind of resource to limit. It should be a symbol such as <code>:CORE</code>, a string such as <code>"CORE"</code> or a constant such as <a href="Process.html#RLIMIT_CORE"><code>Process::RLIMIT_CORE</code></a>. The available resources are OS dependent. Ruby may support following resources.</p>
<dl class="rdoc-list label-list"><dt>AS
<dd>
<p>total available memory (bytes) (SUSv3, NetBSD, FreeBSD, OpenBSD but 4.4BSD-Lite)</p>
</dd><dt>CORE
<dd>
<p>core size (bytes) (SUSv3)</p>
</dd><dt>CPU
<dd>
<p>CPU time (seconds) (SUSv3)</p>
</dd><dt>DATA
<dd>
<p>data segment (bytes) (SUSv3)</p>
</dd><dt>FSIZE
<dd>
<p>file size (bytes) (SUSv3)</p>
</dd><dt>MEMLOCK
<dd>
<p>total size for mlock(2) (bytes) (4.4BSD, GNU/Linux)</p>
</dd><dt>MSGQUEUE
<dd>
<p>allocation for POSIX message queues (bytes) (GNU/Linux)</p>
</dd><dt>NICE
<dd>
<p>ceiling on process's nice(2) value (number) (GNU/Linux)</p>
</dd><dt>NOFILE
<dd>
<p>file descriptors (number) (SUSv3)</p>
</dd><dt>NPROC
<dd>
<p>number of processes for the user (number) (4.4BSD, GNU/Linux)</p>
</dd><dt><a href="RSS.html"><code>RSS</code></a>
<dd>
<p>resident memory size (bytes) (4.2BSD, GNU/Linux)</p>
</dd><dt>RTPRIO
<dd>
<p>ceiling on the process's real-time priority (number) (GNU/Linux)</p>
</dd><dt>RTTIME
<dd>
<p>CPU time for real-time process (us) (GNU/Linux)</p>
</dd><dt>SBSIZE
<dd>
<p>all socket buffers (bytes) (NetBSD, FreeBSD)</p>
</dd><dt>SIGPENDING
<dd>
<p>number of queued signals allowed (signals) (GNU/Linux)</p>
</dd><dt>STACK
<dd>
<p>stack size (bytes) (SUSv3)</p>
</dd></dl>
<p><em>cur_limit</em> and <em>max_limit</em> may be <code>:INFINITY</code>, <code>"INFINITY"</code> or <a href="Process.html#RLIM_INFINITY"><code>Process::RLIM_INFINITY</code></a>, which means that the resource is not limited. They may be <a href="Process.html#RLIM_SAVED_MAX"><code>Process::RLIM_SAVED_MAX</code></a>, <a href="Process.html#RLIM_SAVED_CUR"><code>Process::RLIM_SAVED_CUR</code></a> and corresponding symbols and strings too. See system setrlimit(2) manual for details.</p>
<p>The following example raises the soft limit of core size to the hard limit to try to make core dump possible.</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">setrlimit</span>(<span class="ruby-value">:CORE</span>, <span class="ruby-constant">Process</span>.<span class="ruby-identifier">getrlimit</span>(<span class="ruby-value">:CORE</span>)[<span class="ruby-value">1</span>])
</pre>
<div class="method-source-code" id="setrlimit-source">
<pre>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;
}</pre>
</div>
</div>
</div>
<div id="method-c-setsid" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
setsid → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Establishes this process as a new session and process group leader, with no controlling tty. Returns the session id. Not available on all platforms.</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">setsid</span> <span class="ruby-comment">#=> 27422</span>
</pre>
<div class="method-source-code" id="setsid-source">
<pre>static VALUE
proc_setsid(VALUE _)
{
rb_pid_t pid;
pid = setsid();
if (pid < 0) rb_sys_fail(0);
return PIDT2NUM(pid);
}</pre>
</div>
</div>
</div>
<div id="method-c-spawn" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
spawn([env,] command... [,options]) → pid
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
spawn([env,] command... [,options]) → pid
</span>
</div>
<div class="method-description">
<p>spawn executes specified command and return its pid.</p>
<pre class="ruby"><span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-string">"tar xf ruby-2.0.0-p195.tar.bz2"</span>)
<span class="ruby-constant">Process</span>.<span class="ruby-identifier">wait</span> <span class="ruby-identifier">pid</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-constant">RbConfig</span>.<span class="ruby-identifier">ruby</span>, <span class="ruby-string">"-eputs'Hello, world!'"</span>)
<span class="ruby-constant">Process</span>.<span class="ruby-identifier">wait</span> <span class="ruby-identifier">pid</span>
</pre>
<p>This method is similar to <a href="Kernel.html#method-i-system"><code>Kernel#system</code></a> but it doesn't wait for the command to finish.</p>
<p>The parent process should use <a href="Process.html#method-c-wait"><code>Process.wait</code></a> to collect the termination status of its child or use <a href="Process.html#method-c-detach"><code>Process.detach</code></a> to register disinterest in their status; otherwise, the operating system may accumulate zombie processes.</p>
<p>spawn has bunch of options to specify process attributes:</p>
<pre>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</pre>
<p>The <code>cmdname, arg1, ...</code> 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 <code>Process.spawn 'echo', '%Path%'</code> will display the contents of the <code>%Path%</code> environment variable on Windows, but <code>Process.spawn 'echo', '$PATH'</code> prints the literal <code>$PATH</code>.</p>
<p>If a hash is given as <code>env</code>, the environment is updated by <code>env</code> before <code>exec(2)</code> in the child process. If a pair in <code>env</code> has nil as the value, the variable is deleted.</p>
<pre class="ruby"><span class="ruby-comment"># set FOO as BAR and unset BAZ.</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>({<span class="ruby-string">"FOO"</span><span class="ruby-operator">=></span><span class="ruby-string">"BAR"</span>, <span class="ruby-string">"BAZ"</span><span class="ruby-operator">=></span><span class="ruby-keyword">nil</span>}, <span class="ruby-identifier">command</span>)
</pre>
<p>If a hash is given as <code>options</code>, 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.</p>
<p>The <code>:unsetenv_others</code> key in <code>options</code> specifies to clear environment variables, other than specified by <code>env</code>.</p>
<pre class="ruby"><span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:unsetenv_others</span><span class="ruby-operator">=></span><span class="ruby-keyword">true</span>) <span class="ruby-comment"># no environment variable</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>({<span class="ruby-string">"FOO"</span><span class="ruby-operator">=></span><span class="ruby-string">"BAR"</span>}, <span class="ruby-identifier">command</span>, <span class="ruby-value">:unsetenv_others</span><span class="ruby-operator">=></span><span class="ruby-keyword">true</span>) <span class="ruby-comment"># FOO only</span>
</pre>
<p>The <code>:pgroup</code> key in <code>options</code> 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.</p>
<pre class="ruby"><span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:pgroup</span><span class="ruby-operator">=></span><span class="ruby-keyword">true</span>) <span class="ruby-comment"># process leader</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:pgroup</span><span class="ruby-operator">=></span><span class="ruby-value">10</span>) <span class="ruby-comment"># belongs to the process group 10</span>
</pre>
<p>The <code>:new_pgroup</code> key in <code>options</code> specifies to pass <code>CREATE_NEW_PROCESS_GROUP</code> flag to <code>CreateProcessW()</code> 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 <code>Process.kill(:SIGINT, pid)</code> on the subprocess. :new_pgroup is false by default.</p>
<pre class="ruby"><span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:new_pgroup</span><span class="ruby-operator">=></span><span class="ruby-keyword">true</span>) <span class="ruby-comment"># new process group</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:new_pgroup</span><span class="ruby-operator">=></span><span class="ruby-keyword">false</span>) <span class="ruby-comment"># same process group</span>
</pre>
<p>The <code>:rlimit_</code><em>foo</em> key specifies a resource limit. <em>foo</em> should be one of resource types such as <code>core</code>. 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 <a href="Process.html#method-c-setrlimit"><code>Process.setrlimit</code></a>.</p>
<pre class="ruby"><span class="ruby-identifier">cur</span>, <span class="ruby-identifier">max</span> = <span class="ruby-constant">Process</span>.<span class="ruby-identifier">getrlimit</span>(<span class="ruby-value">:CORE</span>)
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:rlimit_core</span><span class="ruby-operator">=></span>[<span class="ruby-value">0</span>,<span class="ruby-identifier">max</span>]) <span class="ruby-comment"># disable core temporary.</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:rlimit_core</span><span class="ruby-operator">=></span><span class="ruby-identifier">max</span>) <span class="ruby-comment"># enable core dump</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:rlimit_core</span><span class="ruby-operator">=></span><span class="ruby-value">0</span>) <span class="ruby-comment"># never dump core.</span>
</pre>
<p>The <code>:umask</code> key in <code>options</code> specifies the umask.</p>
<pre class="ruby"><span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:umask</span><span class="ruby-operator">=></span><span class="ruby-value">077</span>)
</pre>
<p>The :in, :out, :err, an integer, an <a href="IO.html"><code>IO</code></a> and an array key specifies a redirection. The redirection maps a file descriptor in the child process.</p>
<p>For example, stderr can be merged into stdout as follows:</p>
<pre class="ruby"><span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:err</span><span class="ruby-operator">=></span><span class="ruby-value">:out</span>)
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">2</span><span class="ruby-operator">=></span><span class="ruby-value">1</span>)
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-constant">STDERR</span><span class="ruby-operator">=></span><span class="ruby-value">:out</span>)
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-constant">STDERR</span><span class="ruby-operator">=></span><span class="ruby-constant">STDOUT</span>)
</pre>
<p>The hash keys specifies a file descriptor in the child process started by spawn. :err, 2 and STDERR specifies the standard error stream (stderr).</p>
<p>The hash values specifies a file descriptor in the parent process which invokes spawn. :out, 1 and STDOUT specifies the standard output stream (stdout).</p>
<p>In the above example, the standard output in the child process is not specified. So it is inherited from the parent process.</p>
<p>The standard input stream (stdin) can be specified by :in, 0 and STDIN.</p>
<p>A filename can be specified as a hash value.</p>
<pre class="ruby"><span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:in</span><span class="ruby-operator">=></span><span class="ruby-string">"/dev/null"</span>) <span class="ruby-comment"># read mode</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:out</span><span class="ruby-operator">=></span><span class="ruby-string">"/dev/null"</span>) <span class="ruby-comment"># write mode</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:err</span><span class="ruby-operator">=></span><span class="ruby-string">"log"</span>) <span class="ruby-comment"># write mode</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, [<span class="ruby-value">:out</span>, <span class="ruby-value">:err</span>]<span class="ruby-operator">=></span><span class="ruby-string">"/dev/null"</span>) <span class="ruby-comment"># write mode</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">3</span><span class="ruby-operator">=></span><span class="ruby-string">"/dev/null"</span>) <span class="ruby-comment"># read mode</span>
</pre>
<p>For stdout and stderr (and combination of them), it is opened in write mode. Otherwise read mode is used.</p>
<p>For specifying flags and permission of file creation explicitly, an array is used instead.</p>
<pre class="ruby"><span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:in</span><span class="ruby-operator">=></span>[<span class="ruby-string">"file"</span>]) <span class="ruby-comment"># read mode is assumed</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:in</span><span class="ruby-operator">=></span>[<span class="ruby-string">"file"</span>, <span class="ruby-string">"r"</span>])
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:out</span><span class="ruby-operator">=></span>[<span class="ruby-string">"log"</span>, <span class="ruby-string">"w"</span>]) <span class="ruby-comment"># 0644 assumed</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:out</span><span class="ruby-operator">=></span>[<span class="ruby-string">"log"</span>, <span class="ruby-string">"w"</span>, <span class="ruby-value">0600</span>])
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:out</span><span class="ruby-operator">=></span>[<span class="ruby-string">"log"</span>, <span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">WRONLY</span><span class="ruby-operator">|</span><span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">EXCL</span><span class="ruby-operator">|</span><span class="ruby-constant">File</span><span class="ruby-operator">::</span><span class="ruby-constant">CREAT</span>, <span class="ruby-value">0600</span>])
</pre>
<p>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.</p>
<p>If an array of IOs and integers are specified as a hash key, all the elements are redirected.</p>
<pre class="ruby"><span class="ruby-comment"># stdout and stderr is redirected to log file.</span>
<span class="ruby-comment"># The file "log" is opened just once.</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, [<span class="ruby-value">:out</span>, <span class="ruby-value">:err</span>]<span class="ruby-operator">=></span>[<span class="ruby-string">"log"</span>, <span class="ruby-string">"w"</span>])
</pre>
<p>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.</p>
<pre class="ruby"><span class="ruby-comment"># stdout and stderr is redirected to log file.</span>
<span class="ruby-comment"># The file "log" is opened just once.</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:out</span><span class="ruby-operator">=></span>[<span class="ruby-string">"log"</span>, <span class="ruby-string">"w"</span>], <span class="ruby-value">:err</span><span class="ruby-operator">=></span>[<span class="ruby-value">:child</span>, <span class="ruby-value">:out</span>])
</pre>
<p>[:child, :out] can be used to merge stderr into stdout in <a href="IO.html#method-c-popen"><code>IO.popen</code></a>. In this case, <a href="IO.html#method-c-popen"><code>IO.popen</code></a> redirects stdout to a pipe in the child process and [:child, :out] refers the redirected stdout.</p>
<pre class="ruby"><span class="ruby-identifier">io</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">popen</span>([<span class="ruby-string">"sh"</span>, <span class="ruby-string">"-c"</span>, <span class="ruby-string">"echo out; echo err >&2"</span>, <span class="ruby-value">:err</span><span class="ruby-operator">=></span>[<span class="ruby-value">:child</span>, <span class="ruby-value">:out</span>]])
<span class="ruby-identifier">p</span> <span class="ruby-identifier">io</span>.<span class="ruby-identifier">read</span> <span class="ruby-comment">#=> "out\nerr\n"</span>
</pre>
<p>The <code>:chdir</code> key in <code>options</code> specifies the current directory.</p>
<pre class="ruby"><span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:chdir</span><span class="ruby-operator">=></span><span class="ruby-string">"/var/tmp"</span>)
</pre>
<p>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.</p>
<pre class="ruby"><span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:close_others</span><span class="ruby-operator">=></span><span class="ruby-keyword">true</span>) <span class="ruby-comment"># close 3,4,5,... (default)</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:close_others</span><span class="ruby-operator">=></span><span class="ruby-keyword">false</span>) <span class="ruby-comment"># don't close 3,4,5,...</span>
</pre>
<p>:close_others is false by default for spawn and <a href="IO.html#method-c-popen"><code>IO.popen</code></a>.</p>
<p>Note that fds which close-on-exec flag is already set are closed regardless of :close_others option.</p>
<p>So <a href="IO.html#method-c-pipe"><code>IO.pipe</code></a> and spawn can be used as <a href="IO.html#method-c-popen"><code>IO.popen</code></a>.</p>
<pre class="ruby"><span class="ruby-comment"># similar to r = IO.popen(command)</span>
<span class="ruby-identifier">r</span>, <span class="ruby-identifier">w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:out</span><span class="ruby-operator">=></span><span class="ruby-identifier">w</span>) <span class="ruby-comment"># r, w is closed in the child process.</span>
<span class="ruby-identifier">w</span>.<span class="ruby-identifier">close</span>
</pre>
<p>:close is specified as a hash value to close a fd individually.</p>
<pre class="ruby"><span class="ruby-identifier">f</span> = <span class="ruby-identifier">open</span>(<span class="ruby-identifier">foo</span>)
<span class="ruby-identifier">system</span>(<span class="ruby-identifier">command</span>, <span class="ruby-identifier">f</span><span class="ruby-operator">=></span><span class="ruby-value">:close</span>) <span class="ruby-comment"># don't inherit f.</span>
</pre>
<p>If a file descriptor need to be inherited, io=>io can be used.</p>
<pre class="ruby"><span class="ruby-comment"># valgrind has --log-fd option for log destination.</span>
<span class="ruby-comment"># log_w=>log_w indicates log_w.fileno inherits to child process.</span>
<span class="ruby-identifier">log_r</span>, <span class="ruby-identifier">log_w</span> = <span class="ruby-constant">IO</span>.<span class="ruby-identifier">pipe</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-string">"valgrind"</span>, <span class="ruby-node">"--log-fd=#{log_w.fileno}"</span>, <span class="ruby-string">"echo"</span>, <span class="ruby-string">"a"</span>, <span class="ruby-identifier">log_w</span><span class="ruby-operator">=></span><span class="ruby-identifier">log_w</span>)
<span class="ruby-identifier">log_w</span>.<span class="ruby-identifier">close</span>
<span class="ruby-identifier">p</span> <span class="ruby-identifier">log_r</span>.<span class="ruby-identifier">read</span>
</pre>
<p>It is also possible to exchange file descriptors.</p>
<pre class="ruby"><span class="ruby-identifier">pid</span> = <span class="ruby-identifier">spawn</span>(<span class="ruby-identifier">command</span>, <span class="ruby-value">:out</span><span class="ruby-operator">=></span><span class="ruby-value">:err</span>, <span class="ruby-value">:err</span><span class="ruby-operator">=></span><span class="ruby-value">:out</span>)
</pre>
<p>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, <code>spawn</code> uses an extra file descriptor to resolve such cyclic file descriptor mapping.</p>
<p>See <a href="Kernel.html#method-i-exec"><code>Kernel.exec</code></a> for the standard shell.</p>
<div class="method-source-code" id="spawn-source">
<pre>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
}</pre>
</div>
</div>
</div>
<div id="method-c-times" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
times → aProcessTms
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns a <code>Tms</code> structure (see Process::Tms) that contains user and system CPU times for this process, and also for children processes.</p>
<pre class="ruby"><span class="ruby-identifier">t</span> = <span class="ruby-constant">Process</span>.<span class="ruby-identifier">times</span>
[ <span class="ruby-identifier">t</span>.<span class="ruby-identifier">utime</span>, <span class="ruby-identifier">t</span>.<span class="ruby-identifier">stime</span>, <span class="ruby-identifier">t</span>.<span class="ruby-identifier">cutime</span>, <span class="ruby-identifier">t</span>.<span class="ruby-identifier">cstime</span> ] <span class="ruby-comment">#=> [0.0, 0.02, 0.00, 0.00]</span>
</pre>
<div class="method-source-code" id="times-source">
<pre>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;
}</pre>
</div>
</div>
</div>
<div id="method-c-uid" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
uid → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
Process::UID.rid → integer
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
Process::Sys.getuid → integer
</span>
</div>
<div class="method-description">
<p>Returns the (real) user ID of this process.</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">uid</span> <span class="ruby-comment">#=> 501</span>
</pre>
<div class="method-source-code" id="uid-source">
<pre>static VALUE
proc_getuid(VALUE obj)
{
rb_uid_t uid = getuid();
return UIDT2NUM(uid);
}</pre>
</div>
</div>
</div>
<div id="method-c-uid-3D" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
uid= user → numeric
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Sets the (user) user ID for this process. Not available on all platforms.</p>
<div class="method-source-code" id="uid-3D-source">
<pre>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;
}</pre>
</div>
</div>
</div>
<div id="method-c-wait" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
wait() → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
wait(pid=-1, flags=0) → integer
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
waitpid(pid=-1, flags=0) → integer
</span>
</div>
<div class="method-description">
<p>Waits for a child process to exit, returns its process id, and sets <code>$?</code> to a <a href="Process/Status.html"><code>Process::Status</code></a> object containing information on that process. Which child it waits on depends on the value of <em>pid</em>:</p>
<dl class="rdoc-list note-list"><dt>> 0
<dd>
<p>Waits for the child whose process ID equals <em>pid</em>.</p>
</dd><dt>0
<dd>
<p>Waits for any child whose process group ID equals that of the calling process.</p>
</dd><dt>-1
<dd>
<p>Waits for any child process (the default if no <em>pid</em> is given).</p>
</dd><dt>< -1
<dd>
<p>Waits for any child whose process group ID equals the absolute value of <em>pid</em>.</p>
</dd></dl>
<p>The <em>flags</em> argument may be a logical or of the flag values <a href="Process.html#WNOHANG"><code>Process::WNOHANG</code></a> (do not block if no child available) or <a href="Process.html#WUNTRACED"><code>Process::WUNTRACED</code></a> (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.</p>
<p>Calling this method raises a <a href="SystemCallError.html"><code>SystemCallError</code></a> if there are no child processes. Not available on all platforms.</p>
<pre class="ruby"><span class="ruby-identifier">include</span> <span class="ruby-constant">Process</span>
<span class="ruby-identifier">fork</span> { <span class="ruby-identifier">exit</span> <span class="ruby-value">99</span> } <span class="ruby-comment">#=> 27429</span>
<span class="ruby-identifier">wait</span> <span class="ruby-comment">#=> 27429</span>
<span class="ruby-identifier">$?</span>.<span class="ruby-identifier">exitstatus</span> <span class="ruby-comment">#=> 99</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">fork</span> { <span class="ruby-identifier">sleep</span> <span class="ruby-value">3</span> } <span class="ruby-comment">#=> 27440</span>
<span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span> <span class="ruby-comment">#=> 2008-03-08 19:56:16 +0900</span>
<span class="ruby-identifier">waitpid</span>(<span class="ruby-identifier">pid</span>, <span class="ruby-constant">Process</span><span class="ruby-operator">::</span><span class="ruby-constant">WNOHANG</span>) <span class="ruby-comment">#=> nil</span>
<span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span> <span class="ruby-comment">#=> 2008-03-08 19:56:16 +0900</span>
<span class="ruby-identifier">waitpid</span>(<span class="ruby-identifier">pid</span>, <span class="ruby-value">0</span>) <span class="ruby-comment">#=> 27440</span>
<span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span> <span class="ruby-comment">#=> 2008-03-08 19:56:19 +0900</span>
</pre>
<div class="method-source-code" id="wait-source">
<pre>static VALUE
proc_m_wait(int c, VALUE *v, VALUE _)
{
return proc_wait(c, v);
}</pre>
</div>
</div>
</div>
<div id="method-c-wait2" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
wait2(pid=-1, flags=0) → [pid, status]
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Waits for a child process to exit (see <a href="Process.html#method-c-waitpid"><code>Process::waitpid</code></a> for exact semantics) and returns an array containing the process id and the exit status (a <a href="Process/Status.html"><code>Process::Status</code></a> object) of that child. Raises a <a href="SystemCallError.html"><code>SystemCallError</code></a> if there are no child processes.</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">fork</span> { <span class="ruby-identifier">exit</span> <span class="ruby-value">99</span> } <span class="ruby-comment">#=> 27437</span>
<span class="ruby-identifier">pid</span>, <span class="ruby-identifier">status</span> = <span class="ruby-constant">Process</span>.<span class="ruby-identifier">wait2</span>
<span class="ruby-identifier">pid</span> <span class="ruby-comment">#=> 27437</span>
<span class="ruby-identifier">status</span>.<span class="ruby-identifier">exitstatus</span> <span class="ruby-comment">#=> 99</span>
</pre>
<div class="method-source-code" id="wait2-source">
<pre>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());
}</pre>
</div>
</div>
</div>
<div id="method-c-waitall" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
waitall → [ [pid1,status1], ...]
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Waits for all children, returning an array of <em>pid</em>/<em>status</em> pairs (where <em>status</em> is a <a href="Process/Status.html"><code>Process::Status</code></a> object).</p>
<pre class="ruby"><span class="ruby-identifier">fork</span> { <span class="ruby-identifier">sleep</span> <span class="ruby-value">0.2</span>; <span class="ruby-identifier">exit</span> <span class="ruby-value">2</span> } <span class="ruby-comment">#=> 27432</span>
<span class="ruby-identifier">fork</span> { <span class="ruby-identifier">sleep</span> <span class="ruby-value">0.1</span>; <span class="ruby-identifier">exit</span> <span class="ruby-value">1</span> } <span class="ruby-comment">#=> 27433</span>
<span class="ruby-identifier">fork</span> { <span class="ruby-identifier">exit</span> <span class="ruby-value">0</span> } <span class="ruby-comment">#=> 27434</span>
<span class="ruby-identifier">p</span> <span class="ruby-constant">Process</span>.<span class="ruby-identifier">waitall</span>
</pre>
<p><em>produces</em>:</p>
<pre>[[30982, #<Process::Status: pid 30982 exit 0>],
[30979, #<Process::Status: pid 30979 exit 1>],
[30976, #<Process::Status: pid 30976 exit 2>]]</pre>
<div class="method-source-code" id="waitall-source">
<pre>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;
}</pre>
</div>
</div>
</div>
<div id="method-c-waitpid" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
waitpid(pid=-1, flags=0) → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Waits for a child process to exit, returns its process id, and sets <code>$?</code> to a <a href="Process/Status.html"><code>Process::Status</code></a> object containing information on that process. Which child it waits on depends on the value of <em>pid</em>:</p>
<dl class="rdoc-list note-list"><dt>> 0
<dd>
<p>Waits for the child whose process ID equals <em>pid</em>.</p>
</dd><dt>0
<dd>
<p>Waits for any child whose process group ID equals that of the calling process.</p>
</dd><dt>-1
<dd>
<p>Waits for any child process (the default if no <em>pid</em> is given).</p>
</dd><dt>< -1
<dd>
<p>Waits for any child whose process group ID equals the absolute value of <em>pid</em>.</p>
</dd></dl>
<p>The <em>flags</em> argument may be a logical or of the flag values <a href="Process.html#WNOHANG"><code>Process::WNOHANG</code></a> (do not block if no child available) or <a href="Process.html#WUNTRACED"><code>Process::WUNTRACED</code></a> (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.</p>
<p>Calling this method raises a <a href="SystemCallError.html"><code>SystemCallError</code></a> if there are no child processes. Not available on all platforms.</p>
<pre class="ruby"><span class="ruby-identifier">include</span> <span class="ruby-constant">Process</span>
<span class="ruby-identifier">fork</span> { <span class="ruby-identifier">exit</span> <span class="ruby-value">99</span> } <span class="ruby-comment">#=> 27429</span>
<span class="ruby-identifier">wait</span> <span class="ruby-comment">#=> 27429</span>
<span class="ruby-identifier">$?</span>.<span class="ruby-identifier">exitstatus</span> <span class="ruby-comment">#=> 99</span>
<span class="ruby-identifier">pid</span> = <span class="ruby-identifier">fork</span> { <span class="ruby-identifier">sleep</span> <span class="ruby-value">3</span> } <span class="ruby-comment">#=> 27440</span>
<span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span> <span class="ruby-comment">#=> 2008-03-08 19:56:16 +0900</span>
<span class="ruby-identifier">waitpid</span>(<span class="ruby-identifier">pid</span>, <span class="ruby-constant">Process</span><span class="ruby-operator">::</span><span class="ruby-constant">WNOHANG</span>) <span class="ruby-comment">#=> nil</span>
<span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span> <span class="ruby-comment">#=> 2008-03-08 19:56:16 +0900</span>
<span class="ruby-identifier">waitpid</span>(<span class="ruby-identifier">pid</span>, <span class="ruby-value">0</span>) <span class="ruby-comment">#=> 27440</span>
<span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span> <span class="ruby-comment">#=> 2008-03-08 19:56:19 +0900</span>
</pre>
<div class="method-source-code" id="waitpid-source">
<pre>static VALUE
proc_m_wait(int c, VALUE *v, VALUE _)
{
return proc_wait(c, v);
}</pre>
</div>
</div>
</div>
<div id="method-c-waitpid2" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
waitpid2(pid=-1, flags=0) → [pid, status]
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Waits for a child process to exit (see <a href="Process.html#method-c-waitpid"><code>Process::waitpid</code></a> for exact semantics) and returns an array containing the process id and the exit status (a <a href="Process/Status.html"><code>Process::Status</code></a> object) of that child. Raises a <a href="SystemCallError.html"><code>SystemCallError</code></a> if there are no child processes.</p>
<pre class="ruby"><span class="ruby-constant">Process</span>.<span class="ruby-identifier">fork</span> { <span class="ruby-identifier">exit</span> <span class="ruby-value">99</span> } <span class="ruby-comment">#=> 27437</span>
<span class="ruby-identifier">pid</span>, <span class="ruby-identifier">status</span> = <span class="ruby-constant">Process</span>.<span class="ruby-identifier">wait2</span>
<span class="ruby-identifier">pid</span> <span class="ruby-comment">#=> 27437</span>
<span class="ruby-identifier">status</span>.<span class="ruby-identifier">exitstatus</span> <span class="ruby-comment">#=> 99</span>
</pre>
<div class="method-source-code" id="waitpid2-source">
<pre>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());
}</pre>
</div>
</div>
</div>
</section>
</section>
</main>
<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.2.1.1.
<p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>