Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

exec (system call)

From Wikipedia, the free encyclopedia
Execute a file (a library function and/or a system call)
This article is about the computer operating system function. For other uses, seeExec (disambiguation). For the programming language function that executes a statement, seeeval.
icon
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]

Nomenclature

[edit]

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]

In POSIX systems, other Unix-like systems, and other multitasking systems

[edit]

C language prototypes

[edit]

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:

  • eEnvironment variables are passed as an array of pointers to null-terminated strings of formname=value. The final element of the array must be anull pointer.[3]
  • lCommand-line arguments are passed as individual pointers to null-terminated strings. The last argument must be a null pointer.
  • p – Uses thePATH environment variable to find the file named in thefile argument to be executed.
  • v – 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.[3]
  • f (prefix) – A file descriptor is passed instead. The file descriptor must be opened withO_RDONLY orO_PATH and the caller must have permission to execute its file.[4]

In functions where no environment variables can be passed (execl(),execlp(),execv(),execvp()), the new process image inherits the current environment variables.

First command-line argument

[edit]

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]

Effects

[edit]

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.

Return value

[edit]

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]

In DOS

[edit]

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

In shells

[edit]

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.

In other systems

[edit]

OS/360 and successors include a system callXCTL (transfer control) that performs a similar function to exec.[9]

Versus spawning

[edit]

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]

See also

[edit]

References

[edit]
  1. ^abexec – System Interfaces Reference,The Single UNIX Specification, Version 5 fromThe Open Group
  2. ^Whitney, Tyler."_exec, _wexec Functions".learn.microsoft.com. Retrieved2025-05-26.
  3. ^abexecve – System Interfaces Reference,The Single UNIX Specification, Version 5 fromThe Open Group
  4. ^fexecve – System Interfaces Reference,The Single UNIX Specification, Version 5 fromThe Open Group
  5. ^"Java - Your Application Launcher - Dev.java".Dev.java: The Destination for Java Developers. Retrieved2025-05-26.
  6. ^execve – System Interfaces Reference,The Single UNIX Specification, Version 5 fromThe Open Group
  7. ^Sharma, Sagar (2023-05-28)."Using exec Command in Bash Shell Scripts [4 Examples]".Linux Handbook. Retrieved2025-05-26.
  8. ^"Shell Wrappers".Linux Documentation Project. 2014-03-10. Retrieved2021-02-18.
  9. ^"XCTL".www.ibm.com. Retrieved2025-05-26.
  10. ^posix_spawn – System Interfaces Reference,The Single UNIX Specification, Version 5 fromThe Open Group
Retrieved from "https://en.wikipedia.org/w/index.php?title=Exec_(system_call)&oldid=1311551583"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp