Skip to content

Commit 589c01c

Browse files
[DOC] RDoc for Process (ruby#8179)
1 parent 11f33ba commit 589c01c

File tree

1 file changed

+157
-60
lines changed

1 file changed

+157
-60
lines changed

process.c

Lines changed: 157 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -518,12 +518,12 @@ clear_pid_cache(void)
518518

519519
/*
520520
* call-seq:
521-
* Process.pid -> integer
521+
* Process.pid -> integer
522522
*
523-
* Returns the process id of this process. Not available on all
524-
* platforms.
523+
* Returns the process ID of the current process:
524+
*
525+
* Process.pid # => 15668
525526
*
526-
* Process.pid #=> 27415
527527
*/
528528

529529
static VALUE
@@ -540,18 +540,19 @@ get_ppid(void)
540540

541541
/*
542542
* call-seq:
543-
* Process.ppid -> integer
543+
* Process.ppid -> integer
544544
*
545-
* Returns the process id of the parent of this process. Returns
546-
* untrustworthy value on Win32/64. Not available on all platforms.
545+
* Returns the process ID of the parent of the current process:
547546
*
548-
* puts "I am #{Process.pid}"
549-
* Process.fork { puts "Dad is #{Process.ppid}" }
547+
* puts "Pid is #{Process.pid}."
548+
* fork { puts "Parent pid is #{Process.ppid}." }
550549
*
551-
* <em>produces:</em>
550+
* Output:
551+
*
552+
* Pid is 271290.
553+
* Parent pid is 271290.
552554
*
553-
* I am 27417
554-
* Dad is 27417
555+
* May not return a trustworthy value on certain platforms.
555556
*/
556557

557558
static VALUE
@@ -624,18 +625,25 @@ rb_last_status_get(void)
624625

625626
/*
626627
* call-seq:
627-
* Process.last_status -> Process::Status or nil
628+
* Process.last_status -> Process::Status or nil
628629
*
629-
* Returns the status of the last executed child process in the
630-
* current thread.
630+
* Returns a Process::Status object representing the most recently exited
631+
* child process in the current thread, or +nil+ if none:
631632
*
632-
* Process.wait Process.spawn("ruby", "-e", "exit 13")
633-
* Process.last_status #=> #<Process::Status: pid 4825 exit 13>
633+
* Process.spawn('ruby', '-e', 'exit 13')
634+
* Process.wait
635+
* Process.last_status # => #<Process::Status: pid 14396 exit 13>
634636
*
635-
* If no child process has ever been executed in the current
636-
* thread, this returns +nil+.
637+
* Process.spawn('ruby', '-e', 'exit 14')
638+
* Process.wait
639+
* Process.last_status # => #<Process::Status: pid 4692 exit 14>
640+
*
641+
* Process.spawn('ruby', '-e', 'exit 15')
642+
* # 'exit 15' has not been reaped by #wait.
643+
* Process.last_status # => #<Process::Status: pid 4692 exit 14>
644+
* Process.wait
645+
* Process.last_status # => #<Process::Status: pid 1380 exit 15>
637646
*
638-
* Process.last_status #=> nil
639647
*/
640648
static VALUE
641649
proc_s_last_status(VALUE mod)
@@ -1274,48 +1282,137 @@ proc_wait(int argc, VALUE *argv)
12741282

12751283
/*
12761284
* call-seq:
1277-
* Process.wait() -> integer
1278-
* Process.wait(pid=-1, flags=0) -> integer
1279-
* Process.waitpid(pid=-1, flags=0) -> integer
1280-
*
1281-
* Waits for a child process to exit, returns its process id, and
1282-
* sets <code>$?</code> to a Process::Status object
1283-
* containing information on that process. Which child it waits on
1284-
* depends on the value of _pid_:
1285-
*
1286-
* > 0:: Waits for the child whose process ID equals _pid_.
1287-
*
1288-
* 0:: Waits for any child whose process group ID equals that of the
1289-
* calling process.
1290-
*
1291-
* -1:: Waits for any child process (the default if no _pid_ is
1292-
* given).
1293-
*
1294-
* < -1:: Waits for any child whose process group ID equals the absolute
1295-
* value of _pid_.
1296-
*
1297-
* The _flags_ argument may be a logical or of the flag values
1298-
* Process::WNOHANG (do not block if no child available)
1299-
* or Process::WUNTRACED (return stopped children that
1300-
* haven't been reported). Not all flags are available on all
1301-
* platforms, but a flag value of zero will work on all platforms.
1285+
* Process.wait(pid = -1, flags = 0) -> integer
1286+
*
1287+
* Waits for a suitable child process to exit, returns its process ID,
1288+
* and sets <tt>$?</tt> to a Process::Status object
1289+
* containing information on that process.
1290+
* Which child it waits for depends on the value of the given +pid+:
1291+
*
1292+
* - Positive integer: Waits for the child process whose process ID is +pid+:
1293+
*
1294+
* pid0 = Process.spawn('ruby', '-e', 'exit 13') # => 230866
1295+
* pid1 = Process.spawn('ruby', '-e', 'exit 14') # => 230891
1296+
* Process.wait(pid0) # => 230866
1297+
* $? # => #<Process::Status: pid 230866 exit 13>
1298+
* Process.wait(pid1) # => 230891
1299+
* $? # => #<Process::Status: pid 230891 exit 14>
1300+
* Process.wait(pid0) # Raises Errno::ECHILD
1301+
*
1302+
* - <tt>0</tt>: Waits for any child process whose group ID
1303+
* is the same as that of the current process:
1304+
*
1305+
* parent_pgpid = Process.getpgid(Process.pid)
1306+
* puts "Parent process group ID is #{parent_pgpid}."
1307+
* child0_pid = fork do
1308+
* puts "Child 0 pid is #{Process.pid}"
1309+
* child0_pgid = Process.getpgid(Process.pid)
1310+
* puts "Child 0 process group ID is #{child0_pgid} (same as parent's)."
1311+
* end
1312+
* child1_pid = fork do
1313+
* puts "Child 1 pid is #{Process.pid}"
1314+
* Process.setpgid(0, Process.pid)
1315+
* child1_pgid = Process.getpgid(Process.pid)
1316+
* puts "Child 1 process group ID is #{child1_pgid} (different from parent's)."
1317+
* end
1318+
* retrieved_pid = Process.wait(0)
1319+
* puts "Process.wait(0) returned pid #{retrieved_pid}, which is child 0 pid."
1320+
* begin
1321+
* Process.wait(0)
1322+
* rescue Errno::ECHILD => x
1323+
* puts "Raised #{x.class}, because child 1 process group ID differs from parent process group ID."
1324+
* end
1325+
*
1326+
* Output:
1327+
*
1328+
* Parent process group ID is 225764.
1329+
* Child 0 pid is 225788
1330+
* Child 0 process group ID is 225764 (same as parent's).
1331+
* Child 1 pid is 225789
1332+
* Child 1 process group ID is 225789 (different from parent's).
1333+
* Process.wait(0) returned pid 225788, which is child 0 pid.
1334+
* Raised Errno::ECHILD, because child 1 process group ID differs from parent process group ID.
1335+
*
1336+
* - <tt>-1</tt> (default): Waits for any child process:
1337+
*
1338+
* parent_pgpid = Process.getpgid(Process.pid)
1339+
* puts "Parent process group ID is #{parent_pgpid}."
1340+
* child0_pid = fork do
1341+
* puts "Child 0 pid is #{Process.pid}"
1342+
* child0_pgid = Process.getpgid(Process.pid)
1343+
* puts "Child 0 process group ID is #{child0_pgid} (same as parent's)."
1344+
* end
1345+
* child1_pid = fork do
1346+
* puts "Child 1 pid is #{Process.pid}"
1347+
* Process.setpgid(0, Process.pid)
1348+
* child1_pgid = Process.getpgid(Process.pid)
1349+
* puts "Child 1 process group ID is #{child1_pgid} (different from parent's)."
1350+
* sleep 3 # To force child 1 to exit later than child 0 exit.
1351+
* end
1352+
* child_pids = [child0_pid, child1_pid]
1353+
* retrieved_pid = Process.wait(-1)
1354+
* puts child_pids.include?(retrieved_pid)
1355+
* retrieved_pid = Process.wait(-1)
1356+
* puts child_pids.include?(retrieved_pid)
1357+
*
1358+
* Output:
1359+
*
1360+
* Parent process group ID is 228736.
1361+
* Child 0 pid is 228758
1362+
* Child 0 process group ID is 228736 (same as parent's).
1363+
* Child 1 pid is 228759
1364+
* Child 1 process group ID is 228759 (different from parent's).
1365+
* true
1366+
* true
1367+
*
1368+
* - Less than <tt>-1</tt>: Waits for any child whose process group ID is <tt>-pid</tt>:
1369+
*
1370+
* parent_pgpid = Process.getpgid(Process.pid)
1371+
* puts "Parent process group ID is #{parent_pgpid}."
1372+
* child0_pid = fork do
1373+
* puts "Child 0 pid is #{Process.pid}"
1374+
* child0_pgid = Process.getpgid(Process.pid)
1375+
* puts "Child 0 process group ID is #{child0_pgid} (same as parent's)."
1376+
* end
1377+
* child1_pid = fork do
1378+
* puts "Child 1 pid is #{Process.pid}"
1379+
* Process.setpgid(0, Process.pid)
1380+
* child1_pgid = Process.getpgid(Process.pid)
1381+
* puts "Child 1 process group ID is #{child1_pgid} (different from parent's)."
1382+
* end
1383+
* sleep 1
1384+
* retrieved_pid = Process.wait(-child1_pid)
1385+
* puts "Process.wait(-child1_pid) returned pid #{retrieved_pid}, which is child 1 pid."
1386+
* begin
1387+
* Process.wait(-child1_pid)
1388+
* rescue Errno::ECHILD => x
1389+
* puts "Raised #{x.class}, because there's no longer a child with process group id #{child1_pid}."
1390+
* end
1391+
*
1392+
* Output:
1393+
*
1394+
* Parent process group ID is 230083.
1395+
* Child 0 pid is 230108
1396+
* Child 0 process group ID is 230083 (same as parent's).
1397+
* Child 1 pid is 230109
1398+
* Child 1 process group ID is 230109 (different from parent's).
1399+
* Process.wait(-child1_pid) returned pid 230109, which is child 1 pid.
1400+
* Raised Errno::ECHILD, because there's no longer a child with process group id 230109.
1401+
*
1402+
* Argument +flags+ should be given as one of the following constants,
1403+
* or as the logical OR of both:
1404+
*
1405+
* - Process::WNOHANG: Does not block if no child process is available.
1406+
* - Process:WUNTRACED: May return a stopped child process, even if not yet reported.
1407+
*
1408+
* Not all flags are available on all platforms.
1409+
*
1410+
* Raises Errno::ECHILD if there is no suitable child process.
13021411
*
1303-
* Calling this method raises a SystemCallError if there are no child
1304-
* processes. Not available on all platforms.
1305-
*
1306-
* include Process
1307-
* fork { exit 99 } #=> 27429
1308-
* wait #=> 27429
1309-
* $?.exitstatus #=> 99
1412+
* Not available on all platforms.
13101413
*
1311-
* pid = fork { sleep 3 } #=> 27440
1312-
* Time.now #=> 2008-03-08 19:56:16 +0900
1313-
* waitpid(pid, Process::WNOHANG) #=> nil
1314-
* Time.now #=> 2008-03-08 19:56:16 +0900
1315-
* waitpid(pid, 0) #=> 27440
1316-
* Time.now #=> 2008-03-08 19:56:19 +0900
1414+
* Process.waitpid is an alias for Process.wait.
13171415
*/
1318-
13191416
static VALUE
13201417
proc_m_wait(int c, VALUE *v, VALUE _)
13211418
{
@@ -1329,7 +1426,7 @@ proc_m_wait(int c, VALUE *v, VALUE _)
13291426
* Process.waitpid2(pid=-1, flags=0) -> [pid, status]
13301427
*
13311428
* Waits for a child process to exit (see Process::waitpid for exact
1332-
* semantics) and returns an array containing the process id and the
1429+
* semantics) and returns an array containing the process ID and the
13331430
* exit status (a Process::Status object) of that
13341431
* child. Raises a SystemCallError if there are no child processes.
13351432
*

0 commit comments

Comments
 (0)