#include #include #include #include #include #include using namespace std; void TellCase(int c); int main() { pid_t pid; int myCase{0}; for(myCase = 0; myCase < 12; ++myCase) { cout << endl; TellCase(myCase); pid = fork(); if (pid == 0) { switch (myCase) { case 0: return(0); case 1: return(1); case 2: return(2); case 3: return(3); case 4: return(256); case 5: return(257); case 6: raise(1); break; case 7: raise(9); break; case 8: exit(2); break; case 9: _exit(7); break; case 10: abort(); } return 0; } else { int status; pid_t exitPid; exitPid = wait(&status); 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; } else if (WIFSIGNALED(status)) { int sig = WTERMSIG(status); cout << "\t Wait returned because the child was signaled" << endl; cout << "The signal was "; cout << sig << endl; } 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; return 0; } void TellCase(int c){ cout << endl; cout << "Case " << c << endl; switch(c) { case 0: cout << " return(0); " << endl; break; case 1: cout << " return(1); " << endl; break; case 2: cout << " return(2); " << endl; break; case 3: cout << " return(3); " << endl; break; case 4: cout << " return(256); " << endl; break; case 5: cout << " return(257); " << endl; break; case 6: cout << " raise(1); break; " << endl; break; case 7: cout << " raise(9); break; " << endl; break; case 8: cout << " exit(2); break; " << endl; break; case 9: cout << " _exit(7); break; " << endl; break; case 10: cout << " abort(); break; " << endl; break; case 11: cout << " fallthrough " << endl; break; default: cout << "Unknown case " << endl; } cout << endl; }