orphan.cpp

URL: https://mirkwood.cs.edinboro.edu/~bennett/class/cmsc4000/spring2026/notes/ch3/code/orphan.cpp
 

#include <unistd.h>
#include <iostream>


using namespace std;

int main() {
    cout << endl << endl;

    pid_t forkValue {fork()};
    string cmd = "head -10 /proc/self/status";

    switch(forkValue) {
       case 0:
           cout << getpid() << " is the child" << endl;
           cout << getpid() << " is the parent " << endl;
           cmd = "ps -efH | grep " + to_string (getpid());

           cout << endl;
           cout << "Executing " << cmd << endl;
           system(cmd.c_str());

           sleep(2);
    
           cout << endl;
           cout << "Executing " << cmd << endl;
           system(cmd.c_str());
           cout << endl;

           return 0;
       default:
           usleep(100);
           cout << getpid() << " the parent, is now exiting" << endl;
           return 0;

    }
}