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);
pin the name: accepts a program name and it searches for the program in
vin the name: the program name must be given in full(path).
ein the name: function accepts additional array of env vars.
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) to the name of the program, the second element of the argument list (argv ) 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
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.
man 2 execvefor the systemcall
man 3 execvefor info on exec family
man 3 execfor library function info on exec family
man 1 execfor info on the exec shell utility (which i did not cover in this blog, but just added for fun)
More links: https://ece.uwaterloo.ca/~dwharder/icsrts/Tutorials/fork_exec/ http://www.makelinux.net/alp/ (Chapter 3)