C Programming

C: execve function usage

Execve() function is used for the execution of the program that is referred to by pathname. The exec family is used mainly in the C programming language and has many functions. These functions execute a system command in a separate process from the main program and print the output. In this article, we will discuss some of the main functions of the exec family and mainly execve functions with some elementary examples.

Now let’s look at the exec family in the image attached. This picture shows the syntax of all possible functions of the exec family.


From each name of the exec family in C, you can observe that the base of each function is exec(execute), followed by further one or more letters/alphabets.

e: ‘e’ letter denotes a pointers array that refers to the environmental variables, and this is passed to the new process to overlay the previous one.

l: This letter shows the ‘command-line arguments’ passed individually or in the form of a list to the function.

P: It utilizes the path variable of the environment used to find the file’s name in the file arguments so that it can be executed.

V: Similar to ‘l,’ command-line arguments are passed to the relevant function in the form of a vector array or array of pointers.

From the image mentioned above, you have seen that each function uses some specific argument as a parameter. We will explain them here to demonstrate the working of each.


This argument is the one that shows the name of the path of the file for the execution of a new process. Those arguments beginning at arg0 pointers point to the arguments to be passed to the newly created process. The value of argv is an array of pointers pointing towards the arguments.


The very first argument arg0 must be the name of the editable file. Some programs cannot correctly rely on this parameter because they have provided the wrong location of the executable files. But we cannot guarantee this as it is not a benchmark in the platform of the exec family.


This argument envp is an array of pointers pointing towards the environment’s settings. The system called exec() having the name that ends with the letter ‘e’ is used to alter the environment for the new process. This is done by passing a list of the environment’s settings through the envp argument. That is how this parameter helps the exec system call. This argument is an array with character pointers, in other words, a character array. Each element in the array points to a null-terminated string that defines an environment variable.

Execve ()

The execve function is mainly used to smear(overlay) a process running because of the call to fork(). This makes the program that is currently running by the process called it to get replaced with another new program, involving a newly initialized heap, stack, and other data segments. Execve() executes the program that is allotted by a filename. The filename must be a script that starts with a line having a hash “#” or a binary executable.

Effects of execve()

A file descriptor opens when an execve system call is made and remains open in the new process until it is closed by fcntl. This is the aspect used to specify the standard streams like stdin, stdout, and stderr for the new program. When the overlay of the new process is successfully done, then the memory address space for the previous method and all memory area that was not shared was isolated and again returned to the operating system. At the same time, the data that is not shared with the new program is lost.

Return value of execve()

When execve() is successfully processed, it does not return value. The successful exec replaces the current process and cannot return anything to the program through which the call is made. These processes also have an exit status, but the parent process collects the value. If the execve is returning something to the prom that has been called, it means an error has occurred, and the returning value is -1. And errno includes the values like E2BIG, ENOMEM, EACCES. These errors occur when the list of arguments is above the system limit. There is insufficient memory available that executes the new process, or the relevant file has violated the rules of sharing and locking.

Implementation of execve()

We have implemented the Ubuntu Linux operating system source codes to share some examples. The tools used for the demonstration include a text editor, and for the output, we have used the Ubuntu terminal.

Example 1

First, we have used a library that includes the information regarding execution for all the exec functions.

# including <uninstd.h>

Then in the main program, a binary path is provided that is then used as the constant argument. Other arguments in the parameter include the hostname and the port number. The constant environment consists of the website address. While using the execve system call, the binary path, argument, and the environment variable are used as a parameter.

We use a gcc compiler to compile the code.

$ gcc –o exec exec. c

After compilation, use the below-written command.

$ ./exec

“Exec.c” is the name of the file.

The resultant value is shown below. It will include the constant argument that contains the hostname and port number from the environment variable.

Example 2

Unlike the previous example, here we have the involvement of two files. One is an exec file that contains only a display message. Save the file with a “.c” extension to create an executable file.

$ gcc EXEC.c –o EXEC

After that, create another demo file with the “sample.c” name. Write a C code inside it having an exec() function. In the main program, while introducing the arguments, we call the file’s name we have created “EXEC.c.” Then, by making a function call of execve(), use that call as an argument. And then print a statement ‘ending…’. This printf command will be executed only when the “EXEC.c” file is not executed successfully; when the execve () is called, all the statements written after it is ignored. The process ‘sample.c’ is replaced with “EXEC.c.”

Now execute the rest of the commands. You will see that by executing the “sample.c” file, the statement of “EXEC.c” is displayed. This example is an excellent example of execve() in C.


The article ‘C: execve function usage’ is an article that contains some basic descriptions of all the elements in the exec function family. We have explained the functionality of execve in a detailed manner with the help of some basic examples. The arguments that play an essential role in the description and declaring of execve function are also discussed here.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.

Page was generated in 0.056107044219971