Exec family - A quick note


Categories: C GNU/Linux Systems

This is just for personal future reference:

There is a family of exec() library functions, all of which have slightly different characteristics:

int execl ( const char *path, const char *arg, ... );
int execlp( const char *file, const char *arg, ... );
int execle( const char *path, const char *arg, ..., char *const envp[] );
int execv ( const char *path, char *const argv[] );
int execvp( const char *file, char *const argv[] );
int execvpe(const char *file, char *const argv[], char *const envp[]);

Usage Notes:

execl("/bin/echo","echo", "Hello World", NULL);

When a program is invoked from the shell, the shell sets the first element of the argument list argv[0]) to the name of the program, the second element of the argument list (argv [1]) to the first command-line argument, and so on. When you use an exec function in your programs, you, too, should pass the name of the function as the first element of the argument list.

This is the reason we are passing "echo" as the second argument in execl()

Additional information:

All of these are user space frontends to execve() system call.

int execve( const char *filename, char *const argv[], char *const envp[] );

execve() executes the program pointed to by filename. This causes the program that is currently being run by the calling process to be replaced with a new program, with newly initialized stack, heap, and (initialized and uninitialized) data segments.

More links: https://ece.uwaterloo.ca/~dwharder/icsrts/Tutorials/fork_exec/ http://www.makelinux.net/alp/ (Chapter 3)