Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use execv system call in linux?

Tags:

c

linux

unix

exec

I am writing a program using execl to execute my exe file which is testing and it's work very well and display the output in the Linux CLI. But I have not idea how to change the execl to execv, although I know both of the system call will give the same value. I am confused with the array argument for execv system call

This is my execl sample program

int main(void)
{
   int childpid;
   if((childpid = fork()) == -1 )
{
   perror("can't fork");
   exit(1);
}
 else if(childpid == 0)
{
  execl("./testing","","",(char *)0);
  exit(0);
}
else
{
printf("finish");
exit(0);
}
}

can I know how to change the execl to execv. What I read from online, we must set the file path for my exe file and the argument of array . What type of argument need to set for the array in order to ask the program to execute the testing exe file ? https://support.sas.com/documentation/onlinedoc/sasc/doc/lr2/execv.htm

Is it the link consist of the thing I want ? But what I read from it ,the command is request the list the file,not execute the file. Correct me I make any mistake

like image 717
doe Avatar asked Aug 21 '15 13:08

doe


1 Answers

In order to see the difference, here is a line of code executing a ls -l -R -a

  • with execl(3):

    execl("/bin/ls", "ls", "-l", "-R", "-a", NULL);
    
  • with execv(3):

    char* arr[] = {"ls", "-l", "-R", "-a", NULL};
    execv("/bin/ls", arr);
    

The char(*)[] sent to execv will be passed to /bin/ls as argv (in int main(int argc, char **argv))

like image 195
4rzael Avatar answered Oct 06 '22 02:10

4rzael