Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 正在等待系统调用完成_C++_Linux_Valgrind - Fatal编程技术网

C++ 正在等待系统调用完成

C++ 正在等待系统调用完成,c++,linux,valgrind,C++,Linux,Valgrind,我的任务是创建一个程序,将包含程序列表的文本文件作为输入。然后需要在程序上运行valgrind(一次一个),直到valgrind结束或程序达到分配的最大时间。我让程序做了我需要它做的一切,除了它没有等待valgrind完成。我使用的代码具有以下格式: //code up to this point is working properly pid_t pid = fork(); if(pid == 0){ string s = "sudo valgrind --*options omitt

我的任务是创建一个程序,将包含程序列表的文本文件作为输入。然后需要在程序上运行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完成吗?

我想你在找fork/execv。以下是一个例子:


另一种选择是popen。

你可以
fork
exec
你的程序,然后等待它完成。请参见下面的示例

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);