SI411 Operating Systems, Fall AY07

 

Lab1 – Spawning processes with the UNIX system call fork()

Due: Start of class, Friday, 8 September 2006

 

Overview: This lab examines the use of the UNIX system call fork() to spawn a child process.   See the course web page for your lab1 partner assignment.

 

Deliverables: Turn in a paper copy of your solution to Parts I, II, and III by the due date.

 

Start by copying the below C++ source code into a file named forkDemo.cpp under your UNIX account.  Recall that you can:

 

   Compile with: csb% g++ -o forkDemo forkDemo.cpp

                                                              

   Run with:

             csb% forkDemo  for output to the screen 

                             -or-

             csb% forkDemo > output.dat   for output to the textfile

             output.dat

 

Lab Assignment:

 

Part I: Run the below code (once to the screen to observe its behavior dynamically, and once with output directed to the file “output.dat” as per the above).  Print out a paper copy of the output it produces (output.dat).  Examine the paper copy of the output produced, and determine the process ID (pid) of the process that produced each line. Annotate, by hand, EACH line of the output file to identify the pid of the process that produced the line.

 

Part II: On the same paper copy of the output produced by Part I above, hand draw an activation record (memory image) for EACH process created by the below source code, showing the names and final values for each variable local to each process.

 

Part III: Explain which process completes first, and why.

 

// Source code begins here

 

#include <iostream>

#include <unistd.h>  // needed for getpid(), fork(), and sleep()

#include <stdlib.h>  // needed for system()

 

using namespace std;

 

int main() {

   int fork_return;

   int count = 0;

   int mypid;

 

   system("ps"); 

 

   /* getpid() returns the process id of this process. */

   cout << "pid_________  Process " << getpid()

        << " about to fork a child." << endl;

   fork_return = fork();

 

   if( fork_return < 0)

   {

      cout << " pid_________  Unable to create child process, exiting."

           << endl;

      exit(-1);

   }

   /* BOTH processes will do the below */

   system("ps");

 

   mypid = getpid();

 

   if( fork_return > 0)

   /* Then fork_return is the pid of the child process and I am

      the parent. Start printing a's. */

     {

      cout << " pid_________  Created child process "

           << fork_return << endl;

      while( count++ < 8) {

        cout << " pid_________  parent" << endl;

        sleep(2);

      }

   }

   else

   /* A 0 return tells me that I am the child. Print b's */

   {

      while(count++ < 8) {

         cout << " pid_________  child" << endl;

         sleep(1);

      }

    }

 

 cout << " pid_________  Mypid = " << mypid

      << " fork_return = " << fork_return

      <<  " count = " << count << endl;

 

 return 0;

}