【问题标题】:Waiting for system call to finish等待系统调用完成
【发布时间】:2015-08-17 20:07:09
【问题描述】:

我的任务是创建一个程序,该程序接受一个包含程序列表的文本文件作为输入。然后它需要在程序上运行 valgrind(一次一个),直到 valgrind 结束或直到程序达到最大分配时间。我让程序做我需要它做的一切,除了它不等待 valgrind 完成。我使用的代码格式如下:

//code up to this point is working properly
pid_t pid = fork();
if(pid == 0){
    string s = "sudo valgrind --*options omitted*" + testPath + " &>" + outPath;
    system(s.c_str());
    exit(0);
}
//code after here seems to also be working properly

我遇到了一个问题,孩子只是调用系统并继续前进,而无需等待 valgrind 完成。因此,我猜测系统不是正确的调用,但我不知道我应该打什么电话。谁能告诉我如何让孩子等待 valgrind 完成?

【问题讨论】:

    标签: c++ linux valgrind


    【解决方案1】:

    我认为您正在寻找 fork/execv。这是一个例子:

    http://www.cs.ecu.edu/karl/4630/spr01/example1.html

    可能会出现另一种选择。

    【讨论】:

      【解决方案2】:

      您可以forkexec 您的程序,然后等待它完成。请参阅以下示例。

      pid_t pid = vfork();
      if(pid == -1)
      {
          perror("fork() failed");
          return -1;
      }
      else if(pid == 0)
      {
          char *args[] = {"/bin/sleep", "5", (char *)0};
          execv("/bin/sleep", args);  
      }
      
      int child_status;
      int child_pid = wait(&child_status);
      printf("Child %u finished with status %d\n", child_pid, child_status);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-27
        相关资源
        最近更新 更多