/* CSE 333 Su12 Lecture 20 demo: fork_example.cc */ /* Gribble/Perkins */ // Fork a child process and wait for it to finish. // The sleep delays should make it possible to see the various process // states using ps. #include #include #include #include #include int main(int argc, char **argv) { std::cout << "[parent] about to fork.." << std::endl; pid_t cpid = fork(); if (cpid == 0) { // child std::cout << "[child] I'm alive!" << std::endl; sleep(10); std::cout << "[child] exiting...becoming a zombie." << std::endl; return EXIT_SUCCESS; } // parent int stat_loc; std::cout << "[parent] My child lives!" << std::endl; sleep(30); std::cout << "[parent] Waiting for my child to die..." << std::endl; waitpid(cpid, &stat_loc, 0); std::cout << "[parent] My child has died." << std::endl; sleep(20); return EXIT_SUCCESS; }