exec.2 (2010 09)

e
exec(2) exec(2)
NAME
exec: execl(), execle(), execlp(), execv(), execve(), execvp() - execute a file
SYNOPSIS
#include <unistd.h>
extern char **environ;
int execl(const char *path,
const char *arg0, ...
/*
* [const char *arg1, ..., const char *argn,
]
*/
(char *)0);
int execle(const char *path,
const char *arg0, ...
/*
* [const char *arg1, ..., const char *argn,
]
*/
(char *)0,
char * const envp[]);
int execlp(const char *file,
const char *arg0, ...
/*
* [const char *arg1, ..., const char *argn,]
*/
(char *)0);
int execv(const char *path, char * const argv[]);
int execve(const char *path, char * const argv[], char * const envp[]);
int execvp(const char *file, char * const argv[]);
Remarks
The ANSI C ", ..." construct denotes a variable length argument list whose optional and required
members are given in the associated comment (/* */).
DESCRIPTION
The
exec*() system calls, in all their forms, load a program from an ordinary executable file into the
current process, replacing the current program. The path or file argument refers to either an executable
object file or a file of data for an interpreter. In the latter case, the file of data is also called a script file.
If the calling process is multi-threaded, a call to any of the exec functions will cause all threads and light
weight processes in the calling process to be terminated and the new executable image to be loaded and
executed. No thread specific data destructor functions are called. If the
exec() function fails and
returns to the caller, threads and light weight processes (LWPs) in the calling process will not be ter-
minated.
An executable object file consists of a header (see a.out (4)), text segment, and data segment. The data
segment contains an initialized portion and an uninitialized portion (bss). For
execlp() and
execvp() the POSIX shell (see sh-posix (1)) can be loaded to interpret a script instead. A successful call
to exec*() does not return because the new program overwrites the calling program.
When a C program is executed, it is called as follows:
main (int argc, char **argv, char **envp)
where argc is the argument count and argv is the address of an array of character pointers to the argu-
ments themselves. As indicated, argc usually has a value of at least one, and the first member of the
array points to a string containing the name of the file. Exit conditions from main are discussed in
exit (2).
path points to a path name that identifies the executable file containing the new program.
file (in
execlp() or execvp()) points to a file name identifying the executable file containing the new
program. The path prefix for this file is obtained by searching the directories passed in the environment
variable PATH (see environ (5)). The environment is supplied by the shell (see sh(1)). If file does not have
HP-UX 11i Version 3: September 2010 1 Hewlett-Packard Company 1

Summary of content (4 pages)