Okay, so as time permits I've been trying to get this going and I'm still kind of confused. Below is the parent portion of the forking code. The issue I'm having is that no matter what I do, when I specify WNOHANG in the waitpid function, it automatically returns a WIFEXITED status of 0. What I want it to do is continue iterating through the do/while loop until either the child exits with status, or waittime equals a predesignated timeout value and the parent hard kills the child.
Right now, the do/while loop exits because waitpid passes a 0 to Stat as the result of specifying NOHANG.
Code:
else {
printf("The pid of the child is: %d\n", pid);
int waittime = 0;
int Stat;
do {
wpid = waitpid(pid, &Stat, WNOHANG);
if (WIFEXITED(Stat)) {
printf("Child exited, status=%d\n", WEXITSTATUS(Stat));
break;
}
else if (WIFSIGNALED(Stat)) {
printf("Child %d was terminated with a status of: %d \n", pid, WTERMSIG(Stat));
break;
}
else if (WIFSTOPPED(Stat)) {
printf("Child stopped (signal %d)\n", WSTOPSIG(Stat));
break;
}
else if (waittime < timeout){
printf("Parent waiting %d second(s).\n", waittime);
waittime ++;
sleep(1);
}
else {
printf("Killing child process\n");
kill(pid, SIGKILL);
}
} while (!WIFEXITED(Stat) && !WIFSIGNALED(Stat));
}