[DOC] RDoc for Process (#8179)

This commit is contained in:
Burdette Lamar 2023-08-07 08:51:03 -05:00 committed by GitHub
parent 11f33ba620
commit 589c01c411
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
Notes: git 2023-08-07 13:51:24 +00:00
Merged-By: peterzhu2118 <peter@peterzhu.ca>

197
process.c
View File

@ -520,10 +520,10 @@ clear_pid_cache(void)
* call-seq: * call-seq:
* Process.pid -> integer * Process.pid -> integer
* *
* Returns the process id of this process. Not available on all * Returns the process ID of the current process:
* platforms. *
* Process.pid # => 15668
* *
* Process.pid #=> 27415
*/ */
static VALUE static VALUE
@ -542,16 +542,17 @@ get_ppid(void)
* call-seq: * call-seq:
* Process.ppid -> integer * Process.ppid -> integer
* *
* Returns the process id of the parent of this process. Returns * Returns the process ID of the parent of the current process:
* untrustworthy value on Win32/64. Not available on all platforms.
* *
* puts "I am #{Process.pid}" * puts "Pid is #{Process.pid}."
* Process.fork { puts "Dad is #{Process.ppid}" } * fork { puts "Parent pid is #{Process.ppid}." }
* *
* <em>produces:</em> * Output:
* *
* I am 27417 * Pid is 271290.
* Dad is 27417 * Parent pid is 271290.
*
* May not return a trustworthy value on certain platforms.
*/ */
static VALUE static VALUE
@ -626,16 +627,23 @@ rb_last_status_get(void)
* call-seq: * call-seq:
* Process.last_status -> Process::Status or nil * Process.last_status -> Process::Status or nil
* *
* Returns the status of the last executed child process in the * Returns a Process::Status object representing the most recently exited
* current thread. * child process in the current thread, or +nil+ if none:
* *
* Process.wait Process.spawn("ruby", "-e", "exit 13") * Process.spawn('ruby', '-e', 'exit 13')
* Process.last_status #=> #<Process::Status: pid 4825 exit 13> * Process.wait
* Process.last_status # => #<Process::Status: pid 14396 exit 13>
* *
* If no child process has ever been executed in the current * Process.spawn('ruby', '-e', 'exit 14')
* thread, this returns +nil+. * Process.wait
* Process.last_status # => #<Process::Status: pid 4692 exit 14>
*
* Process.spawn('ruby', '-e', 'exit 15')
* # 'exit 15' has not been reaped by #wait.
* Process.last_status # => #<Process::Status: pid 4692 exit 14>
* Process.wait
* Process.last_status # => #<Process::Status: pid 1380 exit 15>
* *
* Process.last_status #=> nil
*/ */
static VALUE static VALUE
proc_s_last_status(VALUE mod) proc_s_last_status(VALUE mod)
@ -1274,48 +1282,137 @@ proc_wait(int argc, VALUE *argv)
/* /*
* call-seq: * call-seq:
* Process.wait() -> integer * Process.wait(pid = -1, flags = 0) -> integer
* Process.wait(pid=-1, flags=0) -> integer
* Process.waitpid(pid=-1, flags=0) -> integer
* *
* Waits for a child process to exit, returns its process id, and * Waits for a suitable child process to exit, returns its process ID,
* sets <code>$?</code> to a Process::Status object * and sets <tt>$?</tt> to a Process::Status object
* containing information on that process. Which child it waits on * containing information on that process.
* depends on the value of _pid_: * Which child it waits for depends on the value of the given +pid+:
* *
* > 0:: Waits for the child whose process ID equals _pid_. * - Positive integer: Waits for the child process whose process ID is +pid+:
* *
* 0:: Waits for any child whose process group ID equals that of the * pid0 = Process.spawn('ruby', '-e', 'exit 13') # => 230866
* calling process. * pid1 = Process.spawn('ruby', '-e', 'exit 14') # => 230891
* Process.wait(pid0) # => 230866
* $? # => #<Process::Status: pid 230866 exit 13>
* Process.wait(pid1) # => 230891
* $? # => #<Process::Status: pid 230891 exit 14>
* Process.wait(pid0) # Raises Errno::ECHILD
* *
* -1:: Waits for any child process (the default if no _pid_ is * - <tt>0</tt>: Waits for any child process whose group ID
* given). * is the same as that of the current process:
* *
* < -1:: Waits for any child whose process group ID equals the absolute * parent_pgpid = Process.getpgid(Process.pid)
* value of _pid_. * puts "Parent process group ID is #{parent_pgpid}."
* child0_pid = fork do
* puts "Child 0 pid is #{Process.pid}"
* child0_pgid = Process.getpgid(Process.pid)
* puts "Child 0 process group ID is #{child0_pgid} (same as parent's)."
* end
* child1_pid = fork do
* puts "Child 1 pid is #{Process.pid}"
* Process.setpgid(0, Process.pid)
* child1_pgid = Process.getpgid(Process.pid)
* puts "Child 1 process group ID is #{child1_pgid} (different from parent's)."
* end
* retrieved_pid = Process.wait(0)
* puts "Process.wait(0) returned pid #{retrieved_pid}, which is child 0 pid."
* begin
* Process.wait(0)
* rescue Errno::ECHILD => x
* puts "Raised #{x.class}, because child 1 process group ID differs from parent process group ID."
* end
* *
* The _flags_ argument may be a logical or of the flag values * Output:
* Process::WNOHANG (do not block if no child available)
* or Process::WUNTRACED (return stopped children that
* haven't been reported). Not all flags are available on all
* platforms, but a flag value of zero will work on all platforms.
* *
* Calling this method raises a SystemCallError if there are no child * Parent process group ID is 225764.
* processes. Not available on all platforms. * Child 0 pid is 225788
* Child 0 process group ID is 225764 (same as parent's).
* Child 1 pid is 225789
* Child 1 process group ID is 225789 (different from parent's).
* Process.wait(0) returned pid 225788, which is child 0 pid.
* Raised Errno::ECHILD, because child 1 process group ID differs from parent process group ID.
* *
* include Process * - <tt>-1</tt> (default): Waits for any child process:
* fork { exit 99 } #=> 27429
* wait #=> 27429
* $?.exitstatus #=> 99
* *
* pid = fork { sleep 3 } #=> 27440 * parent_pgpid = Process.getpgid(Process.pid)
* Time.now #=> 2008-03-08 19:56:16 +0900 * puts "Parent process group ID is #{parent_pgpid}."
* waitpid(pid, Process::WNOHANG) #=> nil * child0_pid = fork do
* Time.now #=> 2008-03-08 19:56:16 +0900 * puts "Child 0 pid is #{Process.pid}"
* waitpid(pid, 0) #=> 27440 * child0_pgid = Process.getpgid(Process.pid)
* Time.now #=> 2008-03-08 19:56:19 +0900 * puts "Child 0 process group ID is #{child0_pgid} (same as parent's)."
* end
* child1_pid = fork do
* puts "Child 1 pid is #{Process.pid}"
* Process.setpgid(0, Process.pid)
* child1_pgid = Process.getpgid(Process.pid)
* puts "Child 1 process group ID is #{child1_pgid} (different from parent's)."
* sleep 3 # To force child 1 to exit later than child 0 exit.
* end
* child_pids = [child0_pid, child1_pid]
* retrieved_pid = Process.wait(-1)
* puts child_pids.include?(retrieved_pid)
* retrieved_pid = Process.wait(-1)
* puts child_pids.include?(retrieved_pid)
*
* Output:
*
* Parent process group ID is 228736.
* Child 0 pid is 228758
* Child 0 process group ID is 228736 (same as parent's).
* Child 1 pid is 228759
* Child 1 process group ID is 228759 (different from parent's).
* true
* true
*
* - Less than <tt>-1</tt>: Waits for any child whose process group ID is <tt>-pid</tt>:
*
* parent_pgpid = Process.getpgid(Process.pid)
* puts "Parent process group ID is #{parent_pgpid}."
* child0_pid = fork do
* puts "Child 0 pid is #{Process.pid}"
* child0_pgid = Process.getpgid(Process.pid)
* puts "Child 0 process group ID is #{child0_pgid} (same as parent's)."
* end
* child1_pid = fork do
* puts "Child 1 pid is #{Process.pid}"
* Process.setpgid(0, Process.pid)
* child1_pgid = Process.getpgid(Process.pid)
* puts "Child 1 process group ID is #{child1_pgid} (different from parent's)."
* end
* sleep 1
* retrieved_pid = Process.wait(-child1_pid)
* puts "Process.wait(-child1_pid) returned pid #{retrieved_pid}, which is child 1 pid."
* begin
* Process.wait(-child1_pid)
* rescue Errno::ECHILD => x
* puts "Raised #{x.class}, because there's no longer a child with process group id #{child1_pid}."
* end
*
* Output:
*
* Parent process group ID is 230083.
* Child 0 pid is 230108
* Child 0 process group ID is 230083 (same as parent's).
* Child 1 pid is 230109
* Child 1 process group ID is 230109 (different from parent's).
* Process.wait(-child1_pid) returned pid 230109, which is child 1 pid.
* Raised Errno::ECHILD, because there's no longer a child with process group id 230109.
*
* Argument +flags+ should be given as one of the following constants,
* or as the logical OR of both:
*
* - Process::WNOHANG: Does not block if no child process is available.
* - Process:WUNTRACED: May return a stopped child process, even if not yet reported.
*
* Not all flags are available on all platforms.
*
* Raises Errno::ECHILD if there is no suitable child process.
*
* Not available on all platforms.
*
* Process.waitpid is an alias for Process.wait.
*/ */
static VALUE static VALUE
proc_m_wait(int c, VALUE *v, VALUE _) proc_m_wait(int c, VALUE *v, VALUE _)
{ {
@ -1329,7 +1426,7 @@ proc_m_wait(int c, VALUE *v, VALUE _)
* Process.waitpid2(pid=-1, flags=0) -> [pid, status] * Process.waitpid2(pid=-1, flags=0) -> [pid, status]
* *
* Waits for a child process to exit (see Process::waitpid for exact * Waits for a child process to exit (see Process::waitpid for exact
* semantics) and returns an array containing the process id and the * semantics) and returns an array containing the process ID and the
* exit status (a Process::Status object) of that * exit status (a Process::Status object) of that
* child. Raises a SystemCallError if there are no child processes. * child. Raises a SystemCallError if there are no child processes.
* *