In C programming, the exec family of functions is used to replace the current process image with a new process image. These functions are useful for running other programs from within a C program.
The following are the functions in the exec family:
- int execl(const char *path, const char *arg, ...);
- int execv(const char *path, char *const argv[]);
- int execle(const char *path, const char *arg, ..., char *const envp[]);
- int execve(const char *path, char *const argv[], char *const envp[]);
- int execlp(const char *file, const char *arg, ...);
- int execvp(const char *file, char *const argv[]);
Here is a brief description of each of the exec functions:
- execl - This function takes the path of the program to be executed and a list of arguments. The arguments must be passed as separate parameters to the function.
- execv - This function takes the path of the program to be executed and a null-terminated array of arguments. The arguments must be passed as an array of strings.
- execle - This function is similar to execl, but it also allows you to specify the environment variables for the new process. The environment variables must be passed as a null-terminated array of strings.
- execve - This function is similar to execv, but it also allows you to specify the environment variables for the new process. The environment variables must be passed as a null-terminated array of strings.
- execlp - This function is similar to execl, but it searches the PATH environment variable to find the program to be executed. The first argument should be the name of the program, not its path.
- execvp - This function is similar to execv, but it searches the PATH environment variable to find the program to be executed. The first argument should be the name of the program, not its path.
In all of these functions, the new process image replaces the current process image, so any code that comes after the exec function call will not be executed.
Here is an example code demonstrating the usage of the execvp() function to replace the current process with a new process:
File: EXEC.c
File: execDemo.c
In the above example, EXEC.c is a simple C program that prints a message to the console. execDemo.c is another C program that uses the execvp() function to replace the current process with EXEC.c.
The execvp() function takes two arguments: the first argument is the name of the file to be executed ("./EXEC" in this case), and the second argument is an array of pointers to null-terminated strings that represent the arguments to the program. In this example, we pass only the name of the program ("EXEC") as an argument.
When the execvp() function is called, it replaces the current process with the EXEC program. Therefore, the last printf() statement in execDemo.c will never be executed.
To run this example, you can compile both files separately and then run execDemo:
gcc -o EXEC EXEC.c
gcc -o execDemo execDemo.c
./execDemo
In this example, the execlp() function is used to execute the "ls" command, and the execl() function is used to execute a C program named "EXEC". Once the execlp() or execl() function is executed, the current process is replaced by the new process.
The execvpe() function is similar to execvp(), but it allows the caller to specify the environment of the executed program via the envp argument. The execle() function is similar to execl(), but it also allows the caller to specify the environment of the executed program via the envp argument. The envp argument is an array of pointers to null-terminated strings that represent the environment variables for the new process.
In most cases, if the envp argument is NULL, the environment for the new process image is inherited from the calling process. However, if the envp argument is not NULL, it provides a new environment for the new process image. The envp argument is an array of pointers to null-terminated strings that represent the environment variables for the new process. The last element of the array must be a null pointer to indicate the end of the array.
Note that some implementations of the exec() functions may provide the environment variables to the new process image in a different way, such as by passing a pointer to an environment block instead of using the envp argument. However, the general concept of passing environment variables to a new process image is the same.