ArticleslgStudy

computer science

Exec (system call)

Exec (system call) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Exec (system call) rather than just read about it. In short: In computing, exec is a functionality of an operating system that runs an executable file in the context of an already existing process, replacing the previous executable. This act is also referred to as an overlay.

Key takeaways

  • Exec (system call) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Exec (system call) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Exec (system call) from memory before moving on to harder problems.

Reference excerpt

In computing, exec is a functionality of an operating system that runs an executable file in the context of an already existing process, replacing the previous executable. This act is also referred to as an overlay. It is especially important in Unix-like systems, although it also exists elsewhere. As no new process is created, the process identifier (PID) does not change, but the machine code, data, 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 languages including compiled languages and some scripting languages. In command interpreters, the exec built-in command replaces the shell process with the specified program.

Nomenclature Interfaces to exec and its implementations vary. Depending on programming language 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 exec() function. High-level programming languages usually provide one call named exec().

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

C language prototypes The POSIX standard declares a family of exec functions in the unistd.h header file. The same functions are declared in process.h for DOS (see below), OS/2, and Microsoft Windows.

Some implementations provide these functions named with a leading underscore (e.g. _execl). The base of each is exec, followed by one or more letters:

e – Environment variables are passed as an array of pointers to null-terminated strings of form name=value. The final element of the array must be a null pointer. l – Command-line arguments are passed as individual pointers to null-terminated strings. The last argument must be a null pointer. p – Uses the PATH environment variable to find the file named in the file 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. f (prefix) – A file descriptor is passed instead. The file descriptor must be opened with O_RDONLY or O_PATH and the caller must have permission to execute its file. 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 The first argument arg0 is often the name of the executable file and may be the same value as the path argument. However, this is purely convention and there is no guarantee of this behavior, nor is it standardized. For instance, in Java, the first argument is not the path to the executable, but instead the first argument for the program.

Effects A file descriptor open when an exec() call is made remains open in the new process image, unless fcntl() was called 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 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 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. The same API is used for overlaying programs in DOS and it has effects similar to ones on POSIX systems. MS-DOS 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 shells also offer a builtin exec 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) 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 XCTL (transfer control) that performs a similar function to exec.

Exec versus spawn The traditional Unix 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.

See also Process management (computing) Chain loading, overlaying in system programming exit (system call), 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

Worked examples

Example 1 — a first encounter with Exec (system call)

Start with the simplest possible case. Write down what Exec (system call) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Exec (system call) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Exec (system call) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Exec (system call)

In research
Exec (system call) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Exec (system call) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Exec (system call) is common in secondary-school and first-year university syllabi. It links to neighbouring topics POSIX, Process.h, Process (computing), so understanding it makes those chapters shorter.
In everyday life
Look for Exec (system call) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Exec (system call)” →

How to study Exec (system call) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Exec (system call) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Exec (system call) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Exec (system call) in simple terms?

In computing, exec is a functionality of an operating system that runs an executable file in the context of an already existing process, replacing the previous executable. This act is also referred to as an overlay.

Why does Exec (system call) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Exec (system call)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Exec (system call).

Tags

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

Keep exploring