#include #include #include #include #include #include using namespace std; void TellCase(int c); int main() { pid_t pid; cout << endl; pid = fork(); if (pid == 0) { cout << "About to stop myself " << endl; cout << "kill -SIGCONT " << getpid() << endl; raise(SIGSTOP); return 0; } else { bool done{false}; while (! done) { int status; pid_t exitPid; exitPid = waitpid(0, &status, WSTOPPED| WCONTINUED); if (exitPid == -1) { perror("Wait Failed"); } else { cout << "A child exited " << endl; cout << "\tThe PID was " << exitPid << " and the fork pid was " << pid << endl; if (WIFEXITED(status)) { int code = static_cast(WEXITSTATUS(status)); cout << "\t Wait returned because the child was exited" << endl; cout << "\t Exit staus returned "; cout << code; done = true; } else if (WIFSIGNALED(status)) { int sig = WTERMSIG(status); cout << "\t Wait returned because the child was signaled" << endl; cout << "\t The signal was "; cout << sig << endl; done = true; } else if (WIFSTOPPED(status)) { int sig = WSTOPSIG(status); cout << "\t Wait returned because the child was stopped" << endl; cout << "\t The stop signal was " << sig << endl; } else if (WIFCONTINUED(status)) { cout << "\t Wait returned because the child was continued" << endl; } else { cout << "\t unable to determine why the wait returned" << endl; } } } } cout << endl << endl<< endl; pid = fork(); if (pid == 0) { cout << "Child taking a nap" << endl; sleep(10); cout << "Child going away "<< endl; return(0); } else { int status; pid_t exitPid{0}; exitPid = waitpid(0, &status, WNOHANG); while (exitPid == 0) { cout << "Nope, it did not exit yet" << endl; sleep(1); exitPid = waitpid(0, &status, WNOHANG); } } cout << endl; return 0; }