Questions tagged [execvp]

execvp, is a POSIX specified function from the exec* family, which replaces the current process with one specified. Questions using this tag should be related to the use and semantics of this function.

execvp is a POSIX function, from the exec* family, which replaces the current process with one specified.

It is available on most every UNIX-like platform, including Linux and Mac OS X, where a unistd.h file exists, and many languates, such as Python, emulate this function on platforms, such as Windows, where this function does not normally exist. Many languages, including C, C++, and Python, have interfaces to this function.

If it succeeds, it will not return and the current process will be replaced. If it does not succeed, then it will return a -1.


POSIX specifies that this function has the signature:

int execvp(const char *path, char *const argv[]);.

Its arguments are interpreted as follows.:

  • path: This is a NULL terminated string which holds the name of an executable. It is specified that execvp will search the PATH environment variable for this name if it is not a path.

    If it is a path, then execvp will look for the executable where specified, using an absolute path if this argument begins with a /, or a relative one if this argument doesn't.

Example: ping will resolve to /bin/ping.

  • argv: This is an array of NULL terminated strings, which make up the argv of the process in the path argument.

    This array should end with a NULL, and should start with the name of the executable passed in the path argument.

Example: {"ping", "-c", "5", "www.google.com", NULL}


POSIX includes the following complete example for reference:

The following example illustrates the use of execvp to execute the ls shell command:

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

main()
{    
     pid_t pid;
     char *const parmList[] = {"/bin/ls", "-l", "/u/userid/dirname", NULL};

     if ((pid = fork()) == -1)
          perror("fork() error");    
     else if (pid == 0) {
          execvp("ls", parmList);
          printf("Return not expected. Must be an execvp() error.\n");    
     }
}
368 questions
-2
votes
2 answers

Child process takes over parent process when running fork()

I have a problem where I am trying to run multiple commands through execvp(). To do this, I have a while loop doing through each command, parsing it, then calling a function to use fork + exec. The issue is, I will run fork + exec, and while I wait…
B.M. Corwen
  • 205
  • 3
  • 10
-2
votes
1 answer

fork() and execvp() commands in c

I am writing a C program. All code is in while loop but I did not add it yet. It takes input from user like start ls -l and run ls -l. And wait new command. If user write "wait" and there is a process that is not end, It waits until the child…
Emin Çiftçi
  • 139
  • 2
  • 16
-2
votes
1 answer

Multithreading app that lauches and monitor another executable

I found a code that launches a code using fork and execvp but then by digging the web I realized it was not compatible with multithreading so now I am quiet confused... "the Pthreads standard specifies that an exec call from any thread must…
Phil
  • 708
  • 1
  • 11
  • 22
-2
votes
3 answers

execve() and execvp() are ignoring the first argument

This is for an assignment. My code: #include #include #include int main(void){ int run=1; while (run) { int perror; pid_t pid; char in[1536]; char para[512]="/bin/"; char…
DeafFrog
  • 75
  • 9
-2
votes
1 answer

how to write ls command in c using execvp

i am trying to write a simple ls command so i'm input "ls r" and i want to check if my code is allright. the output does not showing anything. this is the code: int main(void){ char *line; char *args[16]; pid_t pid; line =…
tokenaizer
  • 208
  • 1
  • 5
  • 17
-2
votes
1 answer

what's the prob with this code?

Am trying to call evecvp() with these arguments: vectorsubcommand; void parse(char *str) { pid_t pid; char *cmd1=(char *)malloc(sizeof(300)); cmd1=strtok(str," "); while(cmd1!=NULL) { subcommand.push_back(cmd1); …
Manish Jindal
  • 293
  • 3
  • 11
-4
votes
1 answer

Execvp Only runs the first command correctly

I am making a simple shell by using the fork and execvp. When I run my program and I type in a command like ls or ls -l it works just how I like it. When that gets done my program is sitting there waiting for another command, but when I put in…
DrakeJacks
  • 187
  • 1
  • 2
  • 8
-6
votes
1 answer

making a shell with C

i am trying to create a shell using C as a homework but my execvp() doesnt work . it doesnt exe the ls. i am working at a virtual machine lubuntu 32 Thas my outpout any my error message ! if(pid==0){ printf("child"); …
Panos
  • 77
  • 1
  • 7
1 2 3
24
25