argv 如何通过 execvp() 传递给新的进程映像?

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

手册页似乎没有具体说明这是如何完成的。我特别困惑,因为来自 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.

是否在替换过程映像之前将参数数组复制到某处?该行似乎暗示它们总是被修改,因为过程映像总是被替换。

我看到文档还说了以下内容: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.

我觉得这可以帮助我理解,但我不完全确定他们所说的在这里是不变的。

此外,我想知道为什么将 std::string 中的常量 c_str() 转换为 argv 中的 execvp() 是好是坏。

谢谢。

exec() 函数可能失败,return 错误指示。这个发生在调用进程中,调用进程继续运行。在这种情况下,exec() 会失败,就像任何其他系统调用一样,例如 open()read() 也会失败。这只是一个失败的系统调用。

你引用的意思是,在这种情况下,传递给 exec() 的字符串不会被修改。在 exec() return 之后,它们的内容不会被 exec() 调用本身改变。

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

不,过程映像并不总是被替换。如果您将不存在的路径名传递给 exec(),显然它不可能被任何东西替换,并且会出现错误 returned.