Function std.process.execve
Replaces the current process by executing a command,
, with
the arguments in pathname
.
argv
This functions is Posix-Only.
Typically, the first element of
is
the command being executed, i.e. argv
. The 'p'
versions of argv
[0] == pathname
exec
search the PATH environment
variable for
. The 'e' versions additionally take the new process'
pathname
environment
variables as an array of strings of the form key=value.
Does not return on success (the current process will have been replaced). Returns -1 on failure with no indication of the underlying error.
Prototype
int execve( const(string) pathname, const(string[]) argv, const(string[]) envp );
Windows specific
These functions are only supported on POSIX platforms, as the Windows
operating systems do not provide the ability to overwrite the current
process image with another. In single-threaded programs it is possible
to approximate the effect of
by using execv
*spawnProcess
and terminating the current process once the child process has returned.
For example:
auto commandLine = [ "program", "arg1", "arg2" ]; version (Posix) { execv(commandLine[0], commandLine); throw new Exception("Failed to execute program"); } else version (Windows) { import core.stdc.stdlib: _exit; _exit(wait(spawnProcess(commandLine))); }
This is, however, NOT equivalent to POSIX'
. For one thing, the
executed program is started as a separate process, with all this entails.
Secondly, in a multithreaded program, other threads will continue to do
work while the current thread is waiting for the child process to complete.
execv
*
A better option may sometimes be to terminate the current program immediately
after spawning the child process. This is the behaviour exhibited by the
_exec
functions in Microsoft's C runtime library, and it is how D's now-deprecated
Windows
functions work. Example:
execv
*
auto commandLine = [ "program", "arg1", "arg2" ]; version (Posix) { execv(commandLine[0], commandLine); throw new Exception("Failed to execute program"); } else version (Windows) { spawnProcess(commandLine); import core.stdc.stdlib: _exit; _exit(0); }
Authors
Lars Tandle Kyllingstad, Steven Schveighoffer, Vladimir Panteleev