Exec (operating system)
   HOME

TheInfoList



OR:

In
computing Computing is any goal-oriented activity requiring, benefiting from, or creating computing machinery. It includes the study and experimentation of algorithmic processes, and development of both hardware and software. Computing has scientific, ...
, exec is a functionality of an
operating system An operating system (OS) is system software that manages computer hardware, software resources, and provides common services for computer programs. Time-sharing operating systems schedule tasks for efficient use of the system and may also i ...
that runs an
executable file In computing, executable code, an executable file, or an executable program, sometimes simply referred to as an executable or binary, causes a computer "to perform indicated tasks according to encoded instructions", as opposed to a data fil ...
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 se ...
, 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 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 Unix-li ...
systems, although it 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 kernels—such as those of Unix, macOS and Windows—to uniquely identify an active process. This number may be used as a parameter in vario ...
(PID) does not change, but the
machine code In computer programming, machine code is any low-level programming language, consisting of machine language instructions, which are used to control a computer's central processing unit (CPU). Each instruction causes the CPU to perform a ve ...
,
data In the pursuit of knowledge, data (; ) is a collection of discrete Value_(semiotics), values that convey information, describing quantity, qualitative property, quality, fact, statistics, other basic units of meaning, or simply sequences of sy ...
, heap, and stack of the process are replaced by those of the new program. The ''exec'' call is available for many
programming language A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
s including compilable languages and some
scripting language A scripting language or script language is a programming language that is used to manipulate, customize, and automate the facilities of an existing system. Scripting languages are usually interpreted at runtime rather than compiled. A scripting ...
s. In OS 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. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming ...
it may be accessible via one or more functions, and depending on operating system it may be represented with one or more actual
system call In computing, a system call (commonly abbreviated to syscall) is the programmatic way in which a computer program requests a service from the operating system on which it is executed. This may include hardware-related services (for example, acc ...
s. For this reason ''exec'' is sometimes described as a ''collection of functions''. Standard names of such functions in C are , , , , , and (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 .


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 both the system- and user-level application programming inter ...
standard declares ''exec'' functions in the header file, in the
C language C (''pronounced like the letter c'') is a general-purpose computer programming language. It was created in the 1970s by Dennis Ritchie, and remains very widely used and influential. By design, C's features cleanly reflect the capabilities o ...
. The same functions are declared in for DOS (see below), OS/2, and Microsoft Windows. :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 fexecve(int fd, char *const argv[], char *const envp[]); 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 variable An environment variable is a dynamic-named value that can affect the way running processes will behave on a computer. They are part of the environment in which a process runs. For example, a running process can query the value of the TEMP envi ...
s is explicitly passed to the new process image. :l –
Command-line argument A command-line interpreter or command-line processor uses a command-line interface (CLI) to receive commands from a user in the form of lines of text. This provides a means of setting parameters for the environment, invoking executables and pro ...
s 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. ; path The argument specifies the path name of the file to execute as the new process image. Arguments beginning at ''arg0'' are
pointers Pointer may refer to: Places * Pointer, Kentucky * Pointers, New Jersey * Pointers Airport, Wasco County, Oregon, United States * The Pointers, a pair of rocks off Antarctica People with the name * Pointer (surname), a surname (including a lis ...
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 In computer programming, a null-terminated string is a character string stored as an array containing the characters and terminated with a null character (a character with a value of zero, called NUL in this article). Alternative names are C str ...
defining an
environment variable An environment variable is a dynamic-named value that can affect the way running processes will behave on a computer. They are part of the environment in which a process runs. For example, a running process can query the value of the TEMP envi ...
. Each null-terminated string has the form: :name=value 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 Null may refer to: Science, technology, and mathematics Computing * Null (SQL) (or NULL), a special marker and keyword in SQL indicating that something has no value * Null character, the zero-valued ASCII character, also designated by , often use ...
. In the , , , and calls, the new process image inherits the current environment variables.


Effects

A file descriptor open when an ''exec'' call is made remains open in the new process image, unless was ed with FD_CLOEXEC or opened with O_CLOEXEC (the latter was introduced in POSIX.1-2001). This aspect is used to specify the
standard streams In computer programming, standard streams are interconnected input and output communication channels between a computer program and its environment when it begins execution. The three input/output (I/O) connections are called standard input (stdin ...
(stdin, stdout and stderr) of the new program. A successful overlay destroys the previous
memory address In computing, a memory address is a reference to a specific memory location used at various levels by software and hardware. Memory addresses are fixed-length sequences of digits conventionally displayed and manipulated as unsigned integers. ...
space of the process, and all its memory areas, that were not
shared Shared may refer to: * Sharing * Shared ancestry or Common descent * Shared care * Shared-cost service * Shared decision-making in medicine * Shared delusion (disambiguation), Shared delusion, various meanings * Shared government * Shared intellig ...
, are reclaimed by the operating system. Consequently, all its data that were not passed to the new program, or otherwise saved, become lost.


Return value

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 The exit status of a process in computer programming is a small number passed from a child process (or callee) to a parent process (or caller) when it has finished executing a specific procedure or delegated task. In DOS, this may be referred ...
, but that value is collected by the
parent process In computing, a parent process is a process that has created one or more child processes. Unix-like systems In Unix-like operating systems, every process except (the swapper) is created when another process executes the fork() system call. T ...
. If an exec function does return to the calling program, an error occurs, the return value is −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 for "error number").International Standard for Programming Language C ( ...
is set to one of the following values:


DOS operating systems

DOS DOS is shorthand for the MS-DOS and IBM PC DOS family of operating systems. DOS may also refer to: Computing * Data over signalling (DoS), multiplexing data onto a signalling channel * Denial-of-service attack (DoS), an attack on a communicat ...
is not a multitasking operating system, but replacing the previous executable image has a great merit there due to harsh
primary memory Computer data storage is a technology consisting of computer components and recording media that are used to retain digital data. It is a core function and fundamental component of computers. The central processing unit (CPU) of a computer ...
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 very ...
. 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 ope ...
''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).


Command interpreters

Many
Unix shell A Unix shell is a command-line interpreter or shell that provides a command line user interface for Unix-like operating systems. The shell is both an interactive command language and a scripting language, and is used by the operating syste ...
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/ emulation of a computer system. Virtual machines are based on computer architectures and provide functionality of a physical computer. Their implementations may involve specialized h ...
) 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 even possible to use the command for redirection only, without making an actual overlay.


Alternatives

The traditional
Unix Unix (; trademarked as UNIX) is a family of multitasking, multiuser 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, an ...
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 Spawn or spawning may refer to: * Spawn (biology), the eggs and sperm of aquatic animals Arts, entertainment, and media * Spawn (character), a fictional character in the comic series of the same name and in the associated franchise ** '' 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 In computing, particularly in the context of the Unix operating system and its Unix-like, workalikes, fork is an operation whereby a Computer process, process creates a copy of itself. It is an interface which is required for compliance with the P ...
.


Other Systems

OS/360 and successors OS/360, officially known as IBM System/360 Operating System, is a discontinued batch processing operating system developed by IBM for their then-new System/360 mainframe computer, announced in 1964; it was influenced by the earlier IBSYS/IBJOB ...
include a system call XCTL (transfer control) that performs a similar function to exec.


See also

*
Chain loading Chain loading is a method used by computer programs to replace the currently executing program with a new program, using a common data area to pass information from the current program to the new program. It occurs in several areas of computing. ...
, 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 manageme ...
, terminate a process *
fork (system call) In computing, particularly in the context of the Unix operating system and its workalikes, fork is an operation whereby a process creates a copy of itself. It is an interface which is required for compliance with the POSIX and Single UNIX Speci ...
, make a new process (but with the same executable) * clone(), the way to create new threads *
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. In general, each executing process or user session has its own PATH setting ...
, related to semantics of the argument


References


External links

*{{man, sh, exec, SUS, execute a file Process (computing) POSIX Process.h Unix SUS2008 utilities System calls