调用execv之后的怪异行为

ani*_*tar 2 c linux execv

我正在尝试使用execv执行命令。运行该程序后,我可以看到“将要调用execv!”语句。在标准输出上打印。

我还可以从程序“ leaky”中看到打印内容。实际上,一切都按预期工作,除了在if或else块中看不到打印语句外,即“ execv failed!error:”和“ Valgrind成功执行!” 在输出上打印。

我在这里是否缺少关于execv的明显要点?

#include<stdio.h>
#include<errno.h>
#include<string.h>
#include<unistd.h>

int main()
{
   char *args[5] = {"/usr/bin/valgrind", "--leak-check=full", "--log-file=valgrindReport.txt", "./leaky", NULL};
   printf("Going to call execv!\n");
   if(execv("/usr/bin/valgrind", args) < 0)
   {
      printf("execv failed! error: %s\n", strerror(errno));
      return 0;
   }
   else
   {
      printf("Successfully executed valgrind!\n");
   }
   return 0;
}
Run Code Online (Sandbox Code Playgroud)

Wer*_*nze 5

如果您得到valgrind的输出,那么显然execve是成功的。如果execve成功,它将用要启动的过程映像替换当前过程映像,并且不会返回。如果execve返回则失败。