Exec (system call)
Execute a file (a library function and/or a system call)
title: "Exec (system call)" type: doc version: 1 created: 2026-02-28 author: "Wikipedia contributors" status: active scope: public tags: ["process-(computing)", "posix", "process.h", "unix-sus2008-utilities", "system-calls"] description: "Execute a file (a library function and/or a system call)" topic_path: "technology/operating-systems" source: "https://en.wikipedia.org/wiki/Exec_(system_call)" license: "CC BY-SA 4.0" wikipedia_page_id: 0 wikipedia_revision_id: 0
::summary Execute a file (a library function and/or a system call) ::
In computing, **** 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 specially important in Unix-like systems, although it also exists elsewhere. As no new process is created, the 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 call or some variant is available for many programming languages including compiled languages and some scripting languages. In command interpreters, the built-in command replaces the shell process with the specified program.
Nomenclature
Interfaces to and its implementations vary. Depending on programming language it may be accessible via one or more functions, and depending on operating system it may be represented with one or more actual system calls. For this reason, is sometimes described as a collection of functions.
In C, there is no single, plain function.
High-level programming languages usually provide one call named .
In POSIX systems, other Unix-like systems, and other multitasking systems
C language prototypes
The POSIX standard declares a family of functions in the header file. The same functions are declared in for DOS (see below), OS/2, and Microsoft Windows.
::code[lang=c] 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[]); int execvpe(const char* file, char* const argv[], char* const envp[]); int fexecve(int fd, char* const argv[], char* const envp[]); ::
Some implementations provide these functions named with a leading underscore (e.g. ).
The base of each is , followed by one or more letters:
- – Environment variables are passed as an array of pointers to null-terminated strings of form . The final element of the array must be a null pointer.
- – Command-line arguments are passed as individual pointers to null-terminated strings. The last argument must be a null pointer.
- – Uses the PATH environment variable to find the file named in the argument to be executed.
- – Command-line arguments are passed as an array of pointers to null-terminated strings. The final element of the array must be a null pointer.
- (prefix) – A file descriptor is passed instead. The file descriptor must be opened with or and the caller must have permission to execute its file. In functions where no environment variables can be passed (, , , ), the new process image inherits the current environment variables.
First command-line argument
The first argument is often the name of the executable file and may be the same value as the argument. However, this is purely convention and there is no guarantee of this behavior, nor is it standardized. For instance, in Java, the first argument is not the path to the executable, but instead the first argument for the program.
Effects
A file descriptor open when an call is made remains open in the new process image, unless was called with or opened with (the latter was introduced in POSIX.1-2001). This aspect is used to specify the standard streams of the new program.
A successful overlay destroys the previous memory address space of the process. All of 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, are lost.
Return value
A successful call replaces the current process image, so it cannot return anything to the program that made the call. Processes do have an exit status, but that value is collected by the parent process.
If the call fails, the return value is always , and is set to an appropriate value.
In DOS
DOS is not a multitasking operating system, but replacing the previous executable image is essential 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 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 of 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 functions, the program might behave differently from a program invoked directly from the operating-system command line or with one of the functions (see below).
In shells
Many Unix shells also offer a builtin 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 , the resources used by the shell program do not need to stay in use after the program is started.
The command can also perform a redirection. In some shells, it is possible to use it for redirection only, without making an actual overlay.
In other systems
OS/360 and successors include a system call (transfer control) that performs a similar function to exec.
Exec versus spawn
The traditional Unix system does not have the functionality to create a new process running a new executable program in one step. Other systems may use as the main tool for running executables. Its result is equivalent to the fork–exec sequence of Unix-like systems. POSIX supports the routines as an optional extension.
References
References
- {{man. 3. exec
- Whitney, Tyler. "_exec, _wexec Functions".
- {{man. 3. execve
- {{man. 3. fexecve
- "Java - Your Application Launcher - Dev.java".
- {{Man. 3. execve. SUS
- Sharma, Sagar. (2023-05-28). "Using exec Command in Bash Shell Scripts [4 Examples]".
- (2014-03-10). "Shell Wrappers". [[Linux Documentation Project]].
- "XCTL".
- {{man. 3. posix_spawn
::callout[type=info title="Wikipedia Source"] This article was imported from Wikipedia and is available under the Creative Commons Attribution-ShareAlike 4.0 License. Content has been adapted to SurfDoc format. Original contributors can be found on the article history page. ::