View source code Display the source code in std/process.d from which this page was generated on github. Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone. Page wiki View or edit the community-maintained wiki page associated with this page.

Function std.process.execvpe

Replaces the current process by executing a command, pathname, with the arguments in argv.

This functions is Posix-Only.

Typically, the first element of argv is the command being executed, i.e. argv[0] == pathname. The 'p' versions of exec search the PATH environment variable for pathname. The 'e' versions additionally take the new process' 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 execvpe(
  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 execv* by using 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' execv*. 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.

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 execv* functions work. Example:

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

License

Boost License 1.0.

Comments