我想寫一個程序,檢查退出的子進程,並重新啓動它們,如果他們退出。它需要在進程退出時重新啓動進程,而不必等待任何其他進程退出。我有點迷路。以下是我迄今爲止所做的代碼。它沒有完成或沒有改變,真的。但也許有人可以指出我正確的方向?檢查POSIX中退出的子進程?
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);
}