Home > Mobile >  How is argv passed to the new process image with execvp()?
How is argv passed to the new process image with execvp()?

Time:02-27

The man page does not seem to specify how this is done. I am confused particularly because of this line from here: The argv[] and envp[] arrays of pointers and the strings to which those arrays point shall not be modified by a call to one of the exec functions, except as a consequence of replacing the process image.

Are the argument arrays copied somewhere before the process image is replaced? That line seems to imply that they are always modified because the process image is always replaced.

I see that the document also says the following: The statement about argv[] and envp[] being constants is included to make explicit to future writers of language bindings that these objects are completely constant.

I feel like this could help my understanding but I'm not entirely sure exactly what they are saying is constant here.

In addition I would like to know why it is good or bad to cast the constant c_str() from std::string to pass to execvp() in the argv.

Thanks.

CodePudding user response:

exec() functions can fail, and return an error indication. This occurs in the calling process, and the calling process continues to run. In this case, exec() simply fails, just like any other system call, like open() or read() can fail. It's just a failed system call.

All that what you quoted means is that in this event the strings that were passed into exec() do not get modified. Upon return from exec() their content remains unaltered by the exec() call itself.

That line seems to imply that they are always modified because the process image is always replaced

No, the process image is not always replaced. If you pass in a pathname to exec() which does not exist, it should be apparent that it can't possibly be replaced by anything, and an error gets returned.

  • Related