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

Executing a program arguments with C and evecvp failing to forward correctly

I am trying to execute a program with the execvp function within an overseer and client distributed system. The client sends a program to be executed with the arguments: char buf[500]; int bytes_recieved = 0; char array[1][26]; bytes_recieved =…
James
  • 3
  • 1
-1
votes
1 answer

How can I make a process of a program?

Hey I have to write a small process launcher for uni. #include #include #include int main(int argc, char* argv[]){ pid_t pid; if((pid = fork()) < 0){ return 0; } else if(pid == 0){ …
Jan Wolfram
  • 145
  • 8
-1
votes
1 answer

Shell in C - how to read and execute user input?

My assignment is to write a very simple shell in C. I don't have many sources given and I have just started learning C, I have used only scanf() and printf() and coded simple functions. I only know it supposed to be written with fork() and different…
L.krez
  • 31
  • 5
-1
votes
2 answers

execvp find wildcard -name "*.c"

How can you run execvp with a "*.c". I can get it to work with a full name but not a wildcard. Any help would be greatly appreciated. This is what I have so far. #include #include #include #include…
code kid
  • 374
  • 1
  • 6
  • 22
-1
votes
2 answers

Why does my code exit with exit code: 0 even though the loop condition is still valid?

I am trying to use fork with execvp to run two shell commands concurrently. I have two problems which are when I input mkdir folder1&mkdir folder2, it creates a folder named folder1 and another folder named folder2? (the question mark is included in…
Ambitions
  • 2,369
  • 3
  • 13
  • 24
-1
votes
2 answers

execvp isn't reading the entire argv[]?

I have something like a basic shell. My problem is that when i do something like ./test ls -l it doesnt use the -l argument. It's exactly the same as if i did ./test ls. According to man, int execvp(const char *file, char *const argv[]), so isn't…
ohiohai
  • 73
  • 8
-1
votes
1 answer

adding parameters to execvp from char array in c

I have an array of strings containing the input parameters for execvp. How can I turn it into an array of string pointers for execvp? For command with one argument, two strings are present: char param[4][10] = ["wc","file.txt"] And with two…
Safwan Ull Karim
  • 652
  • 5
  • 10
  • 20
-1
votes
1 answer

Why any thing that comes after execvp or the exec* family of functions will not be executed?

So I know that anything that comes after exec* functions will not get executed (if the exec* call is successful of course). I want to understand why is this so ? So I developed this tiny little program #include #include #include…
Pro
  • 173
  • 4
  • 15
-1
votes
1 answer

execvp won't work with cut

I'm making work to school and I have a problem with "cut" command when I try launch this command in c++. So this is my exercise-> I want launch this command in C++ -> "cut -d':' -f5 < file" I wrote text from the file to variable input in main…
Shoxik
  • 119
  • 1
  • 7
-1
votes
1 answer

C Passing one argument and two parameters in execvp

I'm working on a simple shell for my school project and atm I am trying to pass two input paramaters to be used with a command (ex: ls /home/ -l) as I can only pass 1 atm. Meaning whatever goes after "/home/" doesn't get executed. I've tried this to…
-1
votes
2 answers

Running commands from child process

I want to make a shell where the child process runs linux commands(with the help of execvp) such as "ls" etc. .I also want to be able to run commands with arguments like "ls -a" or "ls -l /tmp" Parent must wait for child to execute the given command…
purkavlos
  • 11
  • 3
-1
votes
1 answer

C++ - Function Call To execvp Returns Cannot Access Error In C++ Shell Program

I am writing a simple shell program in C++. When I pass my arguments into execvp, in particular, for the ls command, I receive a ls: cannot access H��p����: Protocol error. A similar error occurs for other commands as well. My strategy is the parse…
iknowhtml
  • 17
  • 7
-1
votes
1 answer

How do I output execvp on a single line?

I need help, i am trying to create a shell in minix3, and i want to create my own "ls" command, so at the moment i am using the execvp command. The only problem is that i would like it to all output on a single line, instead of each directory on…
-1
votes
1 answer

Trying to use execvp to execute external commands in C (in a shell like program)

so I had to implement support for executing external commands(for linux in the C programming language). This is what I have so far, I used the readline library for the history functions but that is irrelevant... can someone tell me what am I doing…
Theo
  • 1
  • 1
-2
votes
1 answer

How to use child process in c

I have a problem figuring out how to use active child processes after they are created in the fork(). I see from another terminal that they are still active until I execute the exit success. If I wanted to say for example: I want to make the child…
merli
  • 3
  • 4
1 2 3
24
25