This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed. Find sources: "Exec" system call – news ·newspapers ·books ·scholar ·JSTOR(February 2024) (Learn how and when to remove this message) |
Incomputing,exec is a functionality of anoperating system that runs anexecutable file in the context of an already existingprocess, replacing the previous executable. This act is also referred to as anoverlay. It is specially important inUnix-like systems, although it also exists elsewhere. As no new process is created, theprocess identifier (PID) does not change, but themachine code,data,heap, andstack of the process are replaced by those of the new program.
Theexec() call or some variant is available for manyprogramming languages includingcompiled languages and somescripting languages. Incommand interpreters, theexecbuilt-in command replaces the shell process with the specified program.[1]
Interfaces toexec and its implementations vary. Depending onprogramming language it may be accessible via one or morefunctions, and depending on operating system it may be represented with one or more actualsystem calls. For this reason,exec is sometimes described as acollection of functions.
InC, there is no single, plainexec() function.
High-level programming languages usually provide one call namedexec().[citation needed]
ThePOSIX standard declares a family ofexec functions in theunistd.h header file. The same functions are declared inprocess.h for DOS (seebelow),OS/2, andMicrosoft Windows.
intexecl(charconst*path,charconst*arg0,...);intexecle(charconst*path,charconst*arg0,...,charconst*envp[]);intexeclp(charconst*file,charconst*arg0,...);intexecv(charconst*path,charconst*argv[]);intexecve(charconst*path,charconst*argv[],charconst*envp[]);intexecvp(charconst*file,charconst*argv[]);intexecvpe(constchar*file,char*constargv[],char*constenvp[]);intfexecve(intfd,char*constargv[],char*constenvp[]);
Some implementations provide these functions named with a leading underscore (e.g._execl).[2]
The base of each isexec, followed by one or more letters:
name=value. The final element of the array must be anull pointer.[3]In functions where no environment variables can be passed (execl(),execlp(),execv(),execvp()), the new process image inherits the current environment variables.
The first argumentarg0 is often the name of the executable file and may be the same value as thepath argument. However, this is purely convention and there is no guarantee of this behavior, nor is it standardized. For instance, inJava, the first argument isnot the path to the executable, but instead the first argument for the program.[5]
Afile descriptor open when anexec() call is made remains open in the new process image, unlessfcntl() was called withFD_CLOEXEC or opened withO_CLOEXEC (the latter was introduced in POSIX.1-2001). This aspect is used to specify thestandard streams of the new program.
A successful overlay destroys the previousmemory address space of the process. All of its memory areas that were notshared are reclaimed by the operating system. Consequently, all its data that were not passed to the new program, or otherwise saved, are lost.
A successful call replaces the current process image, so it cannot return anything to the program that made the call. Processes do have anexit status, but that value is collected by theparent process.
If the call fails, the return value is always-1, anderrno is set to an appropriate value.[6]
DOS is not amultitasking operating system, but replacing the previous executable image is essential due to harshprimary memory limitations and lack ofvirtual memory.The same API is used for overlaying programs in DOS and it has effects similar to ones on POSIX systems.
MS-DOSexec() functions always load the new program into memory as if the "maximum allocation" in the program'sexecutable 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 theexec() functions, the program might behave differently from a program invoked directly from the operating-system command line or with one of thespawn() functions (seebelow).
ManyUnix shells also offer a builtinexec command that replaces the shell process with the specified program.[1][7]Wrapper scripts often use this command to run a program (either directly or through aninterpreter orvirtual machine) after setting environment variables or other configuration. By usingexec, the resources used by the shell program do not need to stay in use after the program is started.[8]
The command can also perform aredirection. In some shells, it is possible to use it for redirection only, without making an actual overlay.
OS/360 and successors include a system callXCTL (transfer control) that performs a similar function to exec.[9]
The traditionalUnix system does not have the functionality to create anew process running a new executable program in one step. Other systems may usespawn as the main tool for running executables. Its result is equivalent to thefork–exec sequence of Unix-like systems. POSIX supports theposix_spawn routines as an optional extension.[10]
exec – System Interfaces Reference,The Single UNIX Specification, Version 5 fromThe Open Groupexecve – System Interfaces Reference,The Single UNIX Specification, Version 5 fromThe Open Groupfexecve – System Interfaces Reference,The Single UNIX Specification, Version 5 fromThe Open Groupexecve – System Interfaces Reference,The Single UNIX Specification, Version 5 fromThe Open Groupposix_spawn – System Interfaces Reference,The Single UNIX Specification, Version 5 fromThe Open Group