1

I am writing a small shell program that takes a command and executes it. If the user enters a not valid command the if statement returns a -1. If the command is correct it executes the command, however once it executes the command the program ends. What am I doing wrong that is does not execute the lines of code after it? I have tested execvp( command.argv[0], command.argv) with ls and cat commands so I am pretty sure it works. Here is my code.

  int shell(char *cmd_str ){
  int commandLength=0;
  cmd_t command;
  commandLength=make_cmd(cmd_str,  command);
  cout<< commandLength<<endl;
  cout << command.argv[0]<< endl;
  if( execvp( command.argv[0], command.argv)==-1)
//if the command it executed nothing runs after this line
  {
    commandLength=-1;

}else
{
  cout<<"work"<<endl;
}

  cout<< commandLength<<endl;
   return commandLength;


}
2
  • That is exactly how the exec* functions are supposed to work! They load the new program and replace your program with the new one. Commented Apr 13, 2012 at 14:38
  • 4
    @Aaron: You would expect airplanes pilots to at least read the manual of the plane they want to "work on". Why don't you have the same expectations about you as a programmer ?
    – ereOn
    Commented Apr 13, 2012 at 14:46

3 Answers 3

6

From man page of execvp(3)

The exec() family of functions replaces the current process image with a new process image

So your current process image is overwritten with the image of your command! Hence you need to use a fork+exec combination always so that your command executes in the child process and your current process continues safely as a parent!

On a lighter note I want to illustrate the problem with a picture as a picture speaks a thousand words. No offence intended :) :)

enter image description here

0

From the documentation on exec

The exec() family of functions replaces the current process image with a new process image. The functions described in this manual page are front-ends for execve(2). (See the manual page for > execve(2) for further details about the replacement of the current process image.)

If you want your process to continue, this is not the function you want to use.

0

@Pavan - Just for nit-pickers like myself, technically the statement "current process is gone" is not true. It's still the same process, with the same pid, just overwritten with a different image (code, data etc).

4
  • This should probably be made as a comment to @Pavan's question, or made a complete and correct answer.
    – crashmstr
    Commented Apr 13, 2012 at 17:08
  • English is a phunny language you know :) I rephrased my answer. Commented Apr 13, 2012 at 18:29
  • @crashmstr: I think that this guy is below the comment threshold.
    – Puppy
    Commented Apr 13, 2012 at 19:01
  • Indeed, this guy is below the comment threshold and can only comment on his own answers like this one.
    – Ya.
    Commented Apr 13, 2012 at 19:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.