Lab 2: Threads and Semaphores

Due: Wed, Oct 24, 2001, 12:00 Noon
Web posted: Mon, Oct 01, 2001, 16:34 hrs



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. Before you begin, please read though Sec 6.7 completely and make sure you understand the notion of priorities and how the JVM schedules threads.

    Section 6.7.3 provides sample code for implementing a round-robin scheduler for Java threads. Using this code as a starting point, implement a MLFQ scheduler that has two queues. The priorities of threads in the two queues should be 2 and 3 respectively. 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 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 priority level 3 and two at priority level 2. 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 the next thread to run.

    It is important to understand that the JVM has its own thread scheduler that uses priorities. Your scheduler does not replace the JVM scheduler, rather it emulates a (user-level) scheduling policy using the notion of priorities. By increasing the priority of the next thread to be scheduled, your user-level sheduler helps the JVM scheduler "pick" the right thread for execution. In some sense, the JVM scheduler acts like a kernel CPU scheduler that we learnt in class, while your scheduler acts like a user-level scheduler in a threads library.
     

  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 (an integer variable), it should produce the following output to the screen:

      Reader <id> reading <integer>
    where <id> is the unique identifier of the Reader and <integer> is the current value of the shared integer variable.

    The Writer threads want to update (i.e. to read and write) the shared object. When a writer updates the shared object (by incrementing it by 1), it should produce the following output on the screen:

      Writer <id> writing <integer>
    where <id> is the unique identifier of the writer and <integer> is the new value written 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 inputs as we may test it with inputs other than what you provide in your sample output listing.


    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.