Process creation using functions of exec system call family [various exec family functions] - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

Process creation using functions of exec system call family [various exec family functions]

Share This

The exec function families are defined in the header unistd.h. So, you must use this header on the C program where you want to use these functions.

The available exec functions along with their function parameters are given below:
int execl(const char *path, const char *arg, …, NULL);
int execlp(const char *file, const char *arg, …, NULL );
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execle(const char *path, const char *arg, …, NULL, char * const envp[] );
int execve(const char *file, char *const argv[], char *const envp[]);
Let’s see what each of these functions do and how to use them.

execl() System Function


In execl() system function takes the path of the executable binary file (i.e. /bin/ls) as the first and second argument. Then, the arguments (i.e. -lh, /home) that you want to pass to the executable followed by NULL. Then execl() system function runs the command and prints the output. If any error occurs, then execl() returns -1. Otherwise, it returns nothing.

int execl(const char *path, const char *arg, ..., NULL);
An example of the execl() system function is given below:

#include <unistd.h> int main(void) { char *binaryPath = "/bin/ls"; char *arg1 = "-lh"; char *arg2 = "/home"; execl(binaryPath, binaryPath, arg1, arg2, NULL); return 0; }

execlp() System Function


execl() does not use the PATH environment variable. So, the full path of the executable file is required to run it with execl(). execlp() uses the PATH environment variable. So, if an executable file or command is available in the PATH, then the command or the filename is enough to run it, the full path is not needed.

int execlp(const char *file, const char *arg, …, NULL );
We can rewrite the execl() example using the execlp() system function as follows:

#include <unistd.h> int main(void) { char *programName = "ls"; char *arg1 = "-lh"; char *arg2 = "/home"; execlp(programName, programName, arg1, arg2, NULL); return 0; }

execv() System Function


In execl() function, the parameters of the executable file is passed to the function as different arguments. With execv(), you can pass all the parameters in a NULL terminated array argv. The first element of the array should be the path of the executable file. Otherwise, execv() function works just as execl() function.

int execv(const char *path, char *const argv[]);
We can rewrite the execl() example as follows:

#include <unistd.h> int main(void) { char *binaryPath = "/bin/ls"; char *args[] = {binaryPath, "-lh", "/home", NULL}; execv(binaryPath, args); return 0; }

execvp() System Function


Works the same way as execv() system function. But, the PATH environment variable is used. So, the full path of the executable file is not required just as in execlp().

int execvp(const char *file, char *const argv[]);
We can rewrite the execv() example as follows:

#include <unistd.h> int main(void) { char *programName = "ls"; char *args[] = {programName, "-lh", "/home", NULL}; execvp(programName, args); return 0; }

execle() System Function


Works just like execl() but you can provide your own environment variables along with it. The environment variables are passed as an array envp. The last element of the envp array should be NULL. All the other elements contain the key-value pairs as string.

int execle(const char *path, const char *arg, ..., NULL, char * const envp[] );
An example of the execle() system function is given below:

#include <unistd.h> int main(void) { char *binaryPath = "/bin/bash"; char *arg1 = "-c"; char *arg2 = "echo "Visit $HOSTNAME:$PORT from your browser.""; char *const env[] = {"HOSTNAME=www.linuxhint.com", "PORT=8080", NULL}; execle(binaryPath, binaryPath,arg1, arg2, NULL, env); return 0; }

execve() System Function


Just like execle() you can provide your own environment variables along with execve(). You can also pass arguments as arrays as you did in execv().

int execve(const char *file, char *const argv[], char *const envp[]);
The execle() example can be rewritten as follows:

#include <unistd.h> int main(void) { char *binaryPath = "/bin/bash"; char *const args[] = {binaryPath, "-c", "echo "Visit $HOSTNAME:$PORT from your browser."", NULL}; char *const env[] = {"HOSTNAME=www.linuxhint.com", "PORT=8080", NULL}; execve(binaryPath, args, env); return 0; }


Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.