Programming Ruby

The Pragmatic Programmer's Guide

Previous < Contents ^
module Process

Index:

egid egid= euid euid= exit! fork getpgid getpgrp getpriority gid gid= kill pid ppid setpgid setpgrp setpriority setsid uid uid= wait wait2 waitpid waitpid2


The Process module is a collection of methods used to manipulate processes.

constants

PRIO_PGRP 1 Process Group priority.
PRIO_PROCESS 0 Process priority.
PRIO_USER 2 User priority.
WNOHANG 1 Do not block if no child has exited. Not available on all platforms.
WUNTRACED 2 Return stopped children as well. Not available on all platforms.

class methods
egid Process.egid -> aFixnum

Returns the effective group id for this process.

Process.egid » 500

egid= Process.egid= aFixnum -> aFixnum

Sets the effective group id for this process.

euid Process.euid -> aFixnum

Returns the effective user id for this process.

Process.euid » 501

euid= Process.euid= aFixnum

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

exit! Process.exit!( aFixnum=-1 )

Exits the process immediately. No exit handlers are run. aFixnum is returned to the underlying system as the exit status.

Process.exit!(0)

fork Process.fork [{ block } ] -> aFixnum or nil

See Kernel::fork on page 416.

getpgid Process.getpgid( anInteger ) -> anInteger

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

Process.getpgid(Process.ppid()) » 18650

getpgrp Process.getpgrp -> anInteger

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

Process.getpgid(0) » 18650
Process.getpgrp » 18650

getpriority Process.getpriority( aKind, anInteger ) -> aFixnum

Gets the scheduling priority for specified process, process group, or user. aKind indicates the kind of entity to find: one of Process::PRIO_PGRP , Process::PRIO_USER , or Process::PRIO_PROCESS . anInteger 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

gid Process.gid -> aFixnum

Returns the group id for this process.

Process.gid » 500

gid= Process.gid= aFixnum -> aFixnum

Sets the group id for this process.

kill Process.kill( aSignal, [ aPid ]+ ) -> aFixnum

Sends the given signal to the specified process id(s), or to the current process if aPid is zero. aSignal may be an integer signal number or a POSIX signal name (either with or without a SIG prefix). If aSignal is negative (or starts with a ``-'' sign), kills process groups instead of processes. Not all signals are available on all platforms.

trap("SIGHUP") { close_then_exit }
Process.kill("SIGHUP", 0)

pid Process.pid -> aFixnum

Returns the process id of this process. Not available on all platforms.

Process.pid » 21282

ppid Process.ppid -> aFixnum

Returns the process id of the parent of this process. Always returns 0 on NT. Not available on all platforms.

print "I am ", Process.pid, "\n"
Process.fork { print "Dad is ", Process.ppid, "\n" }
produces:
I am 21284
Dad is 21284

setpgid Process.setpgid( aPid, anInteger ) -> 0

Sets the process group id of aPid (0 indicates this process) to anInteger. Not available on all platforms.

setpgrp Process.setpgrp -> 0

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

setpriority Process.setpriority( kind, anInteger, anIntPriority ) -> 0

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

setsid Process.setsid -> aFixnum

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 » 21289

uid Process.uid -> aFixnum

Returns the user id of this process.

Process.uid » 501

uid= Process.uid= anInteger -> aNumeric

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

wait Process.wait -> aFixnum

Waits for any child process to exit and returns the process id of that child. Raises a SystemError if there are no child processes. Not available on all platforms.

Process.fork { exit 1; } » 21294
Process.wait » 21294

wait2 Process.wait2 -> anArray

Waits for any child process to exit and returns an array containing the process id and the exit status of that child. Raises a SystemError if there are no child processes.

Process.fork { exit 1 } » 21297
Process.wait2 » [21297, 256]

waitpid Process.waitpid( aPid, anInteger=0 ) -> aPid

Waits for the given child process to exit. anInteger may be a logical or of the flag value 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.

include Process
pid = fork { sleep 3 } » 21300
Time.now » Sun Nov 25 23:51:35 CST 2001
waitpid(pid, Process::WNOHANG) » nil
Time.now » Sun Nov 25 23:51:35 CST 2001
waitpid(pid, 0) » 21300
Time.now » Sun Nov 25 23:51:38 CST 2001

waitpid2 Process.waitpid2( aPid, anInteger=0 ) -> anArray

Waits for the given child process to exit, returning that child's process id and exit status. anInteger may be a logical or of the flag value 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.


Previous < Contents ^

Extracted from the book "Programming Ruby - The Pragmatic Programmer's Guide"
Copyright © 2000 Addison Wesley Longman, Inc. Released under the terms of the Open Publication License V1.0.
This reference is available for download.