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


Article content
Example1: Passing arguments in main function

Here we can see that we have passed three arguments in main we will each one of role one by one.

  1. int argc : this argument will count the number of arguments in you current process or It tells you how many command-line arguments were passed to the program including the name of the program itself.
  2. char *argv[] : It is an array of character pointers (strings). It stores each command-line argument passed to the program as a string.
  3. char *envp[] : envp stands for Environment Pointer. It is an array of strings (just like argv[]), but instead of command-line arguments, it contains the environment variables available to the process at runtime.

Now let's see them how they behave individually.


Article content
Output of the program

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.


Article content
Both are Creating Environment variables

To print the environment variables you can use this simple code.


Article content
Environment Variable Program

Also you can see the output of this code


Article content
These are some environment Variables you can see here using the above 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:

  • execl()
  • execle()
  • execlp()
  • execv()
  • execve().

we will see each of them with the help of examples.


  1. execl() :- "Execute using a list of arguments", The execl() function replaces the current process image with a new process image (i.e., runs a new program), using a list of arguments. int execl(const char pathname, const char arg, ... /*, (char ) NULL /);

Article content
Example of execl() function

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.

Article content
Child Process replaced with the execl() call.

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:-

Article content
To select custom environment variable we have used strncmp() function to compare two strings


Article content
creating of environment variable and selecting it from various environment variables

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.


Article content
Array of Custom environment variable
Article content
Passing path, arguments till arguments are null, envp][ string array.

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.


Article content
Passing arguments in execlp()

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.


Article content
Arguments in execv()

execv() uses a string of characters which includes arguments in it.

Article content
Output

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.



Article content
Passing Arguments in execve()

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.

Article content
Passing Arguments in execvpe()

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

To view or add a comment, sign in

More articles by Himanshu Naroliya

  • System V Semaphores

    Hey Everyone!! In the previous articles, we explored different Inter-Process Communication (IPC) mechanisms and also…

  • Message Queue - IPC Mechanism

    Inter-process communication (IPC) plays a vital role in modern operating systems, as it enables different processes to…

  • Understanding Shared Memory in Inter-process Communication (IPC)

    Hello Every One !! So far we discussed about many IPC Mechanisms that are helpful in sharing information between…

    1 Comment
  • Pipes as an IPC Mechanism II

    Hey everyone! In the previous article, we discussed pipes, how they work, how they create a channel for two processes…

  • Pipes as an IPC Mechanism

    Earlier, we discussed how two processes can communicate using shared memory with the help of files. However, that…

  • Understanding Library Calls (File Handling) in C

    In our previous article, we discussed various file handling techniques using system calls, which are useful for…

  • IPC's- Inter Process Communication & File Handling

    Article Insight's : What is inter-process communication? What is the need of inter-process communication? What are the…

  • Thread Scheduling & Concurrency Control

    Hey Every-One!! Today's Article Insight's Thread Scheduling What is Concurrency in Threads? What are the Problems in…

    1 Comment
  • Understanding The Concepts of Threads in C

    Hey!! Everyone, This Article explains about the basic concepts of Threads Why do we even need threads? Multi-threading…

  • Memory Management & Multi-processes

    Article Insights Memory allocation to Applications. Concept of virtual memory.

Others also viewed

Explore content categories