HOME

TheInfoList



OR:

In
computing Computing is any goal-oriented activity requiring, benefiting from, or creating computer, computing machinery. It includes the study and experimentation of algorithmic processes, and the development of both computer hardware, hardware and softw ...
, exec is a functionality of an
operating system An operating system (OS) is system software that manages computer hardware and software resources, and provides common daemon (computing), services for computer programs. Time-sharing operating systems scheduler (computing), schedule tasks for ...
that runs an executable file in the context of an already existing
process A process is a series or set of activities that interact to produce a result; it may occur once-only or be recurrent or periodic. Things called a process include: Business and management * Business process, activities that produce a specific s ...
, replacing the previous executable. This act is also referred to as an overlay. It is especially important in
Unix-like A Unix-like (sometimes referred to as UN*X, *nix or *NIX) operating system is one that behaves in a manner similar to a Unix system, although not necessarily conforming to or being certified to any version of the Single UNIX Specification. A Uni ...
systems, although it also exists elsewhere. As no new process is created, the
process identifier In computing, the process identifier (a.k.a. process ID or PID) is a number used by most operating system kernel (operating system), kernels—such as those of Unix, macOS and Windows—to uniquely identify an active Process (computing), process. ...
(PID) does not change, but the machine code,
data Data ( , ) are a collection of discrete or continuous values that convey information, describing the quantity, quality, fact, statistics, other basic units of meaning, or simply sequences of symbols that may be further interpreted for ...
, heap, and stack of the process are replaced by those of the new program. The ''exec'' call or some variant is available for many
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
s including compiled languages and some
scripting language In computing, a script is a relatively short and simple set of instructions that typically automation, automate an otherwise manual process. The act of writing a script is called scripting. A scripting language or script language is a programming ...
s. In command interpreters, the built-in command replaces the shell process with the specified program.


Nomenclature

Interfaces to ''exec'' and its implementations vary. Depending on
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
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, ''exec'' is sometimes described as a ''collection of functions''. In C, there is no single, plain function. The
Linux kernel The Linux kernel is a Free and open-source software, free and open source Unix-like kernel (operating system), kernel that is used in many computer systems worldwide. The kernel was created by Linus Torvalds in 1991 and was soon adopted as the k ...
has one corresponding system call named , whereas all other functions are user-space wrappers around it.
High-level programming languages A high-level programming language is a programming language with strong abstraction from the details of the computer. In contrast to low-level programming languages, it may use natural language ''elements'', be easier to use, or may automate ...
usually provide one call named .


In Unix, POSIX, and other multitasking systems


C language prototypes

The
POSIX The Portable Operating System Interface (POSIX; ) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines application programming interfaces (APIs), along with comm ...
standard declares a family of ''exec'' functions in the header file. The same functions are declared in for DOS (see below),
OS/2 OS/2 is a Proprietary software, proprietary computer operating system for x86 and PowerPC based personal computers. It was created and initially developed jointly by IBM and Microsoft, under the leadership of IBM software designer Ed Iacobucci, ...
, and
Microsoft Windows Windows is a Product lining, product line of Proprietary software, proprietary graphical user interface, graphical operating systems developed and marketed by Microsoft. It is grouped into families and subfamilies that cater to particular sec ...
. 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 ''exec'', followed by one or more letters: * –
Environment variable An environment variable is a user-definable value that can affect the way running processes will behave on a computer. Environment variables are part of the environment in which a process runs. For example, a running process can query the va ...
s are passed as an array of pointers to null-terminated strings of form . The final element of the array must be a
null pointer In computing, a null pointer (sometimes shortened to nullptr or null) or null reference is a value saved for indicating that the Pointer (computer programming), pointer or reference (computer science), reference does not refer to a valid Object (c ...
. * – 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 ''file'' 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 Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
, the first argument is ''not'' the path to the executable, but instead the first argument for the program.


Effects

A
file descriptor In Unix and Unix-like computer operating systems, a file descriptor (FD, less frequently fildes) is a process-unique identifier (handle) for a file or other input/output resource, such as a pipe or network socket. File descriptors typically h ...
open when an ''exec'' call is made remains open in the new process image, unless fcntl 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 -1, and
errno errno.h is a header file in the standard library of the C programming language. It defines macros for reporting and retrieving error conditions using the symbol errno (short form for "error number").International Standard for Programming Langu ...
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 In computing, virtual memory, or virtual storage, is a memory management technique that provides an "idealized abstraction of the storage resources that are actually available on a given machine" which "creates the illusion to users of a ver ...
. The same API is used for overlaying programs in DOS and it has effects similar to ones on POSIX systems.
MS-DOS MS-DOS ( ; acronym for Microsoft Disk Operating System, also known as Microsoft DOS) is an operating system for x86-based personal computers mostly developed by Microsoft. Collectively, MS-DOS, its rebranding as IBM PC DOS, and a few op ...
''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 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 ''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).


In shells

Many
Unix shell A Unix shell is a Command-line_interface#Command-line_interpreter, command-line interpreter or shell (computing), shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command languag ...
s 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 In computing, a virtual machine (VM) is the virtualization or emulator, emulation of a computer system. Virtual machines are based on computer architectures and provide the functionality of a physical computer. Their implementations may involve ...
) 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 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.


Versus spawning

The traditional
Unix Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
system does not have the functionality to create a ''new'' process running a new executable program in one step. 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.


See also

* Chain loading, overlaying in system programming *
exit (system call) On many computer operating systems, a computer process terminates its execution by making an exit system call. More generally, an exit in a multithreading environment means that a thread of execution has stopped running. For resource man ...
, to terminate a process * fork (system call), to make a new process (but with the same executable) * clone(), a way to create new threads


References

{{reflist Process (computing) POSIX Process.h Unix SUS2008 utilities System calls