| In: |
./process.c
|
The Process module is a collection of methods used to manipulate processes.
Exits the process immediately. No exit handlers are run. fixnum is returned to the underlying system as the exit status.
Process.exit!(0)
Some operating systems retain the status of terminated child processes until the parent collects that status (normally using some variant of wait(). If the parent never collects this status, the child stays around as a zombie process. Process::detach prevents this by setting up a separate Ruby thread whose sole job is to reap the status of the process pid when it terminates. Use detach only when you do not intent to explicitly wait for the child to terminate. detach only checks the status periodically (currently once each second).
In this first example, we don’t reap the first child process, so it appears as a zombie in the process status display.
p1 = fork { sleep 0.1 }
p2 = fork { sleep 0.2 }
Process.waitpid(p2)
sleep 2
system("ps -ho pid,state -p #{p1}")
produces:
27389 Z
In the next example, Process::detach is used to reap the child automatically.
p1 = fork { sleep 0.1 }
p2 = fork { sleep 0.2 }
Process.detach(p1)
Process.waitpid(p2)
sleep 2
system("ps -ho pid,state -p #{p1}")
(produces no output)
Returns the effective group ID for this process. Not available on all platforms.
Process.egid #=> 500
Returns the process group ID for the given process id. Not available on all platforms.
Process.getpgid(Process.ppid()) #=> 25527
Returns the process group ID for this process. Not available on all platforms.
Process.getpgid(0) #=> 25527 Process.getpgrp #=> 25527
Gets the scheduling priority for specified process, process group, or user. kind indicates the kind of entity to find: one of Process::PRIO_PGRP, Process::PRIO_USER, or Process::PRIO_PROCESS. integer is an id indicating the particular process, process group, or user (an id of 0 means current). Lower priorities are more favorable for scheduling. Not available on all platforms.
Process.getpriority(Process::PRIO_USER, 0) #=> 19 Process.getpriority(Process::PRIO_PROCESS, 0) #=> 19
Get an Array of the gids of groups in the supplemental group access list for this process.
Process.groups #=> [27, 6, 10, 11]
Set the supplemental group access list to the given Array of group IDs.
Process.groups #=> [0, 1, 2, 3, 4, 6, 10, 11, 20, 26, 27] Process.groups = [27, 6, 10, 11] #=> [27, 6, 10, 11] Process.groups #=> [27, 6, 10, 11]
Initializes the supplemental group access list by reading the system group database and using all groups of which the given user is a member. The group with the specified gid is also added to the list. Returns the resulting Array of the gids of all the groups in the supplementary group access list. Not available on all platforms.
Process.groups #=> [0, 1, 2, 3, 4, 6, 10, 11, 20, 26, 27] Process.initgroups( "mgranger", 30 ) #=> [30, 6, 10, 11] Process.groups #=> [30, 6, 10, 11]
Returns the maximum number of gids allowed in the supplemental group access list.
Process.maxgroups #=> 32
Returns the process id of the parent of this process. Always returns 0 on NT. Not available on all platforms.
puts "I am #{Process.pid}"
Process.fork { puts "Dad is #{Process.ppid}" }
produces:
I am 27417 Dad is 27417
Sets the process group ID of pid (0 indicates this process) to integer. Not available on all platforms.
See Process#getpriority.
Process.setpriority(Process::PRIO_USER, 0, 19) #=> 0 Process.setpriority(Process::PRIO_PROCESS, 0, 19) #=> 0 Process.getpriority(Process::PRIO_USER, 0) #=> 19 Process.getpriority(Process::PRIO_PROCESS, 0) #=> 19
Establishes this process as a new session and process group leader, with no controlling tty. Returns the session id. Not available on all platforms.
Process.setsid #=> 27422
Returns a Tms structure (see Struct::Tms on page 388) that contains user and system CPU times for this process.
t = Process.times [ t.utime, t.stime ] #=> [0.0, 0.02]
Waits for a child process to exit, returns its process id, and sets $? to a Process::Status object containing information on that process. Which child it waits on depends on the value of pid:
| > 0: | Waits for the child whose process ID equals pid. |
| 0: | Waits for any child whose process group ID equals that of the calling process. adsasdasd sads adada dsa a sad ad asd sad sa dsa dasdsad asd asd adsasdasd sads adada dsa a sad ad asd sad sa dsa dasdsad asd asd adsasdasd sads adada dsa a sad ad asd sad sa dsa dasdsad asd asd adsasdasd sads adada dsa a sad ad asd sad sa dsa dasdsad asd asd |
| -1: | Waits for any child process (the default if no pid is given). |
| < -1: | Waits for any child whose process group ID equals the absolute value of pid. |
The flags argument may be a logical or of the flag values Process::WNOHANG (do not block if no child available) or Process::WUNTRACED (return stopped children that haven’t been reported). Not all flags are available on all platforms, but a flag value of zero will work on all platforms.
Calling this method raises a SystemError if there are no child processes. Not available on all platforms.
include Process
fork { exit 99 } #=> 27429
wait #=> 27429
$?.exitstatus #=> 99
pid = fork { sleep 3 } #=> 27440
Time.now #=> Wed Apr 09 08:57:09 CDT 2003
waitpid(pid, Process::WNOHANG) #=> nil
Time.now #=> Wed Apr 09 08:57:09 CDT 2003
waitpid(pid, 0) #=> 27440
Time.now #=> Wed Apr 09 08:57:12 CDT 2003
Waits for a child process to exit (see Process::waitpid for exact semantics) and returns an array containing the process id and the exit status (a Process::Status object) of that child. Raises a SystemError if there are no child processes.
Process.fork { exit 99 } #=> 27437
pid, status = Process.wait2
pid #=> 27437
status.exitstatus #=> 99
Waits for all children, returning an array of pid/status pairs (where status is a Process::Status object).
fork { sleep 0.2; exit 2 } #=> 27432
fork { sleep 0.1; exit 1 } #=> 27433
fork { exit 0 } #=> 27434
p Process.waitall
produces:
[[27434, #<Process::Status: pid=27434,exited(0)>],
[27433, #<Process::Status: pid=27433,exited(1)>],
[27432, #<Process::Status: pid=27432,exited(2)>]]
Waits for a child process to exit, returns its process id, and sets $? to a Process::Status object containing information on that process. Which child it waits on depends on the value of pid:
| > 0: | Waits for the child whose process ID equals pid. |
| 0: | Waits for any child whose process group ID equals that of the calling process. adsasdasd sads adada dsa a sad ad asd sad sa dsa dasdsad asd asd adsasdasd sads adada dsa a sad ad asd sad sa dsa dasdsad asd asd adsasdasd sads adada dsa a sad ad asd sad sa dsa dasdsad asd asd adsasdasd sads adada dsa a sad ad asd sad sa dsa dasdsad asd asd |
| -1: | Waits for any child process (the default if no pid is given). |
| < -1: | Waits for any child whose process group ID equals the absolute value of pid. |
The flags argument may be a logical or of the flag values Process::WNOHANG (do not block if no child available) or Process::WUNTRACED (return stopped children that haven’t been reported). Not all flags are available on all platforms, but a flag value of zero will work on all platforms.
Calling this method raises a SystemError if there are no child processes. Not available on all platforms.
include Process
fork { exit 99 } #=> 27429
wait #=> 27429
$?.exitstatus #=> 99
pid = fork { sleep 3 } #=> 27440
Time.now #=> Wed Apr 09 08:57:09 CDT 2003
waitpid(pid, Process::WNOHANG) #=> nil
Time.now #=> Wed Apr 09 08:57:09 CDT 2003
waitpid(pid, 0) #=> 27440
Time.now #=> Wed Apr 09 08:57:12 CDT 2003
Waits for a child process to exit (see Process::waitpid for exact semantics) and returns an array containing the process id and the exit status (a Process::Status object) of that child. Raises a SystemError if there are no child processes.
Process.fork { exit 99 } #=> 27437
pid, status = Process.wait2
pid #=> 27437
status.exitstatus #=> 99