C Programming

Execvp Function in C

execvp() function belongs to the exec family. In today’s topic, we will discuss the characteristics and application of execvp() function under exec family.

The function of exec family in c language helps us execute other programs inside our program. To do that from within my program we use a set of function called exec family of function and these are in Linux program.

Application of execvp() function

If we want to execute a ping of program, we can apply this through execvp() function. For this, we have to use array. The letter ‘v’ used for execvp() function stands for vector.

Syntax

The syntax for execvp() function:

int execvp (const char *file, char *const)

Here, the first parameter is a file pointer which points a file that is to be executed in the program.

The second parameter is a pointer constant which is character in type & going to be used as null terminator. 

Programming Example 1

Here, we will see how execvp() function actually works in our program.

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<unistd.h> // including this file for Linux platform

int main(int argc,char *argv[])
{
char *ar[10] = {"ping","google.com","-c10",NULL} ; // declaration and                                    initialization of an array
    execvp("ping",ar) ;
    printf ( " It does not get any response as execvp () function executes first \n "
          // This statement does not run because execvp() takes control over the program if execvp returned success.
    return 0 ;
}

Output

Explanation
In this programming example, if we want to ping the “google.com” with help of execvp () function in the Linux platform, we have to include a header file named unistd.h in our program.

To execute the execvp () function, we have to declare an array (actually array of string) named ar [] and initialized some values like “ping”,” google.com” and NULL.

Now, if we will try to pass the array of argument inside the parenthesis of execvp () function we will notice it is pinging the google.com as an output.

Programming Example 2

Here we show another application of execvp () function.

#include<unistd.h> // including this file for Linux platform
#include<stdio.h>
#include<stdlib.h>
#include <errno.h>

int main()
{
    char *cmd = "cd" ;
    char *arr_ls[] = { "cd","/root/nothing",NULL} ;  // declaration and                                                                           initialization of an array
    errno = 0;

    int id = execvp(cmd,arr_ls) ;
    if( id == -1 )
    {
        printf("\nHere is the error number returned by execvp :%d \n", errno);

       if (errno == 2)
            printf("!!No such file or directory!!\n");
        else
            perror("execvp");
    }
    printf ( "If execvp returns success, we won't see this line else we will see this line. \n " ) ;
    return 0;
}

Output

Explanation
In this programming example, we will see if execvp () function will return false value. If execvp () function returns false value, then it printed the value of “errorno” which is 2 and print the message that “No such file or directory”.

If execvp () function returns true value, any statement after execvp will not be run. As we know from the characteristics of the execvp () function, no one line is executed when execvp () function takes control of the program.

Programming Example 3

In this programming example, we will see the important application of execvp() function related to the fork() or process.

#include<stdio.h>
#include<unistd.h>
int main()
{
    char *cmd = "ls" ;
    char *arg_ls[] = {"ls","-l",NULL};
    printf ( "Example of the execution of a statement before execvp() is invoked \n" ) ;
    printf ( "We are introducing one more process which indicates a fork \n" ) ;
    if( fork() == 0 ) //child process is introduced which is controlled by "ls-l"
    {
        int sts_cd = execvp(cmd,arg_ls) ;
        printf( "This statement executes that means there is an issue running the command \n" ) ;
        if( sts_cd == -1 )
        {
            printf( "execvp error! \n" ) ;
            return 1 ;
        }
    }
    else // parent program run here
    {
        printf ( "This statement executes that means back to main program \n" ) ;
    }
    return 0 ;
}

Output

Explanation
Like all the functions of exec family in c, execvp() function can execute other programs inside our program.

In this programming example, we declare a character array or a string name arg_ls[]. Inside this string, we initialize some values & NULL also. Then, we call another process called fork().

Here, execvp () function returns true value. So, fork () function can successfully execute its action. If not, then it will go to the main program.

Programming Example 4

Here we will see another feature of execvp () function.

#include<stdio.h>
#include<unistd.h>
int main()
{
    char *cmd = "cd" ;
    char *arg_ls[] = {"cd","/root/bam",NULL};
    printf ( "Example of the execution of a statement before execvp() is invoked \n" ) ;
    printf ( "We are introducing one more process which indicates a fork \n" ) ;
    if( fork() == 0 ) //child process is introduced which is controlled by "ls-l"
    {
        int sts_cd = execvp(cmd,arg_ls) ;
        printf( "This statement executes that means there is an issue running the command \n" ) ;
        if( sts_cd == -1 )
        {
            printf( "execvp error! \n" ) ;
            return 1 ;
        }
    }
    else // parent program run here
    {
        printf ( "This statement executes that means back to main program \n" ) ;
    }
    return 0 ;
}

Output

Explanation
The above programming example is the modified version of programming example 3. Here, we will see if function fork () will return ‘0’. Then, the child process will execute their code and is controlled by “ls-“l command. The value that will return execvp () is assigned to a variable named sts_cd. If sts_cd will see the value -1, then we will understand that it is an error of execvp () function. Otherwise, this function takes control over the rest of the program.

Conclusion 

From the above discussion on the implementation of execvp () in the exec family, we have come to the conclusion that it replaces a process image with a new image in our ram (Random Access Memory) to execute the new process. Actually, this function helps our operating system to manage all the processes that are ready to execute by the processor with less time.

About the author

Bamdeb Ghosh

Bamdeb Ghosh is having hands-on experience in Wireless networking domain.He's an expert in Wireshark capture analysis on Wireless or Wired Networking along with knowledge of Android, Bluetooth, Linux commands and python. Follow his site: wifisharks.com