exec (Unix)

from Wikipedia, the free encyclopedia

exec*()is a group of system calls under Unix and Unix-like operating systems . What they all have in common is to start a new process in the environment of an existing process and thus to replace it. (This process is also called overlay .) In the POSIX standard, the following function prototypes are unistd.h defined in the header file :

 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[]);

Mean:

arg0
the first (actually: zeroth, because it is implicitly set) argument on the command line and conventionally the name of the executable being executed. Poorly programmed software may rely on this information to determine the exact location in the file system from which the running program was loaded. However, this is not only not guaranteed by the standard, it is also not consistently regulated across platforms.
envp []
an array of pointers that point to strings of the form variable = value and represent the environment of the process
path
the path name of the file to be run. (This should be used for this information instead of arg0 .)
argv []
an array of pointers that point to the command line arguments passed.

Both arrays involved, envp[]and argv[]contain pointers to zero-terminated strings. The last element of the array is conventionally a null pointer, which indicates the end of the array.

Side effects

Open file descriptors of the calling process also remain open in the new process. The environment is also basically preserved. On the other hand, (private) data and code of the calling process are destroyed by the overlay and replaced with the data and code of the overlay. In contrast to exclusively used memory , shared memory is not affected.

The return code of a successfully executed exec*()call cannot be queried by the calling process, since this has been replaced by the call. Replacing a process with an exec * () call does not end the process, which continues to exist (only in a changed state). In the case of a failed call (in this case the process to be replaced is still running), −1 is returned and set errnoto one of the following values ​​(see errno.h):

E2BIG The list of arguments is larger than the relevant system limit allows.
EACCES Access to the executable was denied. (Possible reasons can be: file locked, missing rights, ...)
ENOENT The executable to be started was not found.
ENOMEM There is not enough memory available to start the program.

History, current use

The original Unix could not start a new, different process from an existing one in one step. Instead, a copy of the existing process was created with fork () and then one of the exec*()functions was used to provide the created copy with a (different) overlay. This group of system calls was therefore of great importance.

In the meantime, POSIX has defined a group of system calls posix_spawn*()as an (optional) extension with which the sequence fork()- can exec*()be avoided, but with the loss of some of the flexibility that this mechanism offers.

exec as a shell command

Many common Unix shells also implement a command execthat basically does the same thing as the system call: the running process (or the running script) is replaced by an instance of the specified new executable. In the example given, this mechanism is used to exit an X11 start script after calling the window manager:

 #!/bin/ksh
 xclock &
 xsetroot -solid black
 exec mwm -multiscreen -xrm "ShowFeedback: -quit"

Web links