Search code examples
c++linuxforkexecvp

c++ issue with using commandline inputs in a forked execvp


When running the code below I get an incorrect output. I am fairly sure I am doing something wrong with the execvp call as I have tried changing everything and only this seems to be responsible. As far as I know the parameters for execvp should be the filename and the array which I pass using execvp(argv[0], argv). Replacing these parameters with things such argv[1] seems to change the output which is why I think I am doing something wrong here.

a.out file -> lab1 (g++ lab1.cpp -o lab1)
folder -> lab1
.cpp file -> lab1.cpp

current output:

steve@fish lab1]$ lab1 ls -l
lab1 ls -l Parent process id is: 1747646
lab1 ls -l Child process id is: 1747647
[vnath@storm lab1]$ lab1: cannot access 'ls': No such file or directory

What output should be:

steve@fish lab1]$ lab1 ls -l
lab1 ls -l Child process pid is 7302
total 30
-r-xr-xr-x 1 steve staff  3567 Aug 17 07:45 lab1
-rwxr--r-- 1 steve staff   547 Aug 17 08:00 lab1.cpp
-rw-r--r-- 1 steve staff 11291 Aug 17 08:15 lab1.html
-rw-r--r-- 1 steve staff   621 Aug 17 09:59 linux.txt
lab1 ls -l Parent process pid is 7301

code:

#include <iostream>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>

using namespace std;

int main(int argc, char* argv[])
{
  int ppid = 0;
  int cpid = 0;
 

  pid_t pid = fork();

  if(pid == -1) // error handling
  {
    perror("fork");
    return 1; // added
  }
  if(pid == 0) // child process
  {
    //cpid = pid;
    cpid = getpid();

    for(int i = 0; i < argc; i++)
       cout << argv[i] << " ";

    cout << "Child process id is: " << cpid << endl;

  
    if(execvp(argv[1], argv) < 0)
    {
      perror("exec");
      return 1;
    }
  }

  if(pid > 0) // parent process
  {
    ppid = getpid();

    for(int i = 0; i < argc; i++)
       cout << argv[i] << " ";

    cout << "Parent process id is: " << ppid << endl;
  }

  return 0;
}                

Solution

  • For anyone curious turns out execvp(argv[1], &argv[1]) is the key to get it all working.