execve

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

This function 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.

More...
version(StdDdoc)
int
execve
(
in string pathname
,
in string[] argv
,
in string[] envp
)

Detailed Description

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);
}

Meta