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

how to use a file descriptor in a child process after execvp?

I am trying to open a child process using fork(), and then execvp()-ing into another program. I also want the parent process and the child process to communicate with each other using a pipe. here is the parent process - int…
Viktor Karsakov
  • 157
  • 3
  • 10
2
votes
1 answer

Execvp works with "ls -l | wc" and failed with "ls -l|wc

I am writing a shell and currently implementing the pipe process. I am able to run "ls -l | wc", but when running "ls -l|wc" the process fails. I have tried debugging with gdb but unable to make anything of it as they both seem as they are equal…
Continuum
  • 67
  • 1
  • 9
2
votes
1 answer

how to pipe inside a while loop multiple commands

suppose there are multiple commands passed by a user on shell say command 1 | command 2 | command 3 | command 4 so I have written a sample program which reads command 1|command 2 inside char str[] (right now for simplicity I have hard coded the…
ss321c
  • 689
  • 2
  • 11
  • 23
2
votes
1 answer

How to create multiple ordered processes using fork() and execvp() in C?

I am trying to create multiple processes using fork() and execvp() calls, but so far I've been unsuccessful. Here is what I am trying to do: Processes A, B, C should run at the same time. When they are finished, process D should run. When it is…
user246392
  • 2,661
  • 11
  • 54
  • 96
2
votes
1 answer

execvp() prints invalid option -- '

I am creating simple shell interpreter. I am creating this shell using a scanner and a parser using lex and yacc utility. The problem arises when i give an argument to command as it returns invalid option.For example when I type in ls -l it returns…
2
votes
1 answer

C fork loop to exec infinite commands (urandom/tail on an active file)

I'm trying to reproduce the behaviour of a pipe in a unix (OSX) shell (like bash) with C. I have no problem to handle a command which is not infinite (for example: ls | wc -c). But as you can imagine, I have an issue, I can't handle an infinite…
Ogagal
  • 23
  • 3
2
votes
0 answers

dup2 for Input File Redirection Not Working

I am relatively new to C, and am working on an assignment in which we are required to write a shell. The shell must have most standard functionality, including file redirection and piping. File redirection must be done using dup2, and I have been…
2
votes
1 answer

Can't exec other programs in a proper way

I am trying to execute another program from within my own, but i don't understand a few things. So i wrote this code: void run() { cout << "##Run" << endl; pid_t process_id = fork(); if (process_id == 0) { char* args[] = { "sudo", "ls",…
Kamil Bęben
  • 1,064
  • 8
  • 12
2
votes
1 answer

Create multiple child processes and run execvp

I have a function in C which creates a child process and makes it run execvp. int Execute(char **arg) { pid_t pid; int status; if ((pid=fork()) == 0) { execvp(arg[0],arg); perror("Execvp error"); exit(1); …
Dandry
  • 495
  • 12
  • 26
2
votes
2 answers

C directing stdout to file and then resetting

I would like to change the file descriptor so that my execvp commands are sent to a file. Afterwards, I would like to reset stdout back to the console. This is what I have so far: int main() { printf("to console\n"); int stdoutfd =…
dinamix
  • 651
  • 8
  • 22
2
votes
1 answer

Scheduling Policy in an exec system call after a fork

Suppose I have a child process, whose scheduling policy I have set to be SCHED_BATCH, using the C library's function, sched_setscheduler, and now, this child process creates an external process using the execvp system call. Will the Scheduler of the…
Jarvis
  • 8,494
  • 3
  • 27
  • 58
2
votes
1 answer

Pipe function in Linux shell write in C

My mini-shell program accepts pipe command, for example, ls -l | wc -l and uses excevp to execute these commands. My problem is if there is no fork() for execvp, the pipe command works well but the shell terminates afterward. If there is a fork()…
whatthekey
  • 23
  • 4
2
votes
2 answers

How to save execvp output

I have problem with saving the execvp output.I want to save the output of (ps -eo pid,fname,state,ppid,gid,sid) in txt file . This is my code : #include int main(void) { char* args[]={"ps","-eo","pid,fname,state,ppid,gid,sid" ,…
2
votes
1 answer

How can I execute cat command using execvp or other kinds of exec functions in programming C

I have a problem which needs to be solved. I have a string array like : argv[]={"cat","file1.txt","file2.txt"},then I need to use execvp to execute the execvp(argv[0],argv),but I feel why it doesn't work out in my program. I am not sure whether I…
user144600
  • 529
  • 2
  • 7
  • 18
2
votes
3 answers

how to use execvp--finding the file argument

I wrote a test program to try and understand how to use execvp(), but I keep running into a problem. The relevant part of my code is: ... printf("execute: 'ls -a'\n"); char *args[2]; args[0] = "/bin/ls"; args[1] = "ls"; args[2] = "-a"; ... } else…
Tommy K
  • 1,759
  • 3
  • 28
  • 51