Question

I'm trying to write a program that checks for exited child processes and restarts them if they have exited. It needs to restart the processes as they exit, without waiting for any other processes to have exited. I'm a little lost. Here's the code that I've done so far. It's not finished or correct, really. But perhaps someone can point me in the right direction?

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);
}

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top