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
0
votes
1 answer

Does using execvp concatenate stdin?

Does execvp() concatenate stdin, and then execute? I don't completely understand this command A classmate of mine is letting me look at his code(a shell that executes in the terminal) to figure the whole thing out, but I'm not quite understanding…
Chris Phillips
  • 1,997
  • 2
  • 19
  • 34
0
votes
1 answer

WHy are my processes behaving like this?

it's some implementation of linux shell in c. Since i have added background process support i have some output that i fail to understand. Here is the code: #include #include #include #include #include…
user2966439
  • 307
  • 1
  • 5
  • 14
0
votes
1 answer

running pico using execvp after dup2 in C

What I want to do is basically make standard out of the child process write in the write end of the pipe using dup2 and run pico using execvp and the parent would read the read end of the file and do something with it and write it out in the…
yellow
  • 1
  • 1
0
votes
0 answers

Splitting string into char array pointer

I am using user input to pass as an argument to execvp. If I do this as my argument for execvp it works: char *arr[]={ "ls",NULL }; When I try to split the user input into the array above it won't let me store the words into the array with this…
Xilonian
  • 13
  • 4
0
votes
1 answer

run a command with execvp in c program

I want to run this command with execvp gcc file1.o file2.o file3.o I created a tab wich contain this : char * tab = {"file1.o", "file2.o", "file3.o", NULL }; when I call execvp like that : execvp("gcc",tab); I have this error : file1.o: erreur…
rabah Rachid
  • 89
  • 1
  • 8
0
votes
1 answer

Error with fork, execvp in C?

I'm having trouble with execvp and fork. When I run the code, exec doesn't work even when the file is created. It simply returns a "Bad address" error. Also, the printf("in the child") does not appear when I run it. How can my code get to execvp…
user2900984
  • 23
  • 1
  • 5
0
votes
1 answer

C - error when attempting to pass /bin/ls to execvp

I am working on a C program that needs to be able to execute certain commands using execvp, and I have implemented this with: execvp(arguments[0], arguments); where arguments[] is an array of stings. For the most part, my implementation works fine -…
DTR
  • 372
  • 6
  • 22
0
votes
1 answer

Implementing a shell: wrong argument in execvp()

Recently, I am trying to make good practice on C now I come to a command execution. It tells me that I should us char**? But am I have some concept wrong. Please help me. Code is here int excution(int cnt, char token[][BUFSIZE],int *t_token) { …
C learner
  • 53
  • 2
  • 9
0
votes
1 answer

Can't get control back after execvp and wait()

I'm coding a small shell that must execute my commands that I parse. f is a char** like this: [ls][-la] p is the same, used like this: [wc] So I tried to pipe ls -la in wc. My probleme is that when I execute "ls -la | wc && date", which works well…
user2145240
  • 95
  • 1
  • 3
  • 9
0
votes
0 answers

Forked execvp does not return, parent waits indefinitely

Code with some comments: http://pastebin.com/nrXayHx5 I'm executing the command "du" repeatedly on a lot of files and directories, it works for a lot of my input but there's one particular directory that doesn't, i checked which one it was and the…
hbgranja
  • 55
  • 1
  • 5
0
votes
0 answers

C Inter Process Communication

I am trying to write a program which take n number of processes and pass the output of each parent process to the input to the the child . I guess that the problem is in closing the pipes . One example of input is : ./a.out "ps ufax" "grep color"…
Anis Nouri
  • 21
  • 5
0
votes
1 answer

execvp won't execute a command

I am trying to run a piece of code which will execute a few UNIX commands, these commands are stored in the array lineArray which for instance: lineArray = {"ls -l", "ls", "pwd", NULL}; the problem is the this code will only print out the first…
Steinfeld
  • 649
  • 2
  • 10
  • 20
0
votes
1 answer

exevp skips over all code until wait call in c

I am trying to execute a file using fork and execvp, however I am encountering some errors. I have not found any solutions to the problem I am having here online, since I don't get any errors from my exevp nor does it run. Here is my code: pid_t…
Ameya Savale
  • 431
  • 1
  • 8
  • 21
0
votes
3 answers

Losing control after execvp(grep)

I'm trying a write a little program to run grep via execvp. This is basically identical to the problem I had here, but in this case, it only happens when I run grep (as opposed to echo, ls, emacs, etc which all work fine) and I've changed the…
Daniel
  • 2,435
  • 5
  • 26
  • 40
0
votes
2 answers

execvp() not accepting user input

I have the following code: int main(void) { char in[100]; printf("> "); fgets(in, sizeof(in), stdin); int pid; char *f[100]; char *v; int i = 1; v = strtok(in, " "); f[0] = v; while (v = strtok(NULL, " ")){ …
me45
  • 1,059
  • 3
  • 14
  • 16