program stops after execvp( command.argv[0], command.argv)

后端 未结 3 624
闹比i
闹比i 2021-01-23 23:55

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 exe

相关标签:
3条回答
  • 2021-01-24 00:18

    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 讨论(0)
  • 2021-01-24 00:23

    @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).

    0 讨论(0)
  • 2021-01-24 00:30

    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 讨论(0)
提交回复
热议问题