#include #include #include using namespace std; void PrintInfo(string me) { cout << "+++++++++++++++++++++++++" << endl; cout << "I am " << me << endl; cout << "\tMy PID is " << getpid() << endl; cout << "\tMy PPID is " << getppid() << endl; cout << "+++++++++++++++++++++++++" << endl; cout << endl; return; } int main() { pid_t forked, me; ofstream outFile; int fd; outFile.open("ForkFile", std::ofstream::app); if (!outFile) { cerr<< "Opening ForkFile failed" << endl; } outFile << "Before Fork: " << getpid() << endl; PrintInfo("Prefork"); forked = fork(); cout << "Fork returned " << forked << endl; cout << endl; if (0 == forked) { PrintInfo("Return of fork 0, child"); outFile << "After Fork rv = 0: " << getpid() << endl; } else { sleep(1); outFile << "After Fork rv != 0: " << getpid() << endl; cout << "In if, fork returned " << forked << endl; PrintInfo("Return of fork not 0, parent"); } cout << getpid() << " exiting " << endl; cout << endl; return 0; }