These functions may be used to create and manage processes.
The various exec*() functions take a list of arguments for
the new program loaded into the process. In each case, the first of
these arguments is passed to the new program as its own name rather
than as an argument a user may have typed on a command line. For the
C programmer, this is the argv[0]
passed to a program's
main(). For example, "os.execv('/bin/echo', ['foo',
'bar'])" will only print "bar" on standard output; "foo"will seem to be ignored.
3
. Be aware that
programs which use signal.signal() to register a handler
for SIGABRT will behave differently.
Availability: Unix, Windows.
The "l" and "v" variants of the exec*() functions differ in how command-line arguments are passed. The "l" variants are perhaps the easiest to work with if the number of parameters is fixed when the code is written; the individual parameters simply become additional parameters to the execl*() functions. The "v" variants are good when the number of parameters is variable, with the arguments being passed in a list or tuple as the args parameter. In either case, the arguments to the child process must start with the name of the command being run.
The variants which include a "p" near the end (execlp(), execlpe(), execvp(), and execvpe()) will use the PATH environment variable to locate the program file. When the environment is being replaced (using one of the exec*e() variants, discussed in the next paragraph), the new environment is used as the source of the PATH variable. The other variants, execl(), execle(), execv(), and execve(), will not use the PATH variable to locate the executable; path must contain an appropriate absolute or relative path.
For execle(), execlpe(), execve(), and execvpe() (note that these all end in "e"), the env parameter must be a mapping which is used to define the environment variables for the new process; the execl(), execlp(), execv(), and execvp() all cause the new process to inherit the environment of the current process. Availability: Unix, Windows.
Note: the standard way to exit is sys.exit(n)
.
_exit() should normally only be used in the child process
after a fork().
0
in the child, the child's
process id in the parent.
Availability: Unix.
(pid, fd)
,
where pid is 0
in the child, the new child's process id
in the parent, and fd is the file descriptor of the master end
of the pseudo-terminal. For a more portable approach, use the
pty module.
Availability: Some flavors of Unix.
<sys/lock.h>
) determines which segments are locked.
Availability: Unix.
-signal
, where
signal is the signal that killed the process. On Windows, the
process ID will actually be the process handle.
The "l" and "v" variants of the spawn*() functions differ in how command-line arguments are passed. The "l" variants are perhaps the easiest to work with if the number of parameters is fixed when the code is written; the individual parameters simply become additional parameters to the spawnl*() functions. The "v" variants are good when the number of parameters is variable, with the arguments being passed in a list or tuple as the args parameter. In either case, the arguments to the child process must start with the name of the command being run.
The variants which include a second "p" near the end (spawnlp(), spawnlpe(), spawnvp(), and spawnvpe()) will use the PATH environment variable to locate the program file. When the environment is being replaced (using one of the spawn*e() variants, discussed in the next paragraph), the new environment is used as the source of the PATH variable. The other variants, spawnl(), spawnle(), spawnv(), and spawnve(), will not use the PATH variable to locate the executable; path must contain an appropriate absolute or relative path.
For spawnle(), spawnlpe(), spawnve(), and spawnvpe() (note that these all end in "e"), the env parameter must be a mapping which is used to define the environment variables for the new process; the spawnl(), spawnlp(), spawnv(), and spawnvp() all cause the new process to inherit the environment of the current process.
As an example, the following calls to spawnlp() and spawnvpe() are equivalent:
import os os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null') L = ['cp', 'index.html', '/dev/null'] os.spawnvpe(os.P_WAIT, 'cp', L, os.environ)
Availability: Unix, Windows. spawnlp(), spawnlpe(), spawnvp() and spawnvpe() are not available on Windows. New in version 1.6.
-signal
if a signal kills the
process.
Availability: Unix, Windows.
New in version 1.6.
startfile() returns as soon as the associated application is launched. There is no option to wait for the application to close, and no way to retrieve the application's exit status. The path parameter is relative to the current directory. If you want to use an absolute path, make sure the first character is not a slash ("/"); the underlying Win32 ShellExecute() function doesn't work if it is. Use the os.path.normpath() function to ensure that the path is properly encoded for Win32. Availability: Windows. New in version 2.0.
posix.environ
, sys.stdin
,
etc. are not reflected in the environment of the executed command.
The return value is the exit status of the process encoded in the
format specified for wait(), except on Windows 95 and 98,
where it is always 0
. Note that POSIX does not specify the
meaning of the return value of the C system() function,
so the return value of the Python function is system-dependent.
Availability: Unix, Windows.
0
for normal operation.
Availability: Unix.
If pid is greater than 0
, waitpid() requests
status information for that specific process. If pid is
0
, the request is for the status of any child in the process
group of the current process. If pid is -1
, the request
pertains to any child of the current process. If pid is less
than -1
, status is requested for any process in the process
group -pid
(the absolute value of pid).
The following functions take a process status code as returned by system(), wait(), or waitpid() as a parameter. They may be used to determine the disposition of a process.
WIFEXITED(status)
is true, return the integer
parameter to the exit(2) system call. Otherwise, the return
value is meaningless.
Availability: Unix.
See About this document... for information on suggesting changes.