Fun with Fork

The fork() command causes your program to create an exact copy of itself. Immediately after the fork executes, two identical processes will be running. All the variables have the same values. The instruction pointer is in exactly the same place (in other words, the clone starts running right where the original process was running). Everything except the process id number is exactly the same.


Type this program and figure out what it is doing.

// Steve Dannelly
// CSCI 208 Lab #14
#include <iostream>
using namespace std;
int main ()
{
  int value;  // a variable for both processes

  // get something to remember
  cout << "Enter an integer: ";
  cin >> value;

  // create a clone
  fork();

  // output some information
  cout << "I am process " << getpid();
  cout << ", my parent is " << getppid();
  cout << ", and you entered the number " << value << endl;
}


Modify the program so that only the child prints the output message. In other words, put an if statement after the fork(); that the child will find true and the parent will find false.


Turn in a printout of your modified program in one week.