#include #include #include using namespace std; int main(){ pid_t mypid{getpid()}; cout << "The parent has pid " << mypid << endl; // fork a process and exit. // this will be a zombie until the parent calls wait pid_t zombie{fork()}; if (zombie == 0) { mypid = getpid(); cout << "\tThe child has pid " << mypid << endl; usleep(1000); cout << "\tThe child will exit " << endl; return 0; } usleep(100000); cout << "Parent waking up" << endl; cout << "Enter to continue " ; cin.ignore(1000,'\n'); string fileName{"/proc/"+to_string(zombie)+"/status"}; pid_t forked{fork()}; if (forked == 0) { cout << "\tNew Child running head -4 " << fileName << endl; execl("/usr/bin/head", "head", "-4", fileName.c_str(), nullptr); perror("\texecl"); return 1; } int loc; cout << " Parent waiting on " << forked << endl; waitpid(forked, &loc, 0); cout << " Parent waiting on " << zombie << endl; waitpid(zombie, &loc, 0); wait(&loc); return 0; }