等待系统调用完成

Waiting for system call to finish

本文关键字:系统调用 等待      更新时间:2023-10-16

我的任务是创建一个程序,该程序接受包含程序列表的文本文件作为输入。然后,它需要在程序上运行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。下面是一个例子:

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

可以选择popen

您可以将程序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 %dn", child_pid, child_status);