exec (system call)
In computing, exec is a functionality of an operating system that runs an executable file in the context of an already existing process, replacing the previous executable. This act is also referred to as an overlay. It is especially important in Unix-like systems, although other operating systems implement it as well. Since a new process is not created, the original process identifier (PID) does not change, but the machine code, data, heap, and stack of the process are replaced by those of the new program.
The exec call is supported in many programming languages, including compilable languages and some scripting languages. In OS command interpreters, the exec built-in command replaces the shell process with the specified program.
Interfaces to exec and its implementations vary. Depending on the programming language it may be accessible via one or more functions, and depending on the operating system it may be represented with one or more actual system calls. For this reason exec is sometimes described as a collection of functions.
Standard names of such functions in C are execl, execle, execlp, execv, execve, and execvp (see below), but not "exec" itself. The Linux kernel has one corresponding system call named "execve", whereas all aforementioned functions are user-space wrappers around it.
Higher-level languages usually provide one call named exec.
Unix, POSIX, and other multitasking systems
C language prototypes
int execl(char const *path, char const *arg0, ...);
int execle(char const *path, char const *arg0, ..., char const *envp);
int execlp(char const *file, char const *arg0, ...);
int execv(char const *path, char const *argv);
int execve(char const *path, char const *argv, char const *envp);
int execvp(char const *file, char const *argv);
Some implementations provide these functions named with a leading underscore (e.g. _execl).
The base of each is exec (execute), followed by one or more letters:
- e – An array of pointers to environment variables is explicitly passed to the new process image.
- l – Command-line arguments are passed individually (a list) to the function.
- p – Uses the PATH environment variable to find the file named in the file argument to be executed.
- v – Command-line arguments are passed to the function as an array (vector) of pointers.
In the execl, execlp, execv, and execvp calls, the new process image inherits the current environment variables.
- The path argument specifies the path name of the file to execute as the new process image.
- arg0, ...
- Arguments beginning at arg0 are pointers to arguments to be passed to the new process image.
- The first argument arg0 should be the name of the executable file. Usually it is the same value as the path argument. Some programs may incorrectly rely on this argument providing the location of the executable, but there is no guarantee of this nor is it standardized across platforms.
- The argv value is an array of pointers to arguments.
- Argument envp is an array of pointers to environment settings.
- The exec calls named ending with an e alter the environment for the new process image by passing a list of environment settings through the envp argument. This argument is an array of character pointers; each element (except for the final element) points to a null-terminated string defining an environment variable.
- Each null-terminated string has the form:
- where name is the environment variable name, and value is the value of that variable. The final element of the envp array must be null.
A file descriptor open when an exec call is made will remain open in the new process image, unless it was marked
fcntl or opened with
O_CLOEXEC (the latter was introduced in POSIX.1-2001). This aspect is used to specify the standard streams (stdin, stdout and stderr) of the new program.
A successful overlay destroys the previous memory address space of the process, and all its memory areas, that were not shared, are reclaimed by the operating system. Consequently, all its data that were not passed to the new program, or otherwise saved, become lost.
A successful exec replaces the current process image, so it cannot return anything to the program that made the call. Processes do have an exit status, but "exec-ing" is not a termination of the process - it continues to exist.
If an exec function does return to the calling program, an error has occurred. In this case, the return value is −1, and errno is set to one of the following values:
|E2BIG||The argument list exceeds the system limit.|
|EACCES||The specified file has a locking or sharing violation.|
|ENOENT||The file or path name not found.|
|ENOMEM||Not enough memory is available to execute the new process image.|
In Linux, the kernel may destroy the memory map of the old process and start constructing (loading) the new executable, and only then discover that the new executable can't be loaded (for example, because some fields in executable's internal tables contain invalid values). In this case, the process is killed by a fatal signal (such as SIGSEGV). Ideally, the kernel would fail
execve and return an error indication to the old executable, but that would be significantly more complex and slower.
DOS operating systems
DOS is not a multitasking operating system, but replacing the previous executable image has a great merit there due to harsh primary memory limitations and lack of virtual memory. The same API is used for overlaying programs in DOS and it has effects similar to ones on POSIX systems.
MS-DOS exec functions always load the new program into memory as if the "maximum allocation" in the program's executable file header is set to default value 0xFFFF. The EXEHDR utility can be used to change the maximum allocation field of a program. However, if this is done and the program is invoked with one of the exec functions, the program might behave differently from a program invoked directly from the operating-system command line or with one of the spawn functions (see below).
Many Unix shells also offer a builtin exec command that replaces the shell process with the specified program. Wrapper scripts often use this command to run a program (either directly or through an interpreter or virtual machine) after setting environment variables or other configuration. By using exec, the resources used by the shell program do not need to stay in use after the program is started.
The exec command can also perform a redirection. In some shells passing no arguments means to use the exec command for redirection only, without making an actual overlay.
The traditional Unix system does not have the functionality to create a new process running a new executable program in one step, which explains the importance of exec for Unix programming. Other systems may use spawn as the main tool for running executables. Its result is equivalent to the fork-exec sequence of Unix-like systems. POSIX supports the posix_spawn routines as an optional extension that usually is implemented using vfork.
OS/360 and successors include a system call XCTL (transfer control) that performs a similar function to exec. Except for type 4 SVC, this usage is rare. More common is the use of LINK or ATTACH to invoke a load module without terminating the current load module. In particular, TSO invokes commands with ATTACH, sharing memory.
- Chain loading, overlaying in system programming
- exit (system call), terminate a process
- fork (system call), make a new process (but with the same executable)
- PATH (variable), related to semantics of the *path argument
- Tail call, semantically analogous to the built-in
execcommand in most shell script languages but for subroutines instead of processes.