CS-384 Lab 1

Unix System Processes

 

Date:            Tuesday, November 27, 2001

Due:            Tuesday, December 4, 2001

 

The purpose of this lab is to begin to become familiar with Unix system calls (described in section two of the on-line manual pages).  The system calls we’re immediately interested in are:  fork(), vfork(), exit(), _exit(), wait(), waitpid(), and the six different forms of exec(), which are:  execl(), execlp(), execle(), execv(), execvp(), and execve().  Use the ‘man’ command to read about these syscalls.  One caution about the man command:  It uses ‘less’ to paginate output to the screen.  So, use the space bar to advance a full page, use return to advance one line, and use ‘q’ to quit (exit) the man command.  To learn more about less (no pun intended!) read the man page (‘man less’) or type the ‘h’ command from within less.  Also, use the ‘man –k’ option to search for keywords in the descriptions of each manual entry.  There is also a ‘help’ command on Unix, but that only provides information on commands that can be invoked from a ‘shell script’.  One of the things students like to do is print out man pages.  To do this, use the following filter:

 

      (concord) > man –t fork | mpage | print_cc35

 

Obviously, everything up to the ‘>’ is the system prompt.  The ‘mpage’ command will, by default, convert the typeset output from man to ‘4-up’ format (which prints 4 page images on one sheet of paper).  If your eyes are really good, try ‘mpage –8’.  There are predefined aliases for printing on concord:  print_cc35 and print_cc36, so use whichever one is available.  These aliases can also be invoked from the command line as such:  print_cc35 myfile.cc

 

The C++ compiler that we’ll use for the course is the GNU g++ compiler and is invoked by the command:  g++ myfile.cc   This will produce an executable (barring any errors) in file ‘a.out’.  Pick your favorite editor to generate source files (pico, nedit, xedit, …).  There is also a ‘make’ utility on concord, which invokes the GNU make command (check the man pages on this as well).  An alternative is to use Linux on your laptop system.  It comes with a development environment called ‘K Develop’, which is very much like Microsoft Visual Studio; so you may be more comfortable with that IDE.  Once you’ve become familiar with your build environment, try to compile and execute the example Unix system programs I’ve posted to the course web page (www.msoe.edu/~blessing/cs384).  You’ll need to use these as guides for writing your own programs.

 

Once you’ve looked over the sample programs, the task is to write a program that creates three child processes; has each one sleep for a different amount of time (check out ‘man 3 sleep’); and has the parent wait for all three children to complete before exiting itself.  The parent is your program, which will need to use waitpid() in order to distinguish which children have completed and, on which ones it needs to continue to wait.  Have your parent program report the creation of each child process (by sending a message to the console).  When it has successfully completed its work, have the parent report the successful termination of each child process.

 

This is a team assignment.  Work with your group to complete this program.  Then, submit a well-commented source file and a separate report document that tells how you accomplished the tasks at hand in a well-written, detailed manner.  Be aware that your report acts as the external documentation for your program, and so it must tell the story of how your code is designed and works.

 

The following table lists several macros that are needed to process return status information from a system call:

 

 

Macro

Use

WIFEXITED(*status_p)

Returns a nonzero value if a child was terminated via an exit (or _exit) call, and zero otherwise.

WEXITSTATUS(*status_p)

Returns a child exit code that was assigned to an exit (or _exit) call.  This should be called only if WIFEXITED returns a nonzero value.

WIFSIGNALED(*status_p)

Returns a nonzero value if a child was terminated due to a signal interruption.

WTERMSIG(*status_p)

Returns the signal number that had terminated a child process.  This should be called only if WIFSIGNALED returns a nonzero value.

WIFSTOPPED(*status_p)

Returns a nonzero value if a child process has been stopped due to job control.

WSTOPSIG(*status_p)

Returns the signal number that had stopped a child process.  This should be called only if WIFSTOPPED returns a nonzero value.

 

A complete description of these macros can be found in the man pages for wait() and waitpid().