COP4610 - Fall 1997 - P3 - Third Programming Assignment

Objectives

  1. Learn about the implementation details of semaphores.
  2. Solve a process synchronization problem using semaphores.

Deadlines and Deliverables

E-mail a copy of your modified JavaOS files complete with a journal to cop4610@cs.fsu.edu, by Wednesday, October 29th at midnight.

The method for emailing your solution is to create a uuencoded compressed tar image of only the files you changed. This way the grader can just uudecode, uncompress, and un-tar your files on top of an unmodified JavaOS source tree and your files will drop into place for easy grading. For example, assuming that you modified the files SOSSyscallIntHandler.java and AppTests.java, then you would turn in your assignment like this (substitute "tar" for "gnutar" if you are on a Linux machine):

cd
cd cop4610/P3
gnutar czvf ~/P3.tar.gz SOSSyscallIntHandler.java AppTests.java P3.journal
cd
chmod 700 P3.tar.gz
uuencode P3.tar.gz < P3.tar.gz 
      | /usr/ucb/mail -s "P3 submission" cop4610@cs.fsu.edu

Don't forget to include your journal file in the tar file!

Grading

This project will be graded on a scale of 0-100.

Getting Started

The following steps will install an entire working copy of the JavaSOS simulator into your home directory and build the simulator from the source code. Notice that you probably already have a "cop4610" directory (or equivalent) so all these steps may not be necessary. Also notice that this assignment uses a different JavaSOS starting source file from the previous assignments.

cd
mkdir cop4610
chmod 700 cop4610
cd cop4610
mkdir P3
chmod 700 P3
cd P3
gnutar xzvf ~jtbauer/jsosP3.tar.gz
make

To run the simulator, do the following:

java SIM

The simulator will run as described in the Guide to the Java Version of SOS available in class and from the class home page. A text file named sim.out is created that contains all the trace messages from the simulation run.

Assignment

Your assignment is in two parts. The first part extends JavaSOS by implementing semaphores and the second part uses the semaphore implementation to solve a concurrent programming problem.

Assignment - Part 1

Implement the four SOS semaphore operations: AttachSemaphore(), Wait(), Signal() and DetachSemaphore(). The calls are documented on page 309-310 of the texbook and SOS implementation code is in section 8.17 on pages 316 through 319. You do NOT have to modify SendMessageSystemCall and ReceiveMessageSystemCall to use semaphores, as is done in the SOS code on page 318.

To help you test your semaphore code the P3 tar file includes a special version of AppTests.java that includes the four new calls. To use the test, rename the existing AppTests.java and then rename the test file:

mv AppTests.java AppTests.java.orig
mv AppTests_2ProcRendezvous.java AppTests.java
make clean
make

The file AppTests_2ProcRendezvous.java contains a JavaSOS version of the two process rendezvous semaphore example on page 312. The test is started from the second JavaSOS application (which has been relabeled in the middle checkbox to say "Semaphore App").

Assignment - Part 2

Using your four new semaphore calls, write a solution to the following process synchronization problem. You will find the existing code in the AppTests.java rendezvous code a good starting point. Notice also that this version of JavaSOS has a correct solution to the first programming assignment (implementing WaitProcessSystemCall) and is available for your use (you can have a parent WaitProcessSystemCall on a child).


Coach Robinson, the new FSU men's basketball coach, is anxious to have his first year as head coach a successful one. His theme is back to the basics, with lots of drills and teamwork. He's asked the department to help design an algorithm that would maximize individual player throughput while increasing teamwork.

You are to write a basketball drill simulation using semaphores. Here are the rules and conditions:

  1. Initially there are five players on the court, each dribbling a basketball.
  2. Each player first must pause for 600 milliseconds, catching his breath (these guys are in good shape).
  3. A player approaches the basket down the shooting lane, with no more than three players in the shooting lane at a time.
  4. The player announces that they are in the lane, giving the total number of people in the lane at that time (that number should never exceed 3).
  5. One player at a time shoots their ball into the basket.
  6. The player always makes the basket and each basket is worth one point.
  7. The player announces that the shot was taken and gives his total points as well as the total team points at that point in time.
  8. After the basket is taken, the player pauses again for 600 milliseconds (this gives the player time to land back on the floor after a spectacular slam dunk).
  9. The player then leaves the lane and goes to the end of the line, waiting for another chance at the basket (back to the second step).
  10. Once the player has scored at least 5 points they leave the court.
  11. Once the last player has left the court the coach indicates the total number of points scored by all players and the simulation ends.

To make a player pause for 600 milliseconds, use the following Java code:

      try {
          Thread.currentThread().sleep(600);
        } catch (InterruptedException e) {}

The sample output below shows you how to design your SIM.trace() calls so it is easy to trace the player's progress. Make sure that you have each player indicate what event they are currently handling and include their player number. The dispatcher code in SOSProcessManager.java has been modified to use a random selection of Ready processes, so the ordering of the player's executions will not always be the same. In fact, a correct process synchronization problem should not require processes to run in a certain order, since that is not under the user's control.


Sample output from the basketball drill (only Application-level trace messages shown):

Welcome to the SOS simulator
Java version is 1.1.3
Java vendor is Sun Microsystems Inc.
Java home is E:\JDK1.1.3\BIN\..
Java CLASSPATH is .;e:\jdk1.1.3\classes;E:\JDK1.1.3\BIN\..\classes;E:\JDK1.1.3\BIN\..\lib\classes.zip
Initialize SOS (Simple Operating System)
Initialize IO System
Initialize Process System
Suspending the startup thread (for good)
Coach: Attaching to the semaphores.
Coach: Starting player processes!
Coach: player 0 pid = 2
Coach: player 1 pid = 3
Coach: player 2 pid = 4
Coach: player 3 pid = 5
Coach: player 4 pid = 6
Coach: Starting drills!
Coach: Waiting for player 0 to leave.
Player[3]: Starting drills!
Player[2]: Starting drills!
Player[3]: In the lane.
Player[3]: There are now 1 player(s) in the lane
Player[3]: At the basket.
Player[3]: Taking my shot.
Player[3]: Shot dropped!  I now have 1 baskets; team total = 1
Player[1]: Starting drills!
Player[1]: In the lane.
Player[1]: There are now 2 player(s) in the lane
Player[4]: Starting drills!
Player[3]: Leaving basket.
Player[3]: Leaving lane.
Player[3]: In the lane.
Player[3]: There are now 2 player(s) in the lane
Player[2]: In the lane.
Player[2]: There are now 3 player(s) in the lane
Player[1]: At the basket.
Player[1]: Taking my shot.
Player[1]: Shot dropped!  I now have 1 baskets; team total = 2
Player[0]: Starting drills!
Player[1]: Leaving basket.
Player[1]: Leaving lane.
Player[0]: In the lane.
Player[0]: There are now 3 player(s) in the lane
Player[2]: At the basket.
Player[2]: Taking my shot.
Player[2]: Shot dropped!  I now have 1 baskets; team total = 3
Player[2]: Leaving basket.
Player[2]: Leaving lane.
Player[0]: At the basket.
Player[0]: Taking my shot.
Player[0]: Shot dropped!  I now have 1 baskets; team total = 4
Player[0]: Leaving basket.
Player[0]: Leaving lane.
Player[2]: In the lane.
Player[2]: There are now 2 player(s) in the lane
Player[1]: In the lane.
Player[1]: There are now 3 player(s) in the lane
Player[3]: At the basket.
Player[3]: Taking my shot.
Player[3]: Shot dropped!  I now have 2 baskets; team total = 5
Player[3]: Leaving basket.
Player[3]: Leaving lane.
Player[4]: In the lane.
Player[4]: There are now 3 player(s) in the lane
Player[1]: At the basket.
Player[1]: Taking my shot.
Player[1]: Shot dropped!  I now have 2 baskets; team total = 6
Player[1]: Leaving basket.
Player[1]: Leaving lane.
Player[4]: At the basket.
Player[4]: Taking my shot.
Player[4]: Shot dropped!  I now have 1 baskets; team total = 7
Player[3]: In the lane.
Player[3]: There are now 3 player(s) in the lane
Player[4]: Leaving basket.
Player[4]: Leaving lane.
Player[3]: At the basket.
Player[3]: Taking my shot.
Player[3]: Shot dropped!  I now have 3 baskets; team total = 8
Player[3]: Leaving basket.
Player[3]: Leaving lane.
Player[2]: At the basket.
Player[2]: Taking my shot.
Player[2]: Shot dropped!  I now have 2 baskets; team total = 9
Player[4]: In the lane.
Player[4]: There are now 2 player(s) in the lane
Player[2]: Leaving basket.
Player[2]: Leaving lane.
Player[4]: At the basket.
Player[4]: Taking my shot.
Player[4]: Shot dropped!  I now have 2 baskets; team total = 10
Player[4]: Leaving basket.
Player[4]: Leaving lane.
Player[0]: In the lane.
Player[0]: There are now 1 player(s) in the lane
Player[0]: At the basket.
Player[0]: Taking my shot.
Player[0]: Shot dropped!  I now have 2 baskets; team total = 11
Player[1]: In the lane.
Player[1]: There are now 2 player(s) in the lane
Player[3]: In the lane.
Player[3]: There are now 3 player(s) in the lane
Player[0]: Leaving basket.
Player[0]: Leaving lane.
Player[4]: In the lane.
Player[4]: There are now 3 player(s) in the lane
Player[3]: At the basket.
Player[3]: Taking my shot.
Player[3]: Shot dropped!  I now have 4 baskets; team total = 12
Player[3]: Leaving basket.
Player[3]: Leaving lane.
Player[4]: At the basket.
Player[4]: Taking my shot.
Player[4]: Shot dropped!  I now have 3 baskets; team total = 13
Player[0]: In the lane.
Player[0]: There are now 3 player(s) in the lane
Player[4]: Leaving basket.
Player[4]: Leaving lane.
Player[0]: At the basket.
Player[0]: Taking my shot.
Player[0]: Shot dropped!  I now have 3 baskets; team total = 14
Player[0]: Leaving basket.
Player[0]: Leaving lane.
Player[2]: In the lane.
Player[2]: There are now 2 player(s) in the lane
Player[1]: At the basket.
Player[1]: Taking my shot.
Player[1]: Shot dropped!  I now have 3 baskets; team total = 15
Player[1]: Leaving basket.
Player[1]: Leaving lane.
Player[4]: In the lane.
Player[4]: There are now 2 player(s) in the lane
Player[2]: At the basket.
Player[2]: Taking my shot.
Player[2]: Shot dropped!  I now have 3 baskets; team total = 16
Player[2]: Leaving basket.
Player[2]: Leaving lane.
Player[4]: At the basket.
Player[4]: Taking my shot.
Player[4]: Shot dropped!  I now have 4 baskets; team total = 17
Player[3]: In the lane.
Player[3]: There are now 2 player(s) in the lane
Player[0]: In the lane.
Player[0]: There are now 3 player(s) in the lane
Player[4]: Leaving basket.
Player[4]: Leaving lane.
Player[0]: At the basket.
Player[0]: Taking my shot.
Player[0]: Shot dropped!  I now have 4 baskets; team total = 18
Player[1]: In the lane.
Player[1]: There are now 3 player(s) in the lane
Player[0]: Leaving basket.
Player[0]: Leaving lane.
Player[1]: At the basket.
Player[1]: Taking my shot.
Player[1]: Shot dropped!  I now have 4 baskets; team total = 19
Player[4]: In the lane.
Player[4]: There are now 3 player(s) in the lane
Player[1]: Leaving basket.
Player[1]: Leaving lane.
Player[4]: At the basket.
Player[4]: Taking my shot.
Player[4]: Shot dropped!  I now have 5 baskets; team total = 20
Player[2]: In the lane.
Player[2]: There are now 3 player(s) in the lane
Player[4]: Leaving basket.
Player[4]: Leaving lane.
Player[4]: Leaving the court.
Player[1]: In the lane.
Player[1]: There are now 3 player(s) in the lane
Player[2]: At the basket.
Player[2]: Taking my shot.
Player[2]: Shot dropped!  I now have 4 baskets; team total = 21
Player[2]: Leaving basket.
Player[2]: Leaving lane.
Player[1]: At the basket.
Player[1]: Taking my shot.
Player[1]: Shot dropped!  I now have 5 baskets; team total = 22
Player[1]: Leaving basket.
Player[1]: Leaving lane.
Player[1]: Leaving the court.
Player[3]: At the basket.
Player[3]: Taking my shot.
Player[3]: Shot dropped!  I now have 5 baskets; team total = 23
Player[0]: In the lane.
Player[0]: There are now 2 player(s) in the lane
Player[3]: Leaving basket.
Player[3]: Leaving lane.
Player[3]: Leaving the court.
Player[0]: At the basket.
Player[0]: Taking my shot.
Player[0]: Shot dropped!  I now have 5 baskets; team total = 24
Player[2]: In the lane.
Player[2]: There are now 2 player(s) in the lane
Player[0]: Leaving basket.
Player[0]: Leaving lane.
Player[0]: Leaving the court.
Coach: Exit of player 0 returns = 0
Coach: Waiting for player 1 to leave.
Coach: Exit of player 1 returns = 0
Coach: Waiting for player 2 to leave.
Player[2]: At the basket.
Player[2]: Taking my shot.
Player[2]: Shot dropped!  I now have 5 baskets; team total = 25
Player[2]: Leaving basket.
Player[2]: Leaving lane.
Player[2]: Leaving the court.
Coach: Exit of player 2 returns = 0
Coach: Waiting for player 3 to leave.
Coach: Exit of player 3 returns = 0
Coach: Waiting for player 4 to leave.
Coach: Exit of player 4 returns = 0
Coach: Drills over!  Total baskets = 25
Coach: Detaching from the semaphores.
Coach: Detach mutex returns 0
Coach: Detach lane returns 0
Coach: Detach basket returns 0
Parent: Exiting semaphore testing app
System idle. No ready processes, no timer, no disk IO