COP 4600    - Operating Systems, Summer 2000
Project One - Minix Multitasking and Communication Using Pipes

------------------------------------------------------------------------
           PROJECT DUE DATE:  Friday, June 9, 2000 at 5pm
------------------------------------------------------------------------


PROJECT DESCRIPTION
------------------------------------------------------------------------
The purpose of this first project is to help you get familar with Minix
by writing a C program for Minix, compiling it using the Minix C
compiler (mcc), and executing the program from within Minix.  This first
project will also introduce you to multitasking and interprocess
communication using pipes.

You will not be required to recompile Minix for this project.

It is recommended that you write the C program in Solaris on one of the
CISE machines and compile it using the mcc command (see the Minix user
guide at http://www.cise.ufl.edu/~chow/).  You can then use the sunread
command to transfer your compiled program into the Minix operating
system to execute it.

Your assignment is to create one parent and multiple child processes
executing concurrently.  They will share a single pipe for the purpose of
sending messages from the parent to the children. 

Your program will accept two command-line parameters, specifying the
number of children and the number of messages, respectively.  The
acceptable number of children should be between 2 and 4, inclusively, and
the acceptable number of messages should be between 5 and 10, inclusively.

After creating all children, the parent will prompt the user for a message
and address this message to one of the children, selected at random.  The
parent will then place the message on the pipe.  The children will each
read from this pipe.  Since they will all be blocked, awaiting a message
from the parent, Minix must chose one of the children to awaken first.
This child will receive the message.  After reading the message, if the
child determines that the message was not intended for this child, it must
place the message back onto the pipe and await another message from the
parent.  In theory, each child will receive the message until it is
delivered to the correct child.

To allow the children time to recieve the message before the parent sends
another one, you will want to put a sleep() command following the parent's
write command.  Two seconds should be sufficient.

When the parent has sent the specified number of messages in the manner
described above, it will then send a termination message to the children.
The termination message will be identified by addressing it to child_id
number -1.  When a child receives a message addressed to child_id -1, it
should place that message back onto the pipe and then exit.  This will
allow a single termination message to propogate to all of the children.
The parent must wait for all of the children to terminate before exiting
itself.

You should prompt the user for each message one at a time, and you should
send each message after the message has been entered (as opposed to
reading all of the messages at the beginning and then sending all of
the messages at once).  Messages will be at most twenty characters in
length and may consist of letters, numbers, symbols, or spaces. If the
user enters messages more than twenty characters long, your program can
either ask for another input or trancate it to the maximum length.

Each process must display a message to the user whenever it reads a
message from the pipe or has to put one back onto the pipe.  Look at the
sample output provided below and structure your output simiarly.  As a
minimum, each message to the user must include the id of the current
process, the message number, the contents of the message, and who the
message was intended for.  The parent should also display a message to the
user when sending a message to the children, indicating what message
number it is, what the contents of the message are, and who it is intended
for.

Finally, in the event that an error occurs, you must display an error
message to the user and issue a kill() command to terminate all children
and the parent.

The messages sent from the parent to the children must be a structure of
the following definition:

    struct message {
        int  child_id;
        int  mesg_num;
        char mesg[21];
    }

Since this is the first project, we will give you a list of the relevant
functions necessary to complete this assignment.  You can learn about
these functions using man pages, your textbook, or a number of other
resources (such as the Web or books other than your textbook):

pipe()          creates a pipe
fork()          forks a child process
read()          read from a pipe (or file)
write()         write to a pipe (or file)
waitpid()       wait for a process to exit
rand()		random number generator
kill()		send an interrupt signal to all processes


==========================================================================
You must do your project independently.  No cheating will be tolerated.
Cheating will result in a grade of zero, and will be reported to the
student honor court.
==========================================================================



PROJECT REPORT REQUIREMENTS
------------------------------------------------------------------------
The report should consist of four parts:

Part 1: Describe any errors in your program and their locations in
        the code.  This is very important for partial credit.  If the
        program does not run, your description of the errors may be
        the only way to get partial credit.  You MUST use the report
        in conjunction with the code printout to describe the
        location of both logical and/or semantic errors that are
        stopping the code from running.  If you are unsure what the
        error is, please state so. If there is any part of the
        assignment that you have not completed, please note so
        clearly in the report (even if your program runs).  If your
        program runs perfectly and is complete, then simply note
        that.

        If we find an error or incomplete program that was not
        described, it will result in additional LOST POINTS (not just
        for the error, but also for not mentioning it).

Part 2: Provide a printout of your code.

Part 3: Provide a printout of a sample execution of your program.
        You should use the command "project1 4 7" as your sample
        execution, so that there are four children and seven
        messages.  To capture the output, use the "script" command
        as follows:

          o  Go to your Minix src/tools directory
          o  Issue the command "script project1.out" and press enter
          o  Run Minix as usual (sunread, execute your program, etc.)
          o  Exit Minix
          o  Issue the command "exit" and press enter

        This will generate a file in your src/tools directory named
        project1.out that contains a log of your Minix session.



ADDITIONAL REQUIREMENTS
------------------------------------------------------------------------
In order to receive full credit for this assignment, you must do the
following (in addition to getting the program working correctly and
writing the report):

You must put your full name and CISE username on the front page of your
report.  If you have a cover page, put it on the cover page.  If you do
not have a cover page, put it at the top of your report page.  In any
event, your name and username must be CLEARLY VISIBLE on the front page.
Failure to complete this easy step is an automatic five point penalty.

You must comment your code to the extent that someone who is not
familiar with your program can understand what your program does (in
other words, comment everything, and comment thoroughly).

Your program must handle errors. This includes errors in the command line
and errors returned by pipe, fork, waitpid, read, write, etc.

Your program must be modular.  It should be separated into at least two
functions, one for the parent and one for the children, and it should use
loops to handle the variable number of children/messages, as opposed to
hard-coding them.



PROJECT UPDATES AND CHANGES
------------------------------------------------------------------------
>From time to time it is necessary to make updates or changes to project
assignments.  If this is deemed necessary, the changes will be mentiond
during the next lecture session, emailed to all students on the cop4600
mailing list, and posted on the class web page.



SAMPLE OUTPUT
------------------------------------------------------------------------
Here is a sample of the output your program should generate.  Please note
that your output will probably not be exactly like this because of the
concurrency of the process executions.  Things may happen in a different
order for you.  You should be able to tell if your project works correctly
by reading through your output very carefully.

# project1 3 5 
Child 2 is created.
Child 1 is created.
Child 0 is created.
Enter a message (max 20 chars): one
Parent sending message #0 ("one") to Child 2
Child 0 received message #0 ("one") for Child 2
Child 0 put back message #0 ("one") for Child 2
Child 1 received message #0 ("one") for Child 2
Child 1 put back message #0 ("one") for Child 2
Child 2 received message #0 ("one") for Child 2
Enter a message (max 20 chars): 123
Parent sending message #1 ("123") to Child 1
Child 0 received message #1 ("123") for Child 1
Child 0 put back message #1 ("123") for Child 1
Child 1 received message #1 ("123") for Child 1
Enter a message (max 20 chars): @#$%^&
Parent sending message #2 ("@#$%^&") to Child 0
Child 0 received message #2 ("@#$%^&") for Child 0
Enter a message (max 20 chars): cop4600 operating systems
Parent sending message #3 ("cop4600 operating sy") to Child 1
Child 0 received message #3 ("cop4600 operating sy") for Child 1
Child 0 put back message #3 ("cop4600 operating sy") for Child 1
Child 1 received message #3 ("cop4600 operating sy") for Child 1
Enter a message (max 20 chars): having fun?
Parent sending message #4 ("having fun?") to Child 1
Child 0 received message #4 ("having fun?") for Child 1
Child 0 put back message #4 ("having fun?") for Child 1
Child 1 received message #4 ("having fun?") for Child 1
Child 0 exiting.
Child 1 exiting.
Child 2 exiting.
Parent exiting.



RUNAWAY MINIX PROCESSES
------------------------------------------------------------------------
PLEASE NOTE: Sometimes Minix will not exit properly and you will end up
with 'runaway' minix processes.  This irritates the system group
immensely, and they will begin banning you from CISE machines if you do
not 'clean up' your runaway minix processes.  To do this, follow these
instructions:

1) type  "ps -u $user"
       This will show you all processes you are running on the machine
       you are logged into.  If you see some listed as 'minix', follow
       the next instruction.  If there are no 'minix' processes running,
       you may safely log out.

2) type "kill -9 "
       Where  is the PID of the running minix process.  It is the
       (usually four digit) number in the first column of the ps output.

For example:

> ps -u $user
   PID TTY      TIME CMD
  3010 pts/15   0:01 tcsh
  3855 pts/15   0:04 minix
  3922 pts/17   0:01 tcsh
> kill -9 3855

Always do this before you log out.



GRADING GUIDELINES
------------------------------------------------------------------------
1. Report (10%)

   Your report must be clear and concise.  Points will be deducted for
   errors in your program that were not reported or for failure to
   include all three parts of the report.

2. Style (10%)

   The programming style must show readability of your program.  Thus,
   it must be well-organized.  We'll look for how well you use comments,
   how well you modularize the program, how meaningful the use and
   naming of constants and variables are, which variables should be
   global or local, etc.

3. Functionality (80%)

   It is very important that your program is compilable.  If we cannot
   compile your program without errors, your program will be considered
   non-functional.

   NOTE: If your program compiles but you did not make an effort to
   implement most of the features required, your program is still
   incomplete.  In other words, turning in a program that technically
   compiles but performs none of the functions requested is not
   permitted.

   If the program compiles but doesn't run we will use the report to
   understand the errors and give partial credit.

   If your program compiles and runs, we will test it according to the
   project description and give you points based on how well you
   implemented all that was required.

   Make sure to check your program for special cases!  We sure will!



GRADING DISTRIBUTION
------------------------------------------------------------------------
This is an approximation of how grades will be assigned.  We reserve the
right to change this grading criteria at any time without prior notice.

Report:
  Correct bug reporting and inclusion of code and sample output:   8 pts
  Placement of full name and CISE username on front page:          2 pts

Style:
  Meaningful variable names, proper use of global/local scope:     2 pts
  Modular code (as described in the additional requirements):      5 pts
  Adequate and thorough commenting:                                3 pts

Functionality:
  Correct number of children:       10 pts
  Correct number of messages:        5 pts
  Creation of pipe:                 10 pts
  Correct termination sequence:     15 pts
  Message passing:                  15 pts
  Command line arguments:            5 pts
  User input string handling:        5 pts
  Proper use of kill() command:      5 pts
  Proper use of rand() command:      5 pts
  Proper use of message structure:   5 pts
    


HOW TO SUBMIT YOUR PROJECT
------------------------------------------------------------------------
Details on how to submit your program source code will be given to you at
a later point in time via the cop4600 mailing list and through discussion
sessions.

Additionally, you must turn in your report (described above) by the
project deadline.  You may bring it to class and give it to Dr. Chow.
If you can not bring it to class, go to the CISE main office (301 CSE),
get your report timestamped, and then slide it under the TA's office door
by the deadline.

PLEASE DO NOT SLIDE REPORTS UNDER DR. CHOW'S DOOR.