Exec (operating system)
Encyclopedia
The exec collection of functions of Unix-like operating systems cause the running process to be completely replaced by the program passed as an argument to the function. As a new process is not created, the process identifier
Process identifier
In computing, the process identifier is a number used by most operating system kernels to uniquely identify a process...

 (PID) does not change, but the data, heap and stack of the original process are replaced by those of the new process.

In the execl, execlp, execv, and execvp calls, the new process image inherits the current environment variable
Environment variable
Environment variables are a set of dynamic named values that can affect the way running processes will behave on a computer.They can be said in some sense to create the operating environment in which a process runs...

s.

Files open when an exec call is made remain open in the new process. This aspect is used to specify the standard streams
Standard streams
In Unix and Unix-like operating systems , as well as certain programming language interfaces, the standard streams are preconnected input and output channels between a computer program and its environment when it begins execution...

 (stdin, stdout and stderr) of the new process.

In MS-DOS
MS-DOS
MS-DOS is an operating system for x86-based personal computers. It was the most commonly used member of the DOS family of operating systems, and was the main operating system for IBM PC compatible personal computers during the 1980s to the mid 1990s, until it was gradually superseded by operating...

 environments, a program executed with one of the exec functions is always loaded 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.

Many Unix shell
Unix shell
A Unix shell is a command-line interpreter or shell that provides a traditional user interface for the Unix operating system and for Unix-like systems...

s also offer an exec built-in command
Shell builtin
In computing, a shell builtin is a command or a function, called from a shell, that is executed directly in the shell itself, instead of an external executable program which the shell would load and execute....

 that replaces the shell process with the specified program.
Wrapper
Wrapper function
A wrapper function is a function in a computer program whose main purpose is to call a second function with little or no additional computation. This is also known as method delegation. Wrapper functions can be used for a number of purposes....

 scripts often use this command to run a program (either directly or through an interpreter
Interpreter (computing)
In computer science, an interpreter normally means a computer program that executes, i.e. performs, instructions written in a programming language...

 or virtual machine
Virtual machine
A virtual machine is a "completely isolated guest operating system installation within a normal host operating system". Modern virtual machines are implemented with either software emulation or hardware virtualization or both together.-VM Definitions:A virtual machine is a software...

) 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.

Prototypes

The functions are declared in the unistd.h
Unistd.h
In the C and C++ programming languages, unistd.h is the name of the header file that provides access to the POSIX operating system API. It is defined by the POSIX.1 standard, the base of the Single Unix Specification, and should therefore be available in any conforming operating system/compiler .On...

header for the POSIX
POSIX
POSIX , an acronym for "Portable Operating System Interface", is a family of standards specified by the IEEE for maintaining compatibility between operating systems...

 standard and in process.h
Process.h
process.h is a C header file which contains function declarations and macros used in working with threads and processes. Most C compilers that target DOS, Windows 3.1x, Win32, OS/2, Novell NetWare or DOS extenders supply this header and the library functions in their C library...

for DOS, OS/2, and Windows.
int execl(char const *path, char const *arg0, ...);
int execle(char const *path, char const *arg0, ..., char const * const *envp);
int execlp(char const *file, char const *arg0, ...);
int execv(char const *path, char const * const * argv);
int execve(char const *path, char const * const *argv, char const * const *envp);
int execvp(char const *file, char const * const *argv);


Some implementations provide these functions named with a leading underscore (e.g. _execl).

Function names

The base of each function is exec, followed by one or more letters:
e - An array of pointers to environment variable
Environment variable
Environment variables are a set of dynamic named values that can affect the way running processes will behave on a computer.They can be said in some sense to create the operating environment in which a process runs...

s is explicitly passed to the new process image.
l - Command-line arguments are passed individually to the function.
p - Uses the PATH environment variable
Path (variable)
PATH is an environment variable on Unix-like operating systems, DOS, OS/2, and Microsoft Windows, specifying a set of directories where executable programs are located...

 to find the file named in the path argument to be executed.
v - Command-line arguments are passed to the function as an array of pointers.

path

The path argument specifies the path name of the file to execute as the new process image. Arguments beginning at arg0 are pointers to arguments to be passed to the new process image. The argv value is an array of pointers to arguments.

arg0

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.

envp

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:
name=value


where name is the environment variable name, and value is the value of that that variable. The final element of the envp array must be null. If envp itself is null, the new process inherits the current environment settings.

Return value

Normally the exec function will replace the current process, so it cannot return anything to the original process. Processes do have an exit status
Exit status
The exit status or return code of a process in computer programming is a small number passed from a child process to a parent process when it has finished executing a specific procedure or delegated task...

, but that value is collected by the parent process
Parent process
In computing, a parent process is a process that has created one or more child processes.- Unix :In the operating system Unix, every process except is created when another process executes the fork system call. The process that invoked fork is the parent process and the newly-created process is...

.

If an exec function does return to the calling process, an error occurred, the return value is −1, and errno is set to one of the following values:
Name Notes
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.

See also

  • Exit
    Exit (operating system)
    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. The operating system reclaims resources that were used by the process...

  • Fork
    Fork (operating system)
    In computing, when a process forks, it creates a copy of itself. More generally, a fork in a multithreading environment means that a thread of execution is duplicated, creating a child thread from the parent thread....

  • Fork-exec
    Fork-exec
    Fork-exec is a commonly used technique in Unix whereby an executing process spawns a new program. fork is the name of the system call that the parent process uses to "divide" itself . After calling fork, the created child process is an exact copy of the parent except for the return value...

  • Overlay
    Overlay (operating system)
    In operating systems, an overlay is when a process replaces itself with the code of another program. On Unix-like systems, this is accomplished with the exec system call....

  • Path (variable)
    Path (variable)
    PATH is an environment variable on Unix-like operating systems, DOS, OS/2, and Microsoft Windows, specifying a set of directories where executable programs are located...

  • Spawn
    Spawn (computing)
    Spawn in computing refers to a function that loads and executes a new child process.The current process may or may not continue to execute asynchronously...

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK