Lab 2: Threads and Semaphores

Due: Wed, Oct 18, 2000, 12:00 Noon
Web posted: Tue, Oct 03, 2000, 10:38 PM



Getting Started

The Assignment

For this assignment and all subsequent ones, you may work in groups of 1 or 2.
  1. Thread Scheduling
    This question is based on Section 6.7 of the text book. The text provides sample code for implementing a implementing round-robin scheduler for Java threads. Using this code as a starting point, implement a scheduler that has three queues with different priorities, one each with priority 2,3 and 4. Have the scheduler select a thread from the highest-priority non-empty queue, set that thread's priority to 5 and allow the thread to run for the time slice for that queue. Assume that times slice for each priority level is 3* DEFAULT_TIME_SLICE, 2* DEFAULT_TIME_SLICE and DEFAULT_TIME_SLICE, respectively. When this time slice expires, select the next highest priority thread and repeat the process. Assume that round-robin scheduling is used at each priority level, and that a higher priority thread that wakes up does not preempt a currently executing thread. Unlike multi-level feedback queue scheduling, assume that your simple scheduler does not move threads from one priority-level to another.

    Test your scheduler with three threads, one at each priority level. Have each thread run in a loop incrementing a counter. After every 10,000 loops, have each thread print out its id and the current value of its counter and then sleep for 5 seconds, allowing a lower priority thread to run.
     

  2. Threads and Semaphores
    Using Java semaphores described in Section 7.8.6, implement reader/writer communication.

  3. A data object is to be shared among several concurrent threads Some of the threads - Readers want only to read the content of the shared object. When a reader, reads the content of the shared object(a single character), it should produce the following output to the screen:

      Reader <id> reading <character>
    where <id> is the unique identifier of the Reader.

    The Writer threads want to update (i.e. to read and write) the shared object (a single character which it obtains from a file called writer_input.txt). When a writer updates the shared object, it should produce the following output on the screen:

      Writer <id> writing <character>
    where <id> is the unique identifier of the writer and <character> is the character just written on to the shared object.

    Test your solution with multiple readers and writers. Of course, with multiple readers and writers, the output will illustrate the concurrency provided by threads.

    Your code should prompt for the number of readers and the number of writers, in that order. You can assume that a valid integer is provided by the user for each of these. You should be sure that your code works for different input files as we may test it with files other than what you provide.
     

  4. The Sleeping-Barbers Problem
    A barber shop consists of a waiting room with n chairs, and a barber room with one barber chair. If there are no customers to be served, the barber goes to sleep. If a customer enters the barber shop and all chairs are occupied, then the customers leaves the shop. If the barber is busy but chairs are available, then the customers sits in one of the free chairs. If the barber is asleep, the customers wakes up the barber. Write a program to coordinate the barber and the customers using Java synchronization. Your program should take n as input and print out the action of each customer and the barber at each step.

  5. How to Turn in Lab 2

    All of the following files and hard copies must be turned in to get full credit for a question.
    1. Create a directory in your course directory called ~/cs377/lab2. Make the directory group writable (chmod g+w ~/cs377/lab2) and put all files in this directory.
    2. Hand in a hard copy of all the files that you modified.
    3. Hand in a hard copy of a README file identifying your lab partner (if you have one) and containing an outline of what you did for the assignment. It should also explain and motivate your design choices. Keep it short and to the point. Your ~/cs377/lab2 directory should also contain this file.

    4. If your implementation does not work, you should also document the problems in the README, preferably with your explanation of why it does not work and how you would solve it if you had more time. Of course, you should also comment your code. We can't give you credit for something we don't understand!
    5. Hand in a hard copy showing sample output from your programs.
    6. Individual Group Assessment (for students working in groups only)
    7. 10 percent of your lab grade will come from your participation in this project as a member of your two person group.
      What you need to turn in (each person individually):
      Turn in a hard copy of your assessment of the division of labor in the group in the format shown below.  If you give yourself a 50% and your partner gives you a 50%, you will get the whole 10 points.  If you give your partner a 40% and your partner gives himself or herself a 40%, he or she will get 8 points.  And so on...
    8. Note: We will strictly enforce policies on cheating. Remember that we routinely run similarity checking programs on your solutions to detect cheating. Please make sure you turn in your own work.