#include #include #include #include #include using namespace std; int main(int argc, char *argv[]) { int pid; int count = 3; int i; count--; pid=fork(); if (pid > 0) { /* parent continues here */ count--; pid = fork(); if (pid == 0) { /* second child */ count--; if (count == 0) system("/bin/ps -el"); // Last leaf //execlp("/bin/ps","ps","-el", NULL); // Last leaf // -f to get ppid for process tree // -a would show ps command as well // -e goes all the way back } else if (pid > 0) { // Parent ends wait(NULL); cout << endl; } else // Bad fork exit(-1); wait(NULL); // parent wait } else if (pid == 0) { /* first child */ for (i = 0; i < 30000000; i++); // Leaf - delay } else /* only if there was a problem with fork */ exit(-1); return 0; }