杀死执行系统cmd的可执行二进制文件的进程树

时间:2019-08-13 15:07:44

标签: c++11 process kill-process

下面是我正在使用的示例“ TestBinary”

main()
{
...
   system("dd if=/dev/urandom of=/config/largeTestFile.txt count=1024 bs=11111111")
...
}

我有一个看起来像这样的主程序

int forkRet = fork();
if (forkRet > 0)
{
    //We are the parent process so save the experiments process
    testerProcId = forkRet;
    printf("Started configFill with process id: %d\n", testerProcId);
}
else if (forkRet == 0)
{
    //Run the experiment binary
    if (execv(testerBinary, args) < 0)
    //if(execl("/bin/sh", "sh", "-c", testerBinary, "argForBinary", NULL) < 0)
    {
        printf("Could not execv tester configFill, error: %s\n", strerror(errno));
    }
    exit(1);
}
kill(testerProcId, SIGKILL);

为了清楚起见,此设置最终创建3个进程,首先是主程序(A),然后(A)创建第二个进程(B),即TestBinary,TestBinary最终创建另一个进程(C)通过system(“ dd ...”)cmd。

我的问题是,当(A)试图杀死(B)时,我还需要它也杀死(C),而目前还没有。我还尝试将execl与-c选项一起使用,以使(C)在(B)死时死掉,但这似乎也没有启动我的TestBinary。

我很确定我想做的事是可能的,但是我不确定system()cmd是否使它不可能。

0 个答案:

没有答案