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
38
votes
5 answers

g++: error trying to exec 'cc1plus': execvp: No such file or directory

I am using ubuntu 12.04. I'm trying to "make" a project. I get this error: g++: error trying to exec 'cc1plus': execvp: No such file or directory I have g++ installed.
user2824393
  • 639
  • 3
  • 10
  • 19
30
votes
2 answers

Why is argv parameter to execvp not const?

execvp is defined thus: int execvp(const char *file, char *const argv[]); Which precludes code such as this from being used: const char* argv[] = {"/bin/my", "command", "here", NULL}; execvp(argv[0], argv); Was this an accidental omission? Is it…
Jonathan Mayer
  • 1,432
  • 13
  • 17
26
votes
2 answers

How to use execvp()

The user will read a line and i will retain the first word as a command for execvp. Lets say he will type "cat file.txt" ... command will be cat . But i am not sure how to use this execvp(), i read some tutorials but still didn't get it. #include…
Axl
  • 273
  • 1
  • 3
  • 7
18
votes
3 answers

fork after malloc in parent... does the child process need to free it?

Answers to questions in your head: Yes, this is for school. No, I can't use threads for this. And yes, I looked for an answer and some people said "yes" and others said "no." I'm also fact-checking my professor because I don't want to unfairly…
user3598200
  • 181
  • 1
  • 1
  • 5
16
votes
2 answers

Classic C. Using pipes in execvp function, stdin and stdout redirection

I want to simulate bash in my Linux C program using pipes and execvp function. e.g ls -l | wc -l There is my program: if(pipe(des_p) == -1) {perror("Failed to create pipe");} if(fork() == 0) { //first fork close(1); //closing…
krzakov
  • 3,871
  • 11
  • 37
  • 52
12
votes
2 answers

Fatal error: cannot execute 'as': execvp: no such file or directory

Whenever I try to compile c/cpp files it gives this error: gcc: fatal error: cannot execute ‘as’: execvp: No such file or directory compilation terminated. I have also tried to include full path of file while compiling but same error occured. Just…
Sankalp
  • 153
  • 1
  • 2
  • 9
11
votes
5 answers

Creating a child process WITHOUT fork()

Is there a way to start a child process without fork(), using execvp() exclusively?
gonidelis
  • 885
  • 10
  • 32
11
votes
3 answers

Best practice to use execvp in C++

At the beginning, I wrote something like this char* argv[] = { "ls", "-al", ..., (char*)NULL }; execvp("ls", argv); However, GCC popped up this warning, "C++ forbids converting a string constant to char*." Then, I changed my code into const char*…
Kevin Dong
  • 5,001
  • 9
  • 29
  • 62
10
votes
2 answers

Writing a simple shell in C using fork/execvp

I have to develop a simple shell in C using system calls fork()/execvp(). So far my code takes in a command, splits it up using strtok into an array argv and then I call fork to create a child and execute the command. Im working on this in ubuntu…
C1116
  • 173
  • 2
  • 5
  • 16
9
votes
3 answers

Difference between exec, execvp, execl, execv?

I am writing a code that represent a new shell to Linux. One of the commands I want to support is running a process for example if I get the following line command [arguments] Then I want to run command as a process until it finishes running the…
user7886490
  • 115
  • 1
  • 1
  • 5
8
votes
5 answers

exec() any command in C

Say in C, I want to call execvp() on any string command. Command can just be: char command[] = "ls -l"; char command[] = "rm *.txt"; char command[] = "cat makefile"; I want to put this command variable inside execvp(). So the exec() flavored…
JJ Liu
  • 1,351
  • 8
  • 20
  • 36
8
votes
3 answers

Linux Command to Show Stopped and Running processes?

I'm presently executing the following Linux command in one of my c programs to display processes that are running. Is there anyway I can modify it to show stopped processes and running ones? char *const parmList[] =…
Vimzy
  • 1,871
  • 8
  • 30
  • 56
8
votes
4 answers

Does control return after "execvp()"?

if(pid == 0) { execvp(cmd, args); // printf("hello"); // apparently, putting this or not does not work. _exit(-1); } else { // parent process work } "execvp()" replaces the current program with the to-be-execed program (of…
Ajay Garg
  • 79
  • 1
  • 1
  • 4
8
votes
3 answers

Passing an array to execvp() from the user's input

I'm trying to pass arguments entered by the user to execvp(). So far I've split up the string. If the user types ls -a, temp is saved as "ls" and "-a" followed by a NULL character. I'm not quite sure how to point to this properly in execvp. In…
caerulean
  • 95
  • 1
  • 2
  • 9
8
votes
3 answers

C - Executing Bash Commands with Execvp

I want to write a program Shellcode.c that accepts in input a text file, which contains bash commands separeted by newline, and executes every commands in the text file: for example, the text file will contain: echo Hello World mkdir goofy ls I…
elmazzun
  • 1,066
  • 2
  • 18
  • 44
1
2 3
24 25