1
我是Linux编程的新手,我想了解一些关于使用execvp()
开始的进程的建议。以下是启动"TestApplication"
作为子进程的代码。当用户中断(ctrl + C)时,我想杀掉"TestApplication"
以及父进程。如何获取通过execvp启动的进程的进程ID()
有关如何实现此目的的任何建议。 PLS。帮帮我。谢谢。
int main(int argc, char* argv[])
{
signal(SIGINT, KillProcess);
pid_t pid;
pid = fork();
if(pid == -1)
{
printf("Error: Fork process failed");
exit(-1);
}
else if (pid == 0)
{
char *const paramList[] = {"5"," 1", NULL};
execvp("TestApplication", paramList);
}
else
{
// Wait for signal from the TestApplication process when successfully executed
}
return 0;
}
void KillProcess(int sig)
{
// Want to get the process ID of "TestApplication"
// Then force Kill it
}
'getpid()'返回父进程的pid。我想获得我刚刚开始的流程的pid。我刚刚启动了我的程序并检查了任务管理器以查看进程ID。我总是只获得父进程的ID。 – killer
@killer 如果您在父进程中检查'pid'变量。即在其他语句中,它是儿童过程的真实pid。 –
感谢@Tony Tannous指出了这一点。我现在得到正确的pid。干杯。 – killer