我正在尝试编写一个程序来检查退出的子进程并在它们退出时重新启动它们。它需要在进程退出时重新启动它们,而无需等待任何其他进程退出。我有点失落。这是我到目前为止所做的代码。它没有完成或正确,真的。但也许有人可以指出我正确的方向?
for(int ifile = 1; ifile < 4; ifile++){
child_pid[ifile - 1] = vfork();
if(child_pid[ifile - 1] == -1){
cerr << "fork error on " << argv[ifile] << endl;
}
else if(child_pid[ifile - 1] == 0){
execl(argv[ifile], argv[ifile], NULL);
}
}
for(int index = 0; index < 3; index++){
do{
wait_pid = waitpid(child_pid[index], &status, WUNTRACED);
if(WIFEXITED(status)){
count++;
child_pid[index] = vfork();
if(child_pid[index] == -1){
cerr << "rescheduled process error" << endl;
return -1;
}
else if(child_pid[index] == 0){
cout << "Rescheduling " << argv[index + 1] << endl;
execl(argv[index + 1], argv[index + 1], NULL);
}
}
}while(count != 4);
}