Replacing the Current Process: A Beginner’s Guide to exec(), Passing Arguments in main()
Hello Everyone,
Today we will explore about the basic Linux command which is exec() family & How we can pass arguments in main function also how to print them using path, environment variables
Passing Arguments in main function
You have generally seen main function as :- int main() or void main() without any arguments, but we can also pass arguments in main like argument count , argument vector, environment variables how ?? lets see with some examples
Here we can see that we have passed three arguments in main we will each one of role one by one.
Now let's see them how they behave individually.
In the first run, you can see that , it is only showing only one argument this is because ( you've already seen in the definition) the argument counter also counts the name of the program and in the second run, we have passed some arguments so it will count them and the role of argv is to print them(arguments).
To understand about what envp() is doing we first have to know about what exactly environment variables are.
you all have seem environment variables in your system at any point of time while downloading vs code or setting paths to the programs or for any other works.
Environment Variables
Environment variables are key-value pairs stored in the operating system that provide information about the system environment to processes (programs) at runtime. Few examples of environment variable are PATH,HOME,USER...etc. The key is denoted by upper case letters. You can also set Environment variables by using export command for example :- export ENVIROMENT=this_is_an_example.
Also you can make environment variable before running your program let's see how.
To print the environment variables you can use this simple code.
Also you can see the output of this code
Understanding the exec() Family in Linux
To know about role of exec family first we should have basic knowledge about what is the main role of exec function is:
When an exec() system call is invoked the program specified in parameter to exec() will entirely replace the process including all the threads. It replaces the PCB (program counter block) where it is called and stops the execution of the code written before. The exec() family of functions replaces the current process image with a new process image.
exec family consists of various functions like:
we will see each of them with the help of examples.
In the above code we have created a child process using fork() function and we are replacing the child process using execl() and passing path (of previous code which is ./envir) with our previous code which is shared earlier in example1.
Recommended by LinkedIn
2. execle() :- replaces the current process with a new program, just like other exec functions. The key feature of execle() is that it allows you to provide a custom environment (envp[]) for the new program. int execle(const char path, const char arg0, ..., NULL, char *const envp[]);
For Custom environment Variable:-
This code is to select the custom variables from the Environment variable. We can also select Built-in environment variables. Now we have provided the environment variable the step is to provide path and Arguments the code will be same the only change we will make is to pass the environment variable... and the task of this code is also same to replace the current code with the previous code but it will print the environment variables too.
3. execlp() :- replaces the current running program with a new program, searching the new program's executable using the PATH environment variable. int execlp(const char file, const char arg0, ..., (char *)NULL); Here we can see that we need to pass const char file
In Unix-like operating systems, functions like execvp, execlp rely on the PATH environment variable to locate and execute programs. If you're running a custom program that isn't in a standard system directory (like /bin or /usr/bin), you need to update your PATH so that execvp can find it. How to correctly set the PATH variable using the current working directory. By using the pwd command to get your path and appending it to the existing PATH with a colon (:) separator, you ensure that your executable files are accessible system-wide. This is a crucial step when testing or running custom programs from non-standard locations for example:-
PATH=/home/naroliya-98/him:$PATH
now after setting the correct path for your working directory pass the correct arguments to it.
4. execv() :- takes two arguments: the absolute path to the executable file and an array of argument strings, ending with a NULL pointer. It does not search the directories listed in the PATH variable, so you must provide the full path to the executable.
This system call is commonly used when you want to execute a program with a specific set of arguments, using the current process’s environment, without needing advanced features like custom environment settings.
execv() uses a string of characters which includes arguments in it.
5. execve() :- The execve() system call is the core function in the exec family, used to execute a new program by replacing the current process image with a new one. It provides the most fundamental interface for executing programs in Unix-like systems.
Unlike its wrapper functions (execv(), execvp(), etc.), execve() requires the full path to the executable and takes two arrays: one for command-line arguments and one for environment variables. Because it does not search the PATH environment variable, the exact location of the program must be known and specified.
When execve() is called successfully, the current process is replaced by the new program and does not return. If it fails (e.g., file not found or permission denied), it returns -1 and sets the errno variable.
Summary and Conclusion
In this article, I learned and explained how the different exec functions work in Linux. At first, all the names like execv, execvp, execve, felt confusing, but once I understood the small differences between them like using the PATH or passing environment variables it all made more sense.
Each function has its own purpose: some are easier to use, and others give more control. For example, execvp() is great when you want to search using the PATH, and execve() is useful when you want to pass custom environment variables.
Understanding these functions helped me get a better idea of how processes are created and replaced in Linux, and how system-level programming works. I hope this explanation helps other beginners like me who are starting to explore how Linux handles programs behind the scenes.
Special thanks to Kalpavriksha Edu and Shubham Kumar